aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/predicate_builder.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-12-26 15:17:10 -0700
committerSean Griffin <sean@thoughtbot.com>2014-12-26 15:19:42 -0700
commita60770d3bf3a8aeac16c110f3a7d05a6d52a86d6 (patch)
tree159a11b0566e67ce8730fbbcb17052f449434f15 /activerecord/lib/active_record/relation/predicate_builder.rb
parent35362817b16e97c9db07c5d2586a95009e747b28 (diff)
downloadrails-a60770d3bf3a8aeac16c110f3a7d05a6d52a86d6.tar.gz
rails-a60770d3bf3a8aeac16c110f3a7d05a6d52a86d6.tar.bz2
rails-a60770d3bf3a8aeac16c110f3a7d05a6d52a86d6.zip
Remove `klass` and `arel_table` as a dependency of `PredicateBuilder`
This class cares far too much about the internals of other parts of Active Record. This is an attempt to break out a meaningful object which represents the needs of the predicate builder. I'm not fully satisfied with the name, but the general concept is an object which represents a table, the associations to/from that table, and the types associated with it. Many of these exist at the `ActiveRecord::Base` class level, not as properties of the table itself, hence the need for another object. Currently it provides these by holding a reference to the class, but that will likely change in the future. This allows the predicate builder to remain wholy concerned with building predicates. /cc @mrgilman
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, 8 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb
index b0a4f27049..65c4c11e64 100644
--- a/activerecord/lib/active_record/relation/predicate_builder.rb
+++ b/activerecord/lib/active_record/relation/predicate_builder.rb
@@ -8,8 +8,9 @@ module ActiveRecord
require 'active_record/relation/predicate_builder/range_handler'
require 'active_record/relation/predicate_builder/relation_handler'
- def initialize(klass, table)
- @klass = klass
+ delegate :resolve_column_aliases, to: :table
+
+ def initialize(table)
@table = table
@handlers = []
@@ -22,16 +23,6 @@ module ActiveRecord
register_handler(AssociationQueryValue, AssociationQueryHandler.new(self))
end
- def resolve_column_aliases(hash)
- hash = hash.dup
- hash.keys.grep(Symbol) do |key|
- if klass.attribute_alias? key
- hash[klass.attribute_alias(key)] = hash.delete key
- end
- end
- hash
- end
-
def build_from_hash(attributes)
attributes = convert_dot_notation_to_hash(attributes.stringify_keys)
expand_from_hash(attributes)
@@ -43,11 +34,11 @@ module ActiveRecord
#
# For polymorphic relationships, find the foreign key and type:
# PriceEstimate.where(estimate_of: treasure)
- if klass && reflection = klass._reflect_on_association(column)
- value = AssociationQueryValue.new(reflection, value)
+ if table.associated_with?(column)
+ value = AssociationQueryValue.new(table.associated_table(column), value)
end
- build(table[column], value)
+ build(table.arel_attribute(column), value)
end
def self.references(attributes)
@@ -82,17 +73,14 @@ module ActiveRecord
protected
- attr_reader :klass, :table
+ attr_reader :table
def expand_from_hash(attributes)
return ["1=0"] if attributes.empty?
attributes.flat_map do |key, value|
if value.is_a?(Hash)
- arel_table = Arel::Table.new(key)
- association = klass._reflect_on_association(key)
- builder = self.class.new(association && association.klass, arel_table)
-
+ builder = self.class.new(table.associated_table(key))
builder.expand_from_hash(value)
else
expand(key, value)