Changeset 7215
- Timestamp:
- 07/24/07 01:48:17 (1 year ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/validations.rb (modified) (13 diffs)
- trunk/activerecord/test/validations_test.rb (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r7199 r7215 1 1 *SVN* 2 3 * Added :unless clause to validations #8003 [monki]. Example: 4 5 def using_open_id? 6 !identity_url.blank? 7 end 8 9 validates_presence_of :identity_url, :if => using_open_id? 10 validates_presence_of :username, :unless => using_open_id? 11 validates_presence_of :password, :unless => using_open_id? 2 12 3 13 * Fix #count on a has_many :through association so that it recognizes the :uniq option. Closes #8801 [lifofifo] trunk/activerecord/lib/active_record/validations.rb
r7094 r7215 363 363 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 364 364 # method, proc or string should return or evaluate to a true or false value. 365 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 366 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 367 # method, proc or string should return or evaluate to a true or false value. 365 368 def validates_each(*attrs) 366 369 options = attrs.last.is_a?(Hash) ? attrs.pop.symbolize_keys : {} … … 369 372 # Declare the validation. 370 373 send(validation_method(options[:on] || :save)) do |record| 371 # Don't validate when there is an :if condition and that condition is false 372 unless options[:if] && !evaluate_condition(options[:if], record)374 # Don't validate when there is an :if condition and that condition is false or there is an :unless condition and that condition is true 375 unless (options[:if] && !evaluate_condition(options[:if], record)) || (options[:unless] && evaluate_condition(options[:unless], record)) 373 376 attrs.each do |attr| 374 377 value = record.send(attr) … … 402 405 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 403 406 # method, proc or string should return or evaluate to a true or false value. 407 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 408 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 409 # method, proc or string should return or evaluate to a true or false value. 404 410 def validates_confirmation_of(*attr_names) 405 411 configuration = { :message => ActiveRecord::Errors.default_error_messages[:confirmation], :on => :save } … … 432 438 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 433 439 # method, proc or string should return or evaluate to a true or false value. 440 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 441 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 442 # method, proc or string should return or evaluate to a true or false value. 434 443 def validates_acceptance_of(*attr_names) 435 444 configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save, :allow_nil => true, :accept => "1" } … … 460 469 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 461 470 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 471 # method, proc or string should return or evaluate to a true or false value. 472 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 473 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 462 474 # method, proc or string should return or evaluate to a true or false value. 463 475 # … … 481 493 attr_names.each do |attr_name| 482 494 send(validation_method(configuration[:on])) do |record| 483 unless configuration[:if] and not evaluate_condition(configuration[:if], record)495 unless (configuration[:if] && !evaluate_condition(configuration[:if], record)) || (configuration[:unless] && evaluate_condition(configuration[:unless], record)) 484 496 record.errors.add_on_blank(attr_name,configuration[:message]) 485 497 end … … 515 527 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 516 528 # method, proc or string should return or evaluate to a true or false value. 529 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 530 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 531 # method, proc or string should return or evaluate to a true or false value. 517 532 def validates_length_of(*attrs) 518 533 # Merge given options with defaults. … … 600 615 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 601 616 # method, proc or string should return or evaluate to a true or false value. 617 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 618 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 619 # method, proc or string should return or evaluate to a true or false value. 602 620 def validates_uniqueness_of(*attr_names) 603 621 configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken], :case_sensitive => true } … … 648 666 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 649 667 # method, proc or string should return or evaluate to a true or false value. 668 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 669 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 670 # method, proc or string should return or evaluate to a true or false value. 650 671 def validates_format_of(*attr_names) 651 672 configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save, :with => nil } … … 673 694 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 674 695 # method, proc or string should return or evaluate to a true or false value. 696 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 697 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 698 # method, proc or string should return or evaluate to a true or false value. 675 699 def validates_inclusion_of(*attr_names) 676 700 configuration = { :message => ActiveRecord::Errors.default_error_messages[:inclusion], :on => :save } … … 700 724 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 701 725 # method, proc or string should return or evaluate to a true or false value. 726 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 727 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 728 # method, proc or string should return or evaluate to a true or false value. 702 729 def validates_exclusion_of(*attr_names) 703 730 configuration = { :message => ActiveRecord::Errors.default_error_messages[:exclusion], :on => :save } … … 739 766 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 740 767 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 768 # method, proc or string should return or evaluate to a true or false value. 769 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 770 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 741 771 # method, proc or string should return or evaluate to a true or false value. 742 772 def validates_associated(*attr_names) … … 772 802 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 773 803 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 804 # method, proc or string should return or evaluate to a true or false value. 805 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 806 # not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The 774 807 # method, proc or string should return or evaluate to a true or false value. 775 808 def validates_numericality_of(*attr_names) trunk/activerecord/test/validations_test.rb
r7094 r7215 1001 1001 end 1002 1002 1003 def test_ conditional_validation_using_method_true1003 def test_if_validation_using_method_true 1004 1004 # When the method returns true 1005 1005 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true ) … … 1010 1010 end 1011 1011 1012 def test_conditional_validation_using_method_false 1012 def test_unless_validation_using_method_true 1013 # When the method returns true 1014 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => :condition_is_true ) 1015 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1016 assert t.valid? 1017 assert !t.errors.on(:title) 1018 end 1019 1020 def test_if_validation_using_method_false 1013 1021 # When the method returns false 1014 1022 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true_but_its_not ) … … 1018 1026 end 1019 1027 1020 def test_conditional_validation_using_string_true 1028 def test_unless_validation_using_method_false 1029 # When the method returns false 1030 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => :condition_is_true_but_its_not ) 1031 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1032 assert !t.valid? 1033 assert t.errors.on(:title) 1034 assert_equal "hoo 5", t.errors["title"] 1035 end 1036 1037 def test_if_validation_using_string_true 1021 1038 # When the evaluated string returns true 1022 1039 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "a = 1; a == 1" ) … … 1027 1044 end 1028 1045 1029 def test_conditional_validation_using_string_false 1046 def test_unless_validation_using_string_true 1047 # When the evaluated string returns true 1048 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => "a = 1; a == 1" ) 1049 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1050 assert t.valid? 1051 assert !t.errors.on(:title) 1052 end 1053 1054 def test_if_validation_using_string_false 1030 1055 # When the evaluated string returns false 1031 1056 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "false") … … 1035 1060 end 1036 1061 1037 def test_conditional_validation_using_block_true 1062 def test_unless_validation_using_string_false 1063 # When the evaluated string returns false 1064 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => "false") 1065 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1066 assert !t.valid? 1067 assert t.errors.on(:title) 1068 assert_equal "hoo 5", t.errors["title"] 1069 end 1070 1071 def test_if_validation_using_block_true 1038 1072 # When the block returns true 1039 1073 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", … … 1045 1079 end 1046 1080 1047 def test_conditional_validation_using_block_false 1081 def test_unless_validation_using_block_true 1082 # When the block returns true 1083 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1084 :unless => Proc.new { |r| r.content.size > 4 } ) 1085 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1086 assert t.valid? 1087 assert !t.errors.on(:title) 1088 end 1089 1090 def test_if_validation_using_block_false 1048 1091 # When the block returns false 1049 1092 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", … … 1052 1095 assert t.valid? 1053 1096 assert !t.errors.on(:title) 1097 end 1098 1099 def test_unless_validation_using_block_false 1100 # When the block returns false 1101 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1102 :unless => Proc.new { |r| r.title != "uhohuhoh"} ) 1103 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1104 assert !t.valid? 1105 assert t.errors.on(:title) 1106 assert_equal "hoo 5", t.errors["title"] 1054 1107 end 1055 1108