diff options
author | sgrif <sean@thoughtbot.com> | 2013-05-17 18:23:57 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2013-07-28 13:12:20 -0600 |
commit | 92a603387c084f13a36bbf3844d89029bb73a753 (patch) | |
tree | 54a433006d02fec85e44730e00b09f84d98c91d1 /activerecord/lib/active_record/relation/predicate_builder | |
parent | 953b577f4378df06234d7e7ff409baa4d55890e8 (diff) | |
download | rails-92a603387c084f13a36bbf3844d89029bb73a753.tar.gz rails-92a603387c084f13a36bbf3844d89029bb73a753.tar.bz2 rails-92a603387c084f13a36bbf3844d89029bb73a753.zip |
Add ability to specify how a class is converted to Arel predicate
This adds the ability for rails apps or gems to have granular control
over how a domain object is converted to sql. One simple use case would
be to add support for Regexp. Another simple case would be something
like the following:
class DateRange < Struct.new(:start, :end)
def include?(date)
(start..end).cover?(date)
end
end
class DateRangePredicate
def call(attribute, range)
attribute.in(range.start..range.end)
end
end
ActiveRecord::PredicateBuilder.register_handler(DateRange,
DateRangePredicate.new)
More complex cases might include taking a currency object and converting
it from EUR to USD before performing the query.
By moving the existing handlers to this format, we were also able to
nicely refactor a rather nasty method in PredicateBuilder.
Diffstat (limited to 'activerecord/lib/active_record/relation/predicate_builder')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder/array_handler.rb | 29 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb | 13 |
2 files changed, 42 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb new file mode 100644 index 0000000000..2f6c34ac08 --- /dev/null +++ b/activerecord/lib/active_record/relation/predicate_builder/array_handler.rb @@ -0,0 +1,29 @@ +module ActiveRecord + class PredicateBuilder + class ArrayHandler # :nodoc: + def call(attribute, value) + values = value.map { |x| x.is_a?(Base) ? x.id : x } + ranges, values = values.partition { |v| v.is_a?(Range) } + + values_predicate = if values.include?(nil) + values = values.compact + + case values.length + when 0 + attribute.eq(nil) + when 1 + attribute.eq(values.first).or(attribute.eq(nil)) + else + attribute.in(values).or(attribute.eq(nil)) + end + else + attribute.in(values) + end + + array_predicates = ranges.map { |range| attribute.in(range) } + array_predicates << values_predicate + array_predicates.inject { |composite, predicate| composite.or(predicate) } + end + end + end +end diff --git a/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb new file mode 100644 index 0000000000..618fa3cdd9 --- /dev/null +++ b/activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb @@ -0,0 +1,13 @@ +module ActiveRecord + class PredicateBuilder + class RelationHandler # :nodoc: + def call(attribute, value) + if value.select_values.empty? + value = value.select(value.klass.arel_table[value.klass.primary_key]) + end + + attribute.in(value.arel.ast) + end + end + end +end |