-
Notifications
You must be signed in to change notification settings - Fork 1.4k
MONGOID-5900 Fix #pluck on associations #6067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
baffaa5
use the specialized implementation of pluck
jamis 9f64d2f
use a custom pluck implementation
jamis 0227c33
Merge branch 'master' into 5901-pluck-localized-aliased
jamis 77b9bd3
new file
jamis 5a75123
incorporate Johnny's changes from #6044
jamis f2b1852
use same submodules as master
jamis 8b3b40f
Merge branch 'master' into 5901-pluck-localized-aliased
jamis f978d05
Make changes suggested by copilot review
jamis 9e7998f
rubocop appeasement
jamis 2bab956
fix broken refactoring
jamis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Mongoid | ||
| # Provides shared behavior for any document with "pluck" functionality. | ||
| # | ||
| # @api private | ||
| module Pluckable | ||
| extend ActiveSupport::Concern | ||
|
|
||
| private | ||
|
|
||
| # Prepares the field names for plucking by normalizing them to their | ||
| # database field names. Also prepares a projection hash if requested. | ||
| def prepare_pluck(field_names, document_class: klass, prepare_projection: false) | ||
| normalized_field_names = [] | ||
| projection = {} | ||
|
|
||
| field_names.each do |f| | ||
| db_fn = document_class.database_field_name(f) | ||
| normalized_field_names.push(db_fn) | ||
|
|
||
| if prepare_projection | ||
| cleaned_name = document_class.cleanse_localized_field_names(f) | ||
| canonical_name = document_class.database_field_name(cleaned_name) | ||
| projection[canonical_name] = true | ||
| end | ||
| end | ||
|
|
||
| { field_names: normalized_field_names, projection: projection } | ||
| end | ||
|
|
||
| # Plucks the given field names from the given documents. | ||
| def pluck_from_documents(documents, field_names, document_class: klass) | ||
| documents.reduce([]) do |plucked, doc| | ||
| values = field_names.map { |name| extract_value(doc, name.to_s, document_class) } | ||
| plucked << (values.size == 1 ? values.first : values) | ||
| end | ||
| end | ||
|
|
||
| # Fetch the element from the given hash and demongoize it using the | ||
| # given field. If the obj is an array, map over it and call this method | ||
| # on all of its elements. | ||
| # | ||
| # @param [ Hash | Array<Hash> ] obj The hash or array of hashes to fetch from. | ||
| # @param [ String ] key The key to fetch from the hash. | ||
| # @param [ Field ] field The field to use for demongoization. | ||
| # | ||
| # @return [ Object ] The demongoized value. | ||
| def fetch_and_demongoize(obj, key, field) | ||
| if obj.is_a?(Array) | ||
| obj.map { |doc| fetch_and_demongoize(doc, key, field) } | ||
| else | ||
| value = obj.try(:fetch, key, nil) | ||
| field ? field.demongoize(value) : value.class.demongoize(value) | ||
| end | ||
| end | ||
|
|
||
| # Extracts the value for the given field name from the given attribute | ||
| # hash. | ||
| # | ||
| # @param [ Hash ] attrs The attributes hash. | ||
| # @param [ String ] field_name The name of the field to extract. | ||
| # | ||
| # @param [ Object ] The value for the given field name | ||
jamis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def extract_value(attrs, field_name, document_class) | ||
| i = 1 | ||
| num_meths = field_name.count('.') + 1 | ||
| curr = attrs.dup | ||
|
|
||
| document_class.traverse_association_tree(field_name) do |meth, obj, is_field| | ||
| field = obj if is_field | ||
|
|
||
| # use the correct document class to check for localized fields on | ||
| # embedded documents. | ||
| document_class = obj.klass if obj.respond_to?(:klass) | ||
|
|
||
| is_translation = false | ||
| # If no association or field was found, check if the meth is an | ||
| # _translations field. | ||
| if obj.nil? & tr = meth.match(/(.*)_translations\z/)&.captures&.first | ||
jamis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| is_translation = true | ||
| meth = document_class.database_field_name(tr) | ||
| end | ||
|
|
||
| # 1. If curr is an array fetch from all elements in the array. | ||
| # 2. If the field is localized, and is not an _translations field | ||
| # (_translations fields don't show up in the fields hash). | ||
| # - If this is the end of the methods, return the translation for | ||
| # the current locale. | ||
| # - Otherwise, return the whole translations hash so the next method | ||
| # can select the language it wants. | ||
| # 3. If the meth is an _translations field, do not demongoize the | ||
| # value so the full hash is returned. | ||
| # 4. Otherwise, fetch and demongoize the value for the key meth. | ||
| curr = if curr.is_a? Array | ||
| res = fetch_and_demongoize(curr, meth, field) | ||
| res.empty? ? nil : res | ||
| elsif !is_translation && field&.localized? | ||
| if i < num_meths | ||
| curr.try(:fetch, meth, nil) | ||
| else | ||
| fetch_and_demongoize(curr, meth, field) | ||
| end | ||
| elsif is_translation | ||
| curr.try(:fetch, meth, nil) | ||
| else | ||
| fetch_and_demongoize(curr, meth, field) | ||
| end | ||
|
|
||
| i += 1 | ||
| end | ||
| curr | ||
| end | ||
|
|
||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.