aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-01-01 03:14:19 +0530
committerPratik Naik <pratiknaik@gmail.com>2010-01-01 04:17:55 +0530
commit3c23b71a09c28a7be13090f83161963b56a5088e (patch)
tree20b1fed76c7a87f5c334edbd053b6d6d5f85a05d /activerecord/lib/active_record/relation/query_methods.rb
parentae7ada1fde1648a5c81b9e6adf74d515e0bbe0a5 (diff)
downloadrails-3c23b71a09c28a7be13090f83161963b56a5088e.tar.gz
rails-3c23b71a09c28a7be13090f83161963b56a5088e.tar.bz2
rails-3c23b71a09c28a7be13090f83161963b56a5088e.zip
Move predicate building to a stand alone PredicateBuilder class
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb34
1 files changed, 4 insertions, 30 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index dd097db632..432b33e174 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -104,11 +104,14 @@ module ActiveRecord
def where(*args)
return spawn if args.blank?
+ builder = PredicateBuilder.new(Arel::Sql::Engine.new(@klass))
+
conditions = if [String, Array].include?(args.first.class)
merged = @klass.send(:merge_conditions, args.size > 1 ? Array.wrap(args) : args.first)
Arel::SqlLiteral.new(merged) if merged
elsif args.first.is_a?(Hash)
- build_predicate_from_hash(args.first)
+ attributes = @klass.send(:expand_hash_conditions_for_aggregates, args.first)
+ builder.build_from_hash(attributes, table)
else
args.first
end
@@ -130,34 +133,5 @@ module ActiveRecord
}.join(',')
end
- def build_predicate_from_hash(attributes, default_table = self.table)
- attributes = @klass.send(:expand_hash_conditions_for_aggregates, attributes)
-
- predicates = attributes.map do |column, value|
- arel_table = default_table
-
- if value.is_a?(Hash)
- arel_table = Arel::Table.new(column, Arel::Sql::Engine.new(@klass))
- build_predicate_from_hash(value, arel_table)
- else
- column = column.to_s
-
- if column.include?('.')
- table_name, column = column.split('.', 2)
- arel_table = Arel::Table.new(table_name, Arel::Sql::Engine.new(@klass))
- end
-
- case value
- when Array, Range, ActiveRecord::Associations::AssociationCollection, ActiveRecord::NamedScope::Scope
- arel_table[column].in(value)
- else
- arel_table[column].eq(value)
- end
- end
- end
-
- predicates.flatten
- end
-
end
end