mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move predicate building to a stand alone PredicateBuilder class
This commit is contained in:
parent
ae7ada1fde
commit
3c23b71a09
3 changed files with 41 additions and 30 deletions
|
@ -54,6 +54,7 @@ module ActiveRecord
|
|||
autoload :QueryMethods
|
||||
autoload :FinderMethods
|
||||
autoload :CalculationMethods
|
||||
autoload :PredicateBuilder
|
||||
end
|
||||
|
||||
autoload :Base
|
||||
|
|
36
activerecord/lib/active_record/relation/predicate_builder.rb
Normal file
36
activerecord/lib/active_record/relation/predicate_builder.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
module ActiveRecord
|
||||
class PredicateBuilder
|
||||
|
||||
def initialize(engine)
|
||||
@engine = engine
|
||||
end
|
||||
|
||||
def build_from_hash(attributes, default_table)
|
||||
predicates = attributes.map do |column, value|
|
||||
arel_table = default_table
|
||||
|
||||
if value.is_a?(Hash)
|
||||
arel_table = Arel::Table.new(column, @engine)
|
||||
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, @engine)
|
||||
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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue