提交 bdb2a2f1 编写于 作者: R Rick Olson

Add :case_sensitive option to validates_uniqueness_of (closes #3090) [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4207 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 7e76740d
*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
......
......@@ -503,17 +503,23 @@ def validates_length_of(*attrs)
# 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 @@ def validates_uniqueness_of(*attr_names)
end
end
# Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression
# provided.
#
......
......@@ -304,6 +304,37 @@ def test_validate_uniqueness_with_scope_array
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")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册