From 04d0e3c30b877a75cd7e82d3d41422b879a17e64 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 22 Feb 2016 01:09:10 +0900 Subject: 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?`. --- .../active_record/connection_adapters/abstract_adapter.rb | 6 +----- .../connection_adapters/abstract_mysql_adapter.rb | 2 +- activerecord/lib/active_record/validations/uniqueness.rb | 14 +++++++------- 3 files changed, 9 insertions(+), 13 deletions(-) (limited to 'activerecord/lib/active_record') 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) -- cgit v1.2.3 From 320996a7e5dbf0014b37cfc4d2259c88a4092744 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sat, 6 Aug 2016 21:00:46 +0900 Subject: Revert passing arel node with splat binds for `where` Passing arel node with splat binds for `where` was introduced at #22877 for uniqueness validator supports prepared statement. But I'd not like to introduce the following usage: ```ruby Foo.where(arel, *binds) ``` I'd like to revert this internal usage. --- activerecord/lib/active_record/relation/where_clause_factory.rb | 1 - activerecord/lib/active_record/validations/uniqueness.rb | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/where_clause_factory.rb b/activerecord/lib/active_record/relation/where_clause_factory.rb index dbf172a577..a81ff98e49 100644 --- a/activerecord/lib/active_record/relation/where_clause_factory.rb +++ b/activerecord/lib/active_record/relation/where_clause_factory.rb @@ -22,7 +22,6 @@ module ActiveRecord parts = predicate_builder.build_from_hash(attributes) when Arel::Nodes::Node parts = [opts] - binds = other else raise ArgumentError, "Unsupported argument type: #{opts} (#{opts.class})" end diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 58273509be..8c4930a81d 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -76,8 +76,11 @@ module ActiveRecord else klass.connection.case_sensitive_comparison(table, attribute, column, value) end - bind = Relation::QueryAttribute.new(attribute_name, value, cast_type) - klass.unscoped.where!(comparison, bind) + klass.unscoped.tap do |scope| + parts = [comparison] + binds = [Relation::QueryAttribute.new(attribute_name, value, cast_type)] + scope.where_clause += Relation::WhereClause.new(parts, binds) + end end def scope_relation(record, relation) -- cgit v1.2.3