diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-04-09 16:59:26 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-04-09 16:59:26 +0900 |
commit | cc0b566a614f6172bd579f9f3ed5648e22ecd633 (patch) | |
tree | 5e9bdc5880d3ef9e3d7be74d874a15d3dc7b1f95 /activerecord/lib/active_record | |
parent | 7b78cf99986cf06c5fbcabf5586bdd6ed322edf2 (diff) | |
download | rails-cc0b566a614f6172bd579f9f3ed5648e22ecd633.tar.gz rails-cc0b566a614f6172bd579f9f3ed5648e22ecd633.tar.bz2 rails-cc0b566a614f6172bd579f9f3ed5648e22ecd633.zip |
Expose `queries` for `AssociationQueryValue` and `PolymorphicArrayValue`
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb | 25 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb | 21 |
2 files changed, 25 insertions, 21 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb index 82da4689f0..9141d9c537 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb @@ -20,9 +20,7 @@ module ActiveRecord end def call(attribute, value) - table = value.associated_table - queries = { table.association_foreign_key.to_s => value.ids } - predicate_builder.build_from_hash(queries) + predicate_builder.build_from_hash(value.queries) end # TODO Change this to private once we've dropped Ruby 2.2 support. @@ -40,18 +38,21 @@ module ActiveRecord @value = value end - def ids - case value - when Relation - value.select(primary_key) - when Array - value.map { |v| convert_to_id(v) } - else - convert_to_id(value) - end + def queries + { associated_table.association_foreign_key.to_s => ids } end private + def ids + case value + when Relation + value.select_values.empty? ? value.select(primary_key) : value + when Array + value.map { |v| convert_to_id(v) } + else + convert_to_id(value) + end + end def primary_key associated_table.association_primary_key diff --git a/activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb index 21cc324e5e..c2f136256b 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb @@ -6,12 +6,7 @@ module ActiveRecord end def call(attribute, value) - table = value.associated_table - queries = value.type_to_ids_mapping.map do |type, ids| - { table.association_foreign_type.to_s => type, table.association_foreign_key.to_s => ids } - end - - predicates = queries.map { |query| predicate_builder.build_from_hash(query) } + predicates = value.queries.map { |query| predicate_builder.build_from_hash(query) } if predicates.size > 1 type_and_ids_predicates = predicates.map { |type_predicate, id_predicate| Arel::Nodes::Grouping.new(type_predicate.and(id_predicate)) } @@ -36,12 +31,20 @@ module ActiveRecord @values = values end - def type_to_ids_mapping - default_hash = Hash.new { |hsh, key| hsh[key] = [] } - values.each_with_object(default_hash) { |value, hash| hash[base_class(value).name] << convert_to_id(value) } + def queries + type_to_ids_mapping.map do |type, ids| + { + associated_table.association_foreign_type.to_s => type, + associated_table.association_foreign_key.to_s => ids + } + end end private + def type_to_ids_mapping + default_hash = Hash.new { |hsh, key| hsh[key] = [] } + values.each_with_object(default_hash) { |value, hash| hash[base_class(value).name] << convert_to_id(value) } + end def primary_key(value) associated_table.association_primary_key(base_class(value)) |