diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2016-08-31 12:38:37 -0400 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2016-08-31 12:38:37 -0400 |
commit | 84efde740dc357de3ace08b2f2fd7a4a67dbd9bd (patch) | |
tree | c858f06849f59885cb736027d49f954de2ca59b4 /activerecord/lib/active_record/validations | |
parent | 7ba3a48df5bfdc5e98506bb829f937e03b55a5b3 (diff) | |
download | rails-84efde740dc357de3ace08b2f2fd7a4a67dbd9bd.tar.gz rails-84efde740dc357de3ace08b2f2fd7a4a67dbd9bd.tar.bz2 rails-84efde740dc357de3ace08b2f2fd7a4a67dbd9bd.zip |
Revert "Extract `PredicateBuilder::CaseSensitiveHandler`"
This reverts commit 3a1f6fe7b4a70bf0698b0684dd48ac712c6883b6.
This commit takes the code in a direction that I am looking to avoid.
The predicate builder should be purely concerned with AST construction
as it matters to methods like `where`. Things like case sensitivity
should continue to be handled elsewhere.
Diffstat (limited to 'activerecord/lib/active_record/validations')
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 08c4b01439..8c4930a81d 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -50,7 +50,37 @@ module ActiveRecord end def build_relation(klass, attribute, value) # :nodoc: - klass.unscoped.where!({ attribute => value }, options) + 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 end def scope_relation(record, relation) |