diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-12-26 15:52:20 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-12-26 15:52:57 -0700 |
commit | eac4658bb2b4f27ab0219b4986b711801583c39f (patch) | |
tree | 1c0cd0c608b0cc5f8bbf4f2d50ad56820d625b63 /activerecord | |
parent | 3179b4a868c370bf879c15c53b78f25fadec9b41 (diff) | |
download | rails-eac4658bb2b4f27ab0219b4986b711801583c39f.tar.gz rails-eac4658bb2b4f27ab0219b4986b711801583c39f.tar.bz2 rails-eac4658bb2b4f27ab0219b4986b711801583c39f.zip |
Eagerly cast range values in the predicate builder
A custom object is required for this, as you cannot build a range
object out of `Arel::Nodes::Quoted` objects. Depends on the changes
introduced in
https://github.com/rails/arel/commit/cf03bd45e39def057a2f63e42a3391b7d750dece
/cc @mrgilman
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder/range_handler.rb | 24 |
2 files changed, 25 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index fc2cc11c08..ad8dddb9a4 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -17,7 +17,7 @@ module ActiveRecord register_handler(BasicObject, BasicObjectHandler.new(self)) register_handler(Class, ClassHandler.new(self)) register_handler(Base, BaseHandler.new(self)) - register_handler(Range, RangeHandler.new) + register_handler(Range, RangeHandler.new(self)) register_handler(Relation, RelationHandler.new) register_handler(Array, ArrayHandler.new(self)) register_handler(AssociationQueryValue, AssociationQueryHandler.new(self)) diff --git a/activerecord/lib/active_record/relation/predicate_builder/range_handler.rb b/activerecord/lib/active_record/relation/predicate_builder/range_handler.rb index 47dc46bb4b..a6638738fa 100644 --- a/activerecord/lib/active_record/relation/predicate_builder/range_handler.rb +++ b/activerecord/lib/active_record/relation/predicate_builder/range_handler.rb @@ -1,9 +1,33 @@ module ActiveRecord class PredicateBuilder class RangeHandler # :nodoc: + def initialize(predicate_builder) + @predicate_builder = predicate_builder + end + def call(attribute, value) + value = QuotedRange.new( + predicate_builder.type_cast_for_database(attribute.name, value.begin), + predicate_builder.type_cast_for_database(attribute.name, value.end), + value.exclude_end?, + ) attribute.between(value) end + + protected + + attr_reader :predicate_builder + end + + class QuotedRange # :nodoc: + attr_reader :begin, :end, :exclude_end + alias_method :exclude_end?, :exclude_end + + def initialize(begin_val, end_val, exclude) + @begin = begin_val + @end = end_val + @exclude_end = exclude + end end end end |