1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Expose queries for AssociationQueryValue and PolymorphicArrayValue

This commit is contained in:
Ryuta Kamizono 2017-04-09 16:59:26 +09:00
parent 7b78cf9998
commit cc0b566a61
2 changed files with 25 additions and 21 deletions

View file

@ -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

View file

@ -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))