diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2016-02-22 01:09:10 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2016-08-06 20:43:13 +0900 |
commit | 04d0e3c30b877a75cd7e82d3d41422b879a17e64 (patch) | |
tree | b8b8d5abbb4d892f2bb58b53966f6818164958b3 /activerecord/lib | |
parent | d59dc69f847bff13bb85a571c8cf1512cd067d92 (diff) | |
download | rails-04d0e3c30b877a75cd7e82d3d41422b879a17e64.tar.gz rails-04d0e3c30b877a75cd7e82d3d41422b879a17e64.tar.bz2 rails-04d0e3c30b877a75cd7e82d3d41422b879a17e64.zip |
Don't passing a nil value to `case_sensitive_comparison`
A `value` is only used for checking `value.nil?`. It is unnecessary if
immediately return when `value.nil?`.
Diffstat (limited to 'activerecord/lib')
3 files changed, 9 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 0b8bacff4e..0c99c00e78 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -434,11 +434,7 @@ module ActiveRecord end def case_sensitive_comparison(table, attribute, column, value) - if value.nil? - table[attribute].eq(value) - else - table[attribute].eq(Arel::Nodes::BindParam.new) - end + table[attribute].eq(Arel::Nodes::BindParam.new) end def case_insensitive_comparison(table, attribute, column, value) diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 5e9705e02f..85321e825a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -614,7 +614,7 @@ module ActiveRecord end def case_sensitive_comparison(table, attribute, column, value) - if !value.nil? && column.collation && !column.case_sensitive? + if column.collation && !column.case_sensitive? table[attribute].eq(Arel::Nodes::Bin.new(Arel::Nodes::BindParam.new)) else super diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 608d072e48..58273509be 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -55,6 +55,10 @@ module ActiveRecord value = value.attributes[reflection.klass.primary_key] unless value.nil? end + if value.nil? + return klass.unscoped.where!(attribute => value) + end + # the attribute may be an aliased attribute if klass.attribute_alias?(attribute) attribute = klass.attribute_alias(attribute) @@ -66,18 +70,14 @@ module ActiveRecord column = klass.columns_hash[attribute_name] cast_type = klass.type_for_attribute(attribute_name) - comparison = if !options[:case_sensitive] && !value.nil? + comparison = if !options[:case_sensitive] # will use SQL LOWER function before comparison, unless it detects a case insensitive collation klass.connection.case_insensitive_comparison(table, attribute, column, value) else klass.connection.case_sensitive_comparison(table, attribute, column, value) end - if value.nil? - klass.unscoped.where(comparison) - else - bind = Relation::QueryAttribute.new(attribute_name, value, cast_type) - klass.unscoped.where(comparison, bind) - end + bind = Relation::QueryAttribute.new(attribute_name, value, cast_type) + klass.unscoped.where!(comparison, bind) end def scope_relation(record, relation) |