aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/predicate_builder.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-04-09 17:25:43 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-04-09 17:25:43 +0900
commitbaf6072e4550cf2cf5e52240d0192a56dbe8e949 (patch)
tree3951c3c1f18b3d4375af448cb29c73a31c29201b /activerecord/lib/active_record/relation/predicate_builder.rb
parentcc0b566a614f6172bd579f9f3ed5648e22ecd633 (diff)
downloadrails-baf6072e4550cf2cf5e52240d0192a56dbe8e949.tar.gz
rails-baf6072e4550cf2cf5e52240d0192a56dbe8e949.tar.bz2
rails-baf6072e4550cf2cf5e52240d0192a56dbe8e949.zip
Convert `AssociationQueryValue` to PORO queries
Diffstat (limited to 'activerecord/lib/active_record/relation/predicate_builder.rb')
-rw-r--r--activerecord/lib/active_record/relation/predicate_builder.rb28
1 files changed, 23 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb
index fca914aedd..58d30b801c 100644
--- a/activerecord/lib/active_record/relation/predicate_builder.rb
+++ b/activerecord/lib/active_record/relation/predicate_builder.rb
@@ -20,7 +20,6 @@ module ActiveRecord
register_handler(RangeHandler::RangeWithBinds, RangeHandler.new)
register_handler(Relation, RelationHandler.new)
register_handler(Array, ArrayHandler.new(self))
- register_handler(AssociationQueryValue, AssociationQueryHandler.new(self))
register_handler(PolymorphicArrayValue, PolymorphicArrayHandler.new(self))
end
@@ -83,11 +82,10 @@ module ActiveRecord
end
def create_binds_for_hash(attributes)
- result = attributes.dup
+ result = {}
binds = []
attributes.each do |column_name, value|
- binds.concat(value.bound_attributes) if value.is_a?(Relation)
case
when value.is_a?(Hash) && !table.has_column?(column_name)
attrs, bvs = associated_predicate_builder(column_name).create_binds_for_hash(value)
@@ -99,7 +97,24 @@ module ActiveRecord
#
# For polymorphic relationships, find the foreign key and type:
# PriceEstimate.where(estimate_of: treasure)
- result[column_name] = AssociationQueryHandler.value_for(table, column_name, value)
+ associated_table = table.associated_table(column_name)
+ if associated_table.polymorphic_association?
+ case value.is_a?(Array) ? value.first : value
+ when Base, Relation
+ binds.concat(value.bound_attributes) if value.is_a?(Relation)
+ value = [value] unless value.is_a?(Array)
+ klass = PolymorphicArrayValue
+ end
+ end
+
+ if klass
+ result[column_name] = klass.new(associated_table, value)
+ else
+ queries = AssociationQueryValue.new(associated_table, value).queries
+ attrs, bvs = create_binds_for_hash(queries)
+ result.merge!(attrs)
+ binds.concat(bvs)
+ end
when value.is_a?(Range) && !table.type(column_name).respond_to?(:subtype)
first = value.begin
last = value.end
@@ -115,9 +130,12 @@ module ActiveRecord
result[column_name] = RangeHandler::RangeWithBinds.new(first, last, value.exclude_end?)
else
if can_be_bound?(column_name, value)
- result[column_name] = Arel::Nodes::BindParam.new
binds << build_bind_param(column_name, value)
+ value = Arel::Nodes::BindParam.new
+ elsif value.is_a?(Relation)
+ binds.concat(value.bound_attributes)
end
+ result[column_name] = value
end
end