From 2da8f2154b2f4c6beac5e50225742beb3caea996 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Sun, 25 Jan 2015 16:53:46 -0700 Subject: 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. --- .../active_record/relation/where_clause_factory.rb | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 activerecord/lib/active_record/relation/where_clause_factory.rb (limited to 'activerecord/lib/active_record/relation/where_clause_factory.rb') 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 -- cgit v1.2.3