Emit one MongoDB embedded config per (parent, path) occurrence#164
Open
nyancat3 wants to merge 2 commits into
Open
Emit one MongoDB embedded config per (parent, path) occurrence#164nyancat3 wants to merge 2 commits into
nyancat3 wants to merge 2 commits into
Conversation
The Mongoid schema generator grouped models by collection name and emitted a single embedded config bound to one embedded_in, so an embedded class embedded at more than one (parent, path) place got a config for only one occurrence; per-field masking declared for that class applied at that single location while every other occurrence dumped raw. Discover every occurrence by scanning all models' embeds_one/embeds_many relations and emit one config per occurrence. The occurrence matching the class's declared embedded_in keeps the plain collection name (existing single-occurrence output is byte-for-byte unchanged and never renamed when a second occurrence later appears); each additional occurrence gets a name disambiguated by its parent collection and path. Each occurrence merges against its own file on regeneration, preserving hand-edited masks per occurrence. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Mongoid schema generator so that when the same embedded class appears in multiple distinct (parent collection, path) locations, exwiw emits one embedded config per occurrence rather than a single config per embedded class/collection—ensuring masking rules apply everywhere the value object is embedded.
Changes:
- Extend
Exwiw::MongoidSchemaGeneratorto discover embedded occurrences by scanning all models’embeds_one/embeds_manyrelations and emit one config per(parent, path)occurrence. - Add dummy Mongoid models + specs covering multi-occurrence emission, per-occurrence file output, regeneration durability, and end-to-end masking via
MongodbAdapter. - Update README and CHANGELOG to document the new behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| spec/mongoid_schema_generator_spec.rb | Adds specs validating per-occurrence config emission, file persistence across regen, and adapter-driven masking correctness. |
| script/mongoid_models.rb | Adds isolated dummy models representing a shared embedded value object used in two distinct (parent, path) locations. |
| README.md | Documents per-occurrence embedded config behavior and provides an illustrative example. |
| lib/exwiw/mongoid_schema_generator.rb | Implements embedding occurrence indexing and multi-occurrence config emission. |
| CHANGELOG.md | Notes the new generator behavior under Unreleased. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+88
to
96
| private def build_configs_for(collection_name, models, existing_by_name, embedding_index) | ||
| occurrences = embedding_index[collection_name] | ||
|
|
||
| if models.any?(&:embedded?) && occurrences.size >= 2 | ||
| build_multi_occurrence_configs(collection_name, models, occurrences, existing_by_name) | ||
| else | ||
| [build_collection_for(collection_name, models, existing_by_name[collection_name])] | ||
| end | ||
| end |
Comment on lines
+229
to
+234
| declared = | ||
| begin | ||
| embedded_in_for(models.find(&:embedded?)) | ||
| rescue StandardError | ||
| nil | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
Mongoid スキーマ生成タスク(
rake exwiw:schema:generate_mongoid)は、モデルをコレクション名でグルーピングし、埋め込みドキュメントのクラスごとに 1 つだけ 設定ファイルを出力していた。しかし同じ埋め込みクラスが複数の(親コレクション, パス)に埋め込まれている場合(親をまたいで再利用される値オブジェクト、あるいは 1 つの親の複数キーに埋め込まれるケース)、生成されるembedded_inは 1 箇所ぶんだけになる。その結果、そのクラスに対して宣言したフィールド単位のマスキング(
replace_with/ignore)は 1 箇所にしか効かず、他の埋め込み箇所は素のままダンプされてしまう。一方でランタイム(
MongodbAdapter)は、埋め込み設定を(embedded_in.collection_name, path)ごとに独立して適用できる作りになっている。ギャップは生成側にのみ存在していた。対応
embeds_one/embeds_manyを走査して、埋め込みクラスの(親コレクション, パス)の出現箇所をすべて発見し、出現箇所ごとに 1 つの設定を出力するようにした。embedded_inに一致する出現箇所は、従来どおりコレクション名そのままの名前を維持する。これにより:addresses__shipments__address)を付与。ランタイムはこの名前ではなくembedded_inでサブドキュメントを解決するため、名前は識別子・ファイル名としてのみ機能する。補足(耐久性の確認)
schema:tidyは存在せず(tidyは ActiveRecord 版SchemaGeneratorのみ)、write_filesは生成対象外のファイルを削除しない。したがって出現箇所ごとの設定ファイルが再生成で削除されることはない。テスト
MODELS/SEEDには含めず、既存のスナップショット/件数コントラクトに影響なし)。embedded_inの設定が生成されるMongodbAdapterのマスキングを両箇所で駆動する(エンドツーエンド)bundle exec rspec: 699 examples, 0 failures。