aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/validations
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2016-07-02 10:14:53 +0900
committerRyuta Kamizono <kamipo@gmail.com>2016-08-16 19:02:02 +0900
commit3a1f6fe7b4a70bf0698b0684dd48ac712c6883b6 (patch)
tree54ddcf0345c6b58d232e675b5c44137d7e85d1e4 /activerecord/lib/active_record/validations
parentc6900ce1bb3dc2daf7cd1ecfdeeb276f2e9645b5 (diff)
downloadrails-3a1f6fe7b4a70bf0698b0684dd48ac712c6883b6.tar.gz
rails-3a1f6fe7b4a70bf0698b0684dd48ac712c6883b6.tar.bz2
rails-3a1f6fe7b4a70bf0698b0684dd48ac712c6883b6.zip
Extract `PredicateBuilder::CaseSensitiveHandler`
Currently uniqueness validator is coupled with building Arel ASTs. This commit extracts `PredicateBuilder::CaseSensitiveHandler` for decouple the building Arel ASTs.
Diffstat (limited to 'activerecord/lib/active_record/validations')
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb32
1 files changed, 1 insertions, 31 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb
index 8c4930a81d..08c4b01439 100644
--- a/activerecord/lib/active_record/validations/uniqueness.rb
+++ b/activerecord/lib/active_record/validations/uniqueness.rb
@@ -50,37 +50,7 @@ module ActiveRecord
end
def build_relation(klass, attribute, value) # :nodoc:
- if reflection = klass._reflect_on_association(attribute)
- attribute = reflection.foreign_key
- 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)
- end
-
- attribute_name = attribute.to_s
-
- table = klass.arel_table
- column = klass.columns_hash[attribute_name]
- cast_type = klass.type_for_attribute(attribute_name)
-
- 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
- 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
+ klass.unscoped.where!({ attribute => value }, options)
end
def scope_relation(record, relation)