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:
parent
7b78cf9998
commit
cc0b566a61
2 changed files with 25 additions and 21 deletions
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue