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
cf03bd45e3

/cc @mrgilman
This commit is contained in:
Sean Griffin 2014-12-26 15:52:20 -07:00
parent 3179b4a868
commit eac4658bb2
2 changed files with 25 additions and 1 deletions

View File

@ -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))

View File

@ -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