aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG6
-rwxr-xr-xactiverecord/lib/active_record/validations.rb14
-rwxr-xr-xactiverecord/test/validations_test.rb31
3 files changed, 48 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index e2b3404533..32f0933bc3 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,11 @@
*SVN*
+* Add :case_sensitive option to validates_uniqueness_of (closes #3090) [Rick]
+
+ class Account < ActiveRecord::Base
+ validates_uniqueness_of :email, :case_sensitive => false
+ end
+
* Allow multiple association extensions with :extend option (closes #4666) [Josh Susser]
class Account < ActiveRecord::Base
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index fec2f59a1b..648b7f8f5a 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -503,17 +503,23 @@ module ActiveRecord
# Configuration options:
# * <tt>message</tt> - Specifies a custom error message (default is: "has already been taken")
# * <tt>scope</tt> - One or more columns by which to limit the scope of the uniquness constraint.
+ # * <tt>case_sensitive</tt> - Looks for an exact match. Ignored by non-text columns (true by default).
# * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
# method, proc or string should return or evaluate to a true or false value.
def validates_uniqueness_of(*attr_names)
- configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken] }
+ configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken], :case_sensitive => true }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
validates_each(attr_names,configuration) do |record, attr_name, value|
- condition_sql = "#{record.class.table_name}.#{attr_name} #{attribute_condition(value)}"
- condition_params = [value]
+ if value.nil? || (configuration[:case_sensitive] || !columns_hash[attr_name.to_s].text?)
+ condition_sql = "#{record.class.table_name}.#{attr_name} #{attribute_condition(value)}"
+ condition_params = [value]
+ else
+ condition_sql = "UPPER(#{record.class.table_name}.#{attr_name}) #{attribute_condition(value)}"
+ condition_params = [value.upcase]
+ end
if scope = configuration[:scope]
Array(scope).map do |scope_item|
scope_value = record.send(scope_item)
@@ -531,6 +537,8 @@ module ActiveRecord
end
end
+
+
# Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression
# provided.
#
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index d7ba74597c..b73058a64b 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -304,6 +304,37 @@ class ValidationsTest < Test::Unit::TestCase
assert !r3.save, "Saving r3 the third time."
end
+ def test_validate_case_insensitive_uniqueness
+ Topic.validates_uniqueness_of(:title, :parent_id, :case_sensitive => false, :allow_nil => true)
+
+ t = Topic.new("title" => "I'm unique!", :parent_id => 2)
+ assert t.save, "Should save t as unique"
+
+ t.content = "Remaining unique"
+ assert t.save, "Should still save t as unique"
+
+ t2 = Topic.new("title" => "I'm UNIQUE!", :parent_id => 1)
+ assert !t2.valid?, "Shouldn't be valid"
+ assert !t2.save, "Shouldn't save t2 as unique"
+ assert t2.errors.on(:title)
+ assert t2.errors.on(:parent_id)
+ assert_equal "has already been taken", t2.errors.on(:title)
+
+ t2.title = "I'm truly UNIQUE!"
+ assert !t2.valid?, "Shouldn't be valid"
+ assert !t2.save, "Shouldn't save t2 as unique"
+ assert_nil t2.errors.on(:title)
+ assert t2.errors.on(:parent_id)
+
+ t2.parent_id = 3
+ assert t2.save, "Should now save t2 as unique"
+
+ t2.parent_id = nil
+ t2.title = nil
+ assert t2.valid?, "should validate with nil"
+ assert t2.save, "should save with nil"
+ end
+
def test_validate_format
Topic.validates_format_of(:title, :content, :with => /^Validation\smacros \w+!$/, :message => "is bad data")