aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-01-14 13:22:54 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-01-18 14:56:43 +0900
commit5b6daff5b6d5439e07c058718069f54b34970f93 (patch)
tree2056e890057bb59eeef8356eb41741037aadcfaa /activerecord
parenteb63faaa1af7ef28ae1a716d068acc447e28c174 (diff)
downloadrails-5b6daff5b6d5439e07c058718069f54b34970f93.tar.gz
rails-5b6daff5b6d5439e07c058718069f54b34970f93.tar.bz2
rails-5b6daff5b6d5439e07c058718069f54b34970f93.zip
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 ```
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/query_attribute.rb19
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb2
-rw-r--r--activerecord/lib/arel/nodes/bind_param.rb4
-rw-r--r--activerecord/lib/arel/visitors/to_sql.rb8
4 files changed, 19 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/relation/query_attribute.rb b/activerecord/lib/active_record/relation/query_attribute.rb
index b45326bdda..5e0b4ac160 100644
--- a/activerecord/lib/active_record/relation/query_attribute.rb
+++ b/activerecord/lib/active_record/relation/query_attribute.rb
@@ -20,18 +20,23 @@ module ActiveRecord
def nil?
!value_before_type_cast.is_a?(StatementCache::Substitute) &&
(value_before_type_cast.nil? || value_for_database.nil?)
+ rescue ::RangeError
end
- def boundable?
- return @_boundable if defined?(@_boundable)
- nil?
- @_boundable = true
+ def infinite?
+ infinity?(value_before_type_cast) || infinity?(value_for_database)
rescue ::RangeError
- @_boundable = false
end
- def infinite?
- infinity?(value_before_type_cast) || boundable? && infinity?(value_for_database)
+ def unboundable?
+ if defined?(@_unboundable)
+ @_unboundable
+ else
+ value_for_database
+ @_unboundable = nil
+ end
+ rescue ::RangeError
+ @_unboundable = type.cast(value_before_type_cast) <=> 0
end
private
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb
index 111b6c9a64..fb745af125 100644
--- a/activerecord/lib/active_record/validations/uniqueness.rb
+++ b/activerecord/lib/active_record/validations/uniqueness.rb
@@ -58,7 +58,7 @@ module ActiveRecord
def build_relation(klass, attribute, value)
relation = klass.unscoped
comparison = relation.bind_attribute(attribute, value) do |attr, bind|
- return relation.none! unless bind.boundable?
+ return relation.none! if bind.unboundable?
if bind.nil?
attr.eq(bind)
diff --git a/activerecord/lib/arel/nodes/bind_param.rb b/activerecord/lib/arel/nodes/bind_param.rb
index f145e44ae3..344e46479f 100644
--- a/activerecord/lib/arel/nodes/bind_param.rb
+++ b/activerecord/lib/arel/nodes/bind_param.rb
@@ -28,8 +28,8 @@ module Arel # :nodoc: all
value.respond_to?(:infinite?) && value.infinite?
end
- def boundable?
- !value.respond_to?(:boundable?) || value.boundable?
+ def unboundable?
+ value.respond_to?(:unboundable?) && value.unboundable?
end
end
end
diff --git a/activerecord/lib/arel/visitors/to_sql.rb b/activerecord/lib/arel/visitors/to_sql.rb
index b5a960ce68..c08403eea9 100644
--- a/activerecord/lib/arel/visitors/to_sql.rb
+++ b/activerecord/lib/arel/visitors/to_sql.rb
@@ -576,7 +576,7 @@ module Arel # :nodoc: all
def visit_Arel_Nodes_In(o, collector)
if Array === o.right && !o.right.empty?
- o.right.keep_if { |value| boundable?(value) }
+ o.right.delete_if { |value| unboundable?(value) }
end
if Array === o.right && o.right.empty?
@@ -590,7 +590,7 @@ module Arel # :nodoc: all
def visit_Arel_Nodes_NotIn(o, collector)
if Array === o.right && !o.right.empty?
- o.right.keep_if { |value| boundable?(value) }
+ o.right.delete_if { |value| unboundable?(value) }
end
if Array === o.right && o.right.empty?
@@ -812,8 +812,8 @@ module Arel # :nodoc: all
}
end
- def boundable?(value)
- !value.respond_to?(:boundable?) || value.boundable?
+ def unboundable?(value)
+ value.respond_to?(:unboundable?) && value.unboundable?
end
def has_join_sources?(o)