public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/JackDanger/rails.git
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
dhh (author)
Wed Dec 22 15:40:44 -0800 2004
commit  ab4c640b96f1bf1882f78847d0357e7492b621b1
tree    4b13433c1c00f73c6a51afc0dfc32f861b65a3da
parent  d834b65b540665db81cca51fdc828d0c91e314a6
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Added scope option to validation_uniqueness #349 [Kent Sibilev]
0
+
0
 * Added allow_nil options to validates_inclusion_of so that validation is only triggered if the attribute is not nil [what-a-day]
0
 
0
 * Added work-around for PostgreSQL and the problem of getting fixtures to be created from id 1 on each test case.
...
198
199
200
201
 
202
203
204
...
206
207
208
 
209
210
211
212
213
214
 
 
 
 
 
215
216
217
...
198
199
200
 
201
202
203
204
...
206
207
208
209
210
211
212
213
214
 
215
216
217
218
219
220
221
222
0
@@ -198,7 +198,7 @@ module ActiveRecord
0
       # can be named "davidhh".
0
       #
0
       # class Person < ActiveRecord::Base
0
- # validates_uniqueness_of :user_name
0
+ # validates_uniqueness_of :user_name, :scope => "account_id"
0
       # end
0
       #
0
       # 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
0
@@ -206,12 +206,17 @@ module ActiveRecord
0
       #
0
       # Configuration options:
0
       # * <tt>message</tt> - Specifies a custom error message (default is: "has already been taken")
0
+ # * <tt>scope</tt> - Ensures that the uniqueness is restricted to a condition of "scope = record.scope"
0
       def validates_uniqueness_of(*attr_names)
0
         configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken] }
0
         configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
0
 
0
         for attr_name in attr_names
0
- 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])}))
0
+ if scope = configuration[:scope]
0
+ 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}])}))
0
+ else
0
+ 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])}))
0
+ end
0
         end
0
       end
0
       
...
204
205
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
208
209
...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
0
@@ -204,6 +204,25 @@ class ValidationsTest < Test::Unit::TestCase
0
     assert t2.save, "Should now save t2 as unique"
0
   end
0
 
0
+ def test_validate_uniqueness_with_scope
0
+ Reply.validates_uniqueness_of(:content, :scope => "parent_id")
0
+
0
+ t = Topic.create("title" => "I'm unique!")
0
+
0
+ r1 = t.replies.create "title" => "r1", "content" => "hello world"
0
+ assert r1.valid?, "Saving r1"
0
+
0
+ r2 = t.replies.create "title" => "r2", "content" => "hello world"
0
+ assert !r2.valid?, "Saving r2 first time"
0
+
0
+ r2.content = "something else"
0
+ assert r2.save, "Saving r2 second time"
0
+
0
+ t2 = Topic.create("title" => "I'm unique too!")
0
+ r3 = t2.replies.create "title" => "r3", "content" => "hello world"
0
+ assert r3.valid?, "Saving r3"
0
+ end
0
+
0
   def test_validate_format
0
     Topic.validates_format_of(:title, :content, :with => /^Validation macros rule!$/, :message => "is bad data")
0
 

Comments

    No one has commented yet.