1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use unboundable? rather than boundable?

The `unboundable?` behaves like the `infinite?`.

```ruby
inf = Topic.predicate_builder.build_bind_attribute(:id, Float::INFINITY)
inf.infinite?    # => 1

oob = Topic.predicate_builder.build_bind_attribute(:id, 9999999999999999999999999999999)
oob.unboundable? # => 1

inf = Topic.predicate_builder.build_bind_attribute(:id, -Float::INFINITY)
inf.infinite?    # => -1

oob = Topic.predicate_builder.build_bind_attribute(:id, -9999999999999999999999999999999)
oob.unboundable? # => -1
```
This commit is contained in:
Ryuta Kamizono 2019-01-14 13:22:54 +09:00
parent eb63faaa1a
commit 5b6daff5b6
4 changed files with 20 additions and 15 deletions

View file

@ -20,18 +20,23 @@ module ActiveRecord
def nil? def nil?
!value_before_type_cast.is_a?(StatementCache::Substitute) && !value_before_type_cast.is_a?(StatementCache::Substitute) &&
(value_before_type_cast.nil? || value_for_database.nil?) (value_before_type_cast.nil? || value_for_database.nil?)
end
def boundable?
return @_boundable if defined?(@_boundable)
nil?
@_boundable = true
rescue ::RangeError rescue ::RangeError
@_boundable = false
end end
def infinite? def infinite?
infinity?(value_before_type_cast) || boundable? && infinity?(value_for_database) infinity?(value_before_type_cast) || infinity?(value_for_database)
rescue ::RangeError
end
def unboundable?
if defined?(@_unboundable)
@_unboundable
else
value_for_database
@_unboundable = nil
end
rescue ::RangeError
@_unboundable = type.cast(value_before_type_cast) <=> 0
end end
private private

View file

@ -58,7 +58,7 @@ module ActiveRecord
def build_relation(klass, attribute, value) def build_relation(klass, attribute, value)
relation = klass.unscoped relation = klass.unscoped
comparison = relation.bind_attribute(attribute, value) do |attr, bind| comparison = relation.bind_attribute(attribute, value) do |attr, bind|
return relation.none! unless bind.boundable? return relation.none! if bind.unboundable?
if bind.nil? if bind.nil?
attr.eq(bind) attr.eq(bind)

View file

@ -28,8 +28,8 @@ module Arel # :nodoc: all
value.respond_to?(:infinite?) && value.infinite? value.respond_to?(:infinite?) && value.infinite?
end end
def boundable? def unboundable?
!value.respond_to?(:boundable?) || value.boundable? value.respond_to?(:unboundable?) && value.unboundable?
end end
end end
end end

View file

@ -576,7 +576,7 @@ module Arel # :nodoc: all
def visit_Arel_Nodes_In(o, collector) def visit_Arel_Nodes_In(o, collector)
if Array === o.right && !o.right.empty? if Array === o.right && !o.right.empty?
o.right.keep_if { |value| boundable?(value) } o.right.delete_if { |value| unboundable?(value) }
end end
if Array === o.right && o.right.empty? if Array === o.right && o.right.empty?
@ -590,7 +590,7 @@ module Arel # :nodoc: all
def visit_Arel_Nodes_NotIn(o, collector) def visit_Arel_Nodes_NotIn(o, collector)
if Array === o.right && !o.right.empty? if Array === o.right && !o.right.empty?
o.right.keep_if { |value| boundable?(value) } o.right.delete_if { |value| unboundable?(value) }
end end
if Array === o.right && o.right.empty? if Array === o.right && o.right.empty?
@ -812,8 +812,8 @@ module Arel # :nodoc: all
} }
end end
def boundable?(value) def unboundable?(value)
!value.respond_to?(:boundable?) || value.boundable? value.respond_to?(:unboundable?) && value.unboundable?
end end
def has_join_sources?(o) def has_join_sources?(o)