MONGOID-5900, MONGOID-5901 - Bugfix 🐛 The combination of localized and aliased fields is broken with pluck#6044
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bug where plucking localized and aliased fields was broken due to incorrect field name resolution. The fix ensures that database_field_name is called after removing _translations from field names in the pluck operation.
- Adds comprehensive test coverage for pluck operations with localized and aliased fields
- Fixes field name resolution in the pluck method by properly handling aliased localized fields
- Updates model definitions to support the new test scenarios
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| spec/support/models/seo.rb | Adds localized and aliased fields for embedded pluck testing |
| spec/support/models/product.rb | Adds company association to support has_many pluck tests |
| spec/support/models/passport.rb | Adds aliased localized field for embedded pluck testing |
| spec/support/models/company.rb | Adds products association for has_many pluck tests |
| spec/mongoid/criteria_spec.rb | Adds test cases for localized+aliased field pluck scenarios |
| spec/mongoid/association/referenced/has_many/enumerable_spec.rb | Adds comprehensive pluck tests including localized+aliased scenarios |
| lib/mongoid/contextual/mongo.rb | Fixes field name resolution by applying database_field_name after cleansing |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| it 'returns the translation for the current locale' do | ||
| I18n.with_locale(:ja) do | ||
| expect(plucked).to eq('京都') | ||
| end | ||
| end | ||
|
|
||
| it 'returns the translation for the current locale when unaliased' do | ||
| I18n.with_locale(:ja) do | ||
| expect(plucked_unaliased).to eq('京都') | ||
| end | ||
| end | ||
|
|
||
| it 'returns the full _translation hash' do | ||
| expect(plucked_translations).to eq({ 'en' => 'Kyoto', 'ja' => '京都' }) | ||
| end | ||
|
|
||
| it 'returns the translation for the requested locale' do | ||
| expect(plucked_translations_field).to eq('Kyoto') | ||
| end |
There was a problem hiding this comment.
The test data setup doesn't match the expected values. The setup creates 'english-text'/'deutsch-text' but the tests expect 'Kyoto'/'京都'. This will cause test failures.
|
Closing this PR in favor of #6067 (which includes the specs that were added in this PR). |
* use the specialized implementation of pluck * use a custom pluck implementation * new file * incorporate Johnny's changes from #6044 * use same submodules as master * Make changes suggested by copilot review * rubocop appeasement * fix broken refactoring
* use the specialized implementation of pluck * use a custom pluck implementation * new file * incorporate Johnny's changes from mongodb#6044 * use same submodules as master * Make changes suggested by copilot review * rubocop appeasement * fix broken refactoring
* use the specialized implementation of pluck * use a custom pluck implementation * new file * incorporate Johnny's changes from #6044 * use same submodules as master * Make changes suggested by copilot review * rubocop appeasement * fix broken refactoring
This PR adds tests for pluck cases, both of which are broken on master.
I have fixed the non-embedded case, however, I think the best fix will patch
#cleanse_localized_field_namesso that it correctly callsdatabase_field_nameafter removing_translationsfrom the name.To test: