public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Use define_callbacks helper for ActiveRecord validations.
josh (author)
Sun Apr 20 09:45:44 -0700 2008
commit  46ab7422d9ebac0d529f71a3a7c2feaf0b9d5dbd
tree    b8b28b08ced54f68137f43d796db00ffd8205b89
parent  14a40804a29a57ad05ca6bffbe1e5334089593a9
...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
23
24
25
...
3
4
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
7
8
9
0
@@ -3,23 +3,7 @@ module ActiveModel
0
     def self.included(base) # :nodoc:
0
       base.extend(ClassMethods)
0
       base.send!(:include, ActiveSupport::Callbacks)
0
-
0
- %w( validate validate_on_create validate_on_update ).each do |validation_method|
0
- base.class_eval <<-"end_eval"
0
- def self.#{validation_method}(*methods, &block)
0
- self.#{validation_method}_callback_chain | CallbackChain.build(:#{validation_method}, *methods, &block)
0
- end
0
-
0
- def self.#{validation_method}_callback_chain
0
- if chain = read_inheritable_attribute(:#{validation_method})
0
- return chain
0
- else
0
- write_inheritable_attribute(:#{validation_method}, CallbackChain.new)
0
- return #{validation_method}_callback_chain
0
- end
0
- end
0
- end_eval
0
- end
0
+ base.define_callbacks :validate, :validate_on_create, :validate_on_update
0
     end
0
 
0
     module ClassMethods
...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
 
301
302
303
...
403
404
405
406
 
407
408
409
...
437
438
439
440
 
441
442
443
...
519
520
521
522
 
523
524
525
...
596
597
598
599
 
600
601
602
...
281
282
283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284
285
286
287
...
387
388
389
 
390
391
392
393
...
421
422
423
 
424
425
426
427
...
503
504
505
 
506
507
508
509
...
580
581
582
 
583
584
585
586
0
@@ -281,23 +281,7 @@ module ActiveRecord
0
       end
0
 
0
       base.send :include, ActiveSupport::Callbacks
0
-
0
- VALIDATIONS.each do |validation_method|
0
- base.class_eval <<-"end_eval"
0
- def self.#{validation_method}(*methods, &block)
0
- self.#{validation_method}_callback_chain | CallbackChain.build(:#{validation_method}, *methods, &block)
0
- end
0
-
0
- def self.#{validation_method}_callback_chain
0
- if chain = read_inheritable_attribute(:#{validation_method})
0
- return chain
0
- else
0
- write_inheritable_attribute(:#{validation_method}, CallbackChain.new)
0
- return #{validation_method}_callback_chain
0
- end
0
- end
0
- end_eval
0
- end
0
+ base.define_callbacks *VALIDATIONS
0
     end
0
 
0
     # All of the following validations are defined in the class scope of the model that you're interested in validating.
0
@@ -403,7 +387,7 @@ module ActiveRecord
0
       # method, proc or string should return or evaluate to a true or false value.
0
       # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
0
       # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The
0
- # method, proc or string should return or evaluate to a true or false value.
0
+ # method, proc or string should return or evaluate to a true or false value.
0
       def validates_confirmation_of(*attr_names)
0
         configuration = { :message => ActiveRecord::Errors.default_error_messages[:confirmation], :on => :save }
0
         configuration.update(attr_names.extract_options!)
0
@@ -437,7 +421,7 @@ module ActiveRecord
0
       # method, proc or string should return or evaluate to a true or false value.
0
       # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
0
       # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The
0
- # method, proc or string should return or evaluate to a true or false value.
0
+ # method, proc or string should return or evaluate to a true or false value.
0
       def validates_acceptance_of(*attr_names)
0
         configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save, :allow_nil => true, :accept => "1" }
0
         configuration.update(attr_names.extract_options!)
0
@@ -519,7 +503,7 @@ module ActiveRecord
0
       # method, proc or string should return or evaluate to a true or false value.
0
       # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should
0
       # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The
0
- # method, proc or string should return or evaluate to a true or false value.
0
+ # method, proc or string should return or evaluate to a true or false value.
0
       def validates_length_of(*attrs)
0
         # Merge given options with defaults.
0
         options = {
0
@@ -596,7 +580,7 @@ module ActiveRecord
0
       # attribute (that maps to a column). When the record is updated, the same check is made but disregarding the record itself.
0
       #
0
       # Because this check is performed outside the database there is still a chance that duplicate values
0
- # will be inserted in two parallel transactions. To guarantee against this you should create a
0
+ # will be inserted in two parallel transactions. To guarantee against this you should create a
0
       # unique index on the field. See +add_index+ for more information.
0
       #
0
       # Configuration options:
...
58
59
60
61
62
63
 
 
 
64
65
66
...
839
840
841
842
 
843
844
845
846
847
848
 
849
850
851
 
852
853
854
...
1351
1352
1353
1354
1355
1356
 
 
 
1357
1358
1359
...
58
59
60
 
 
 
61
62
63
64
65
66
...
839
840
841
 
842
843
844
845
846
847
 
848
849
850
 
851
852
853
854
...
1351
1352
1353
 
 
 
1354
1355
1356
1357
1358
1359
0
@@ -58,9 +58,9 @@ class ValidationsTest < ActiveRecord::TestCase
0
   fixtures :topics, :developers, 'warehouse-things'
0
 
0
   def setup
0
- Topic.write_inheritable_attribute(:validate, nil)
0
- Topic.write_inheritable_attribute(:validate_on_create, nil)
0
- Topic.write_inheritable_attribute(:validate_on_update, nil)
0
+ Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
0
+ Topic.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
0
+ Topic.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
0
   end
0
 
0
   def test_single_field_validation
0
@@ -839,16 +839,16 @@ class ValidationsTest < ActiveRecord::TestCase
0
     reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain')
0
     assert t.valid?
0
   end
0
-
0
+
0
   def test_validates_size_of_association_using_within
0
     assert_nothing_raised { Topic.validates_size_of :replies, :within => 1..2 }
0
     t = Topic.new('title' => 'noreplies', 'content' => 'whatever')
0
     assert !t.save
0
     assert t.errors.on(:replies)
0
-
0
+
0
     reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain')
0
     assert t.valid?
0
-
0
+
0
     2.times { t.replies.build('title' => 'areply', 'content' => 'whateveragain') }
0
     assert !t.save
0
     assert t.errors.on(:replies)
0
@@ -1351,9 +1351,9 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase
0
   JUNK = ["not a number", "42 not a number", "0xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
0
 
0
   def setup
0
- Topic.write_inheritable_attribute(:validate, nil)
0
- Topic.write_inheritable_attribute(:validate_on_create, nil)
0
- Topic.write_inheritable_attribute(:validate_on_update, nil)
0
+ Topic.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
0
+ Topic.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
0
+ Topic.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
0
   end
0
 
0
   def test_default_validates_numericality_of
...
99
100
101
102
103
 
 
104
105
106
...
224
225
226
227
228
 
 
229
230
231
...
236
237
238
239
 
240
241
242
 
243
244
245
246
247
248
 
249
250
251
252
 
253
254
255
256
257
 
258
259
260
261
262
263
 
264
265
266
...
99
100
101
 
 
102
103
104
105
106
...
224
225
226
 
 
227
228
229
230
231
...
236
237
238
 
239
240
241
 
242
243
244
245
246
247
 
248
249
250
251
 
252
253
254
255
256
 
257
258
259
260
261
262
 
263
264
265
266
0
@@ -99,8 +99,8 @@ module ActiveSupport
0
       def |(chain)
0
         if chain.is_a?(CallbackChain)
0
           chain.each { |callback| self | callback }
0
- elsif chain.is_a?(Callback)
0
- if index = index(chain)
0
+ else
0
+ if (found_callback = find(chain)) && (index = index(chain))
0
             self[index] = chain
0
           else
0
             self << chain
0
@@ -224,8 +224,8 @@ module ActiveSupport
0
       end
0
     end
0
 
0
- # Runs all the callbacks defined for the given options.
0
- #
0
+ # Runs all the callbacks defined for the given options.
0
+ #
0
     # If a block is given it will be called after each callback receiving as arguments:
0
     #
0
     # * the result from the callback
0
@@ -236,31 +236,31 @@ module ActiveSupport
0
     # Example:
0
     # class Storage
0
     # include ActiveSupport::Callbacks
0
- #
0
+ #
0
     # define_callbacks :before_save, :after_save
0
     # end
0
- #
0
+ #
0
     # class ConfigStorage < Storage
0
     # before_save :pass
0
     # before_save :pass
0
     # before_save :stop
0
     # before_save :pass
0
- #
0
+ #
0
     # def pass
0
     # puts "pass"
0
     # end
0
- #
0
+ #
0
     # def stop
0
     # puts "stop"
0
     # return false
0
     # end
0
- #
0
+ #
0
     # def save
0
     # result = run_callbacks(:before_save) { |result, object| result == false }
0
     # puts "- save" if result
0
     # end
0
     # end
0
- #
0
+ #
0
     # config = ConfigStorage.new
0
     # config.save
0
     #

Comments

    No one has commented yet.