Permalink
Browse files

Added scope option to validation_uniqueness #349 [Kent Sibilev]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@259 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Dec 22, 2004
1 parent d834b65 commit ab4c640b96f1bf1882f78847d0357e7492b621b1
Showing with 28 additions and 2 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +7 −2 activerecord/lib/active_record/validations.rb
  3. +19 −0 activerecord/test/validations_test.rb
View
@@ -1,5 +1,7 @@
*SVN*
+* Added scope option to validation_uniqueness #349 [Kent Sibilev]
+
* Added allow_nil options to validates_inclusion_of so that validation is only triggered if the attribute is not nil [what-a-day]
* Added work-around for PostgreSQL and the problem of getting fixtures to be created from id 1 on each test case.
@@ -198,20 +198,25 @@ def validates_length_of(*attr_names)
# can be named "davidhh".
#
# class Person < ActiveRecord::Base
- # validates_uniqueness_of :user_name
+ # validates_uniqueness_of :user_name, :scope => "account_id"
# end
#
# When the record is created, a check is performed to make sure that no record exist in the database with the given value for the specified
# attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself.
#
# Configuration options:
# * <tt>message</tt> - Specifies a custom error message (default is: "has already been taken")
+ # * <tt>scope</tt> - Ensures that the uniqueness is restricted to a condition of "scope = record.scope"
def validates_uniqueness_of(*attr_names)
configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken] }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
for attr_name in attr_names
- class_eval(%(validate %{errors.add("#{attr_name}", "#{configuration[:message]}") if self.class.find_first(new_record? ? ["#{attr_name} = ?", #{attr_name}] : ["#{attr_name} = ? AND id <> ?", #{attr_name}, id])}))
+ if scope = configuration[:scope]
+ class_eval(%(validate %{errors.add('#{attr_name}', '#{configuration[:message]}') if self.class.find_first(new_record? ? ['#{attr_name} = ? AND #{scope} = ?', #{attr_name}, #{scope}] : ["#{attr_name} = ? AND id <> ? AND #{scope} = ?", #{attr_name}, id, #{scope}])}))
+ else
+ class_eval(%(validate %{errors.add('#{attr_name}', '#{configuration[:message]}') if self.class.find_first(new_record? ? ['#{attr_name} = ?', #{attr_name}] : ["#{attr_name} = ? AND id <> ?", #{attr_name}, id])}))
+ end
end
end
@@ -204,6 +204,25 @@ def test_validate_uniqueness
assert t2.save, "Should now save t2 as unique"
end
+ def test_validate_uniqueness_with_scope
+ Reply.validates_uniqueness_of(:content, :scope => "parent_id")
+
+ t = Topic.create("title" => "I'm unique!")
+
+ r1 = t.replies.create "title" => "r1", "content" => "hello world"
+ assert r1.valid?, "Saving r1"
+
+ r2 = t.replies.create "title" => "r2", "content" => "hello world"
+ assert !r2.valid?, "Saving r2 first time"
+
+ r2.content = "something else"
+ assert r2.save, "Saving r2 second time"
+
+ t2 = Topic.create("title" => "I'm unique too!")
+ r3 = t2.replies.create "title" => "r3", "content" => "hello world"
+ assert r3.valid?, "Saving r3"
+ end
+
def test_validate_format
Topic.validates_format_of(:title, :content, :with => /^Validation macros rule!$/, :message => "is bad data")

0 comments on commit ab4c640

Please sign in to comment.