Skip to content

Conversation

johnnyshields
Copy link
Contributor

@johnnyshields johnnyshields commented Sep 26, 2025

This PR adds tests for pluck cases, both of which are broken on master.

  • #pluck with localized + aliased field
  • #pluck with embedded + localized + aliased field

I have fixed the non-embedded case, however, I think the best fix will patch #cleanse_localized_field_names so that it correctly calls database_field_name after removing _translations from the name.

To test:

  • MONGOID-5900
    • Mongoid::Criteria#pluck - localized + aliased field on base doc
    • Mongoid::Criteria#pluck - localized + aliased field on embeds one doc
    • Mongoid::Criteria#pluck - localized + aliased field on embeds many doc
  • MONGOID-5901
    • Mongoid::Association::Referenced::HasMany::Enumerable#pluck - localized + aliased field on base doc
    • Mongoid::Association::Referenced::HasMany::Enumerable#pluck - localized + aliased field on embeds one doc
    • Mongoid::Association::Referenced::HasMany::Enumerable#pluck - localized + aliased field on embeds many doc
    • HasAndBelongsToMany ?
    • eager loading?
    • embeds many?

@johnnyshields johnnyshields requested a review from a team as a code owner September 26, 2025 00:56
@johnnyshields johnnyshields requested review from Copilot and jamis and removed request for Copilot September 26, 2025 00:56
@Copilot Copilot AI review requested due to automatic review settings September 26, 2025 01:59
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +2491 to +2509
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
Copy link
Preview

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.

@johnnyshields johnnyshields changed the title Bugfix 🐛 The combination of localized and aliased fields is broken with pluck MONGOID-5900, MONGOID-5901 - Bugfix 🐛 The combination of localized and aliased fields is broken with pluck Sep 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant