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
|
end
|
||||||
|
|
||||||
def call(attribute, value)
|
def call(attribute, value)
|
||||||
table = value.associated_table
|
predicate_builder.build_from_hash(value.queries)
|
||||||
queries = { table.association_foreign_key.to_s => value.ids }
|
|
||||||
predicate_builder.build_from_hash(queries)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO Change this to private once we've dropped Ruby 2.2 support.
|
# TODO Change this to private once we've dropped Ruby 2.2 support.
|
||||||
|
@ -40,10 +38,15 @@ module ActiveRecord
|
||||||
@value = value
|
@value = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def queries
|
||||||
|
{ associated_table.association_foreign_key.to_s => ids }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
def ids
|
def ids
|
||||||
case value
|
case value
|
||||||
when Relation
|
when Relation
|
||||||
value.select(primary_key)
|
value.select_values.empty? ? value.select(primary_key) : value
|
||||||
when Array
|
when Array
|
||||||
value.map { |v| convert_to_id(v) }
|
value.map { |v| convert_to_id(v) }
|
||||||
else
|
else
|
||||||
|
@ -51,8 +54,6 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def primary_key
|
def primary_key
|
||||||
associated_table.association_primary_key
|
associated_table.association_primary_key
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,12 +6,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(attribute, value)
|
def call(attribute, value)
|
||||||
table = value.associated_table
|
predicates = value.queries.map { |query| predicate_builder.build_from_hash(query) }
|
||||||
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) }
|
|
||||||
|
|
||||||
if predicates.size > 1
|
if predicates.size > 1
|
||||||
type_and_ids_predicates = predicates.map { |type_predicate, id_predicate| Arel::Nodes::Grouping.new(type_predicate.and(id_predicate)) }
|
type_and_ids_predicates = predicates.map { |type_predicate, id_predicate| Arel::Nodes::Grouping.new(type_predicate.and(id_predicate)) }
|
||||||
|
@ -36,13 +31,21 @@ module ActiveRecord
|
||||||
@values = values
|
@values = values
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
def type_to_ids_mapping
|
||||||
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
|
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) }
|
values.each_with_object(default_hash) { |value, hash| hash[base_class(value).name] << convert_to_id(value) }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def primary_key(value)
|
def primary_key(value)
|
||||||
associated_table.association_primary_key(base_class(value))
|
associated_table.association_primary_key(base_class(value))
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue