aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-12-28 11:42:29 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-12-28 11:42:29 -0800
commit0b61e3f86a2f723582e3a9295d67b541f2bb2013 (patch)
tree3358bf5770d88a67f3b6cff9e4c1692f9045dae1
parent2fb5d0a3fc889c8ef322be3e57f4cf7b1016bca8 (diff)
parent612d671e991a27dbb698afbbc6b8401367186c01 (diff)
downloadrails-0b61e3f86a2f723582e3a9295d67b541f2bb2013.tar.gz
rails-0b61e3f86a2f723582e3a9295d67b541f2bb2013.tar.bz2
rails-0b61e3f86a2f723582e3a9295d67b541f2bb2013.zip
Merge pull request #4222 from amatsuda/ar_predicate_builder_refactor
refactor AR::PredicateBuilder.build_from_hash
-rw-r--r--activerecord/lib/active_record/relation/predicate_builder.rb72
1 files changed, 37 insertions, 35 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb
index 95b6efb402..88081edae2 100644
--- a/activerecord/lib/active_record/relation/predicate_builder.rb
+++ b/activerecord/lib/active_record/relation/predicate_builder.rb
@@ -15,47 +15,49 @@ module ActiveRecord
table = Arel::Table.new(table_name, engine)
end
- attribute = table[column.to_sym]
-
- case value
- when ActiveRecord::Relation
- value = value.select(value.klass.arel_table[value.klass.primary_key]) if value.select_values.empty?
- attribute.in(value.arel.ast)
- when Array, ActiveRecord::Associations::CollectionProxy
- values = value.to_a.map {|x| x.is_a?(ActiveRecord::Model) ? x.id : x}
- ranges, values = values.partition {|v| v.is_a?(Range) || v.is_a?(Arel::Relation)}
-
- array_predicates = ranges.map {|range| attribute.in(range)}
+ build(table[column.to_sym], value)
+ end
+ end
+ predicates.flatten
+ end
- if values.include?(nil)
- values = values.compact
- case values.length
- when 0
- array_predicates << attribute.eq(nil)
- when 1
- array_predicates << attribute.eq(values.first).or(attribute.eq(nil))
- else
- array_predicates << attribute.in(values).or(attribute.eq(nil))
- end
+ private
+ def self.build(attribute, value)
+ case value
+ when ActiveRecord::Relation
+ value = value.select(value.klass.arel_table[value.klass.primary_key]) if value.select_values.empty?
+ attribute.in(value.arel.ast)
+ when Array, ActiveRecord::Associations::CollectionProxy
+ values = value.to_a.map {|x| x.is_a?(ActiveRecord::Model) ? x.id : x}
+ ranges, values = values.partition {|v| v.is_a?(Range) || v.is_a?(Arel::Relation)}
+
+ array_predicates = ranges.map {|range| attribute.in(range)}
+
+ if values.include?(nil)
+ values = values.compact
+ case values.length
+ when 0
+ array_predicates << attribute.eq(nil)
+ when 1
+ array_predicates << attribute.eq(values.first).or(attribute.eq(nil))
else
- array_predicates << attribute.in(values)
+ array_predicates << attribute.in(values).or(attribute.eq(nil))
end
-
- array_predicates.inject {|composite, predicate| composite.or(predicate)}
- when Range, Arel::Relation
- attribute.in(value)
- when ActiveRecord::Model
- attribute.eq(value.id)
- when Class
- # FIXME: I think we need to deprecate this behavior
- attribute.eq(value.name)
else
- attribute.eq(value)
+ array_predicates << attribute.in(values)
end
+
+ array_predicates.inject {|composite, predicate| composite.or(predicate)}
+ when Range, Arel::Relation
+ attribute.in(value)
+ when ActiveRecord::Model
+ attribute.eq(value.id)
+ when Class
+ # FIXME: I think we need to deprecate this behavior
+ attribute.eq(value.name)
+ else
+ attribute.eq(value)
end
end
-
- predicates.flatten
- end
end
end