diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-25 16:53:46 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-25 16:53:46 -0700 |
commit | 2da8f2154b2f4c6beac5e50225742beb3caea996 (patch) | |
tree | f9e66e1b83d3da7c5b634e8b2f8046df9a1b7f0f /activerecord | |
parent | 320600c773a16418fe37eccea2eb1082f58062f7 (diff) | |
download | rails-2da8f2154b2f4c6beac5e50225742beb3caea996.tar.gz rails-2da8f2154b2f4c6beac5e50225742beb3caea996.tar.bz2 rails-2da8f2154b2f4c6beac5e50225742beb3caea996.zip |
Move the construction of `WhereClause` objects out of `Relation`
Yes, I know, I called it a factory so I'm basically the worst person
ever who loves Java and worships the Gang of Four.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 22 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/where_clause_factory.rb | 34 |
2 files changed, 42 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 9a636bc527..863d7bb1aa 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -2,6 +2,7 @@ require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/string/filters' require 'active_model/forbidden_attributes_protection' require "active_record/relation/where_clause" +require "active_record/relation/where_clause_factory" module ActiveRecord module QueryMethods @@ -969,20 +970,9 @@ module ActiveRecord end def build_where(opts, other = []) - case opts - when String, Array - [@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))] - when Hash - opts = predicate_builder.resolve_column_aliases(opts) - opts = @klass.send(:expand_hash_conditions_for_aggregates, opts) - - attributes, bind_values = predicate_builder.create_binds(opts) - self.bind_values += bind_values - - predicate_builder.build_from_hash(attributes) - else - [opts] - end + where_clause = where_clause_factory.build(opts, other) + self.bind_values += where_clause.binds + where_clause.parts end def association_for_table(table_name) @@ -1151,5 +1141,9 @@ module ActiveRecord def new_where_clause Relation::WhereClause.empty end + + def where_clause_factory + @where_clause_factory ||= Relation::WhereClauseFactory.new(klass, predicate_builder) + end end end diff --git a/activerecord/lib/active_record/relation/where_clause_factory.rb b/activerecord/lib/active_record/relation/where_clause_factory.rb new file mode 100644 index 0000000000..0430922be3 --- /dev/null +++ b/activerecord/lib/active_record/relation/where_clause_factory.rb @@ -0,0 +1,34 @@ +module ActiveRecord + class Relation + class WhereClauseFactory + def initialize(klass, predicate_builder) + @klass = klass + @predicate_builder = predicate_builder + end + + def build(opts, other) + binds = [] + + case opts + when String, Array + parts = [klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))] + when Hash + attributes = predicate_builder.resolve_column_aliases(opts) + attributes = klass.send(:expand_hash_conditions_for_aggregates, attributes) + + attributes, binds = predicate_builder.create_binds(attributes) + + parts = predicate_builder.build_from_hash(attributes) + else + parts = [opts] + end + + WhereClause.new(parts, binds) + end + + protected + + attr_reader :klass, :predicate_builder + end + end +end |