Ticket #8003: add_unless_conditional_to_validations.diff
| File add_unless_conditional_to_validations.diff, 15.1 kB (added by monki, 2 years ago) |
|---|
-
activerecord/test/validations_test.rb
old new 979 979 assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic).last 980 980 end 981 981 982 def test_ conditional_validation_using_method_true982 def test_if_validation_using_method_true 983 983 # When the method returns true 984 984 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true ) 985 985 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") … … 988 988 assert_equal "hoo 5", t.errors["title"] 989 989 end 990 990 991 def test_conditional_validation_using_method_false 991 def test_unless_validation_using_method_true 992 # When the method returns true 993 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => :condition_is_true ) 994 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 995 assert t.valid? 996 assert !t.errors.on(:title) 997 end 998 999 def test_if_validation_using_method_false 992 1000 # When the method returns false 993 1001 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true_but_its_not ) 994 1002 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") … … 996 1004 assert !t.errors.on(:title) 997 1005 end 998 1006 999 def test_conditional_validation_using_string_true 1007 def test_unless_validation_using_method_false 1008 # When the method returns false 1009 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => :condition_is_true_but_its_not ) 1010 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1011 assert !t.valid? 1012 assert t.errors.on(:title) 1013 assert_equal "hoo 5", t.errors["title"] 1014 end 1015 1016 def test_if_validation_using_string_true 1000 1017 # When the evaluated string returns true 1001 1018 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "a = 1; a == 1" ) 1002 1019 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") … … 1005 1022 assert_equal "hoo 5", t.errors["title"] 1006 1023 end 1007 1024 1008 def test_conditional_validation_using_string_false 1025 def test_unless_validation_using_string_true 1026 # When the evaluated string returns true 1027 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => "a = 1; a == 1" ) 1028 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1029 assert t.valid? 1030 assert !t.errors.on(:title) 1031 end 1032 1033 def test_if_validation_using_string_false 1009 1034 # When the evaluated string returns false 1010 1035 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "false") 1011 1036 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") … … 1013 1038 assert !t.errors.on(:title) 1014 1039 end 1015 1040 1016 def test_conditional_validation_using_block_true 1041 def test_unless_validation_using_string_false 1042 # When the evaluated string returns false 1043 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => "false") 1044 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1045 assert !t.valid? 1046 assert t.errors.on(:title) 1047 assert_equal "hoo 5", t.errors["title"] 1048 end 1049 1050 def test_if_validation_using_block_true 1017 1051 # When the block returns true 1018 1052 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1019 1053 :if => Proc.new { |r| r.content.size > 4 } ) … … 1023 1057 assert_equal "hoo 5", t.errors["title"] 1024 1058 end 1025 1059 1026 def test_conditional_validation_using_block_false 1060 def test_unless_validation_using_block_true 1061 # When the block returns true 1062 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1063 :unless => Proc.new { |r| r.content.size > 4 } ) 1064 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1065 assert t.valid? 1066 assert !t.errors.on(:title) 1067 end 1068 1069 def test_if_validation_using_block_false 1027 1070 # When the block returns false 1028 1071 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1029 1072 :if => Proc.new { |r| r.title != "uhohuhoh"} ) … … 1032 1075 assert !t.errors.on(:title) 1033 1076 end 1034 1077 1078 def test_unless_validation_using_block_false 1079 # When the block returns false 1080 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1081 :unless => Proc.new { |r| r.title != "uhohuhoh"} ) 1082 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1083 assert !t.valid? 1084 assert t.errors.on(:title) 1085 assert_equal "hoo 5", t.errors["title"] 1086 end 1087 1035 1088 def test_validates_associated_missing 1036 1089 Reply.validates_presence_of(:topic) 1037 1090 r = Reply.create("title" => "A reply", "content" => "with content!") -
activerecord/lib/active_record/validations.rb
old new 351 351 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 352 352 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 353 353 # method, proc or string should return or evaluate to a true or false value. 354 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 355 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 356 # method, proc or string should return or evaluate to a true or false value. 354 357 def validates_each(*attrs) 355 358 options = attrs.last.is_a?(Hash) ? attrs.pop.symbolize_keys : {} 356 359 attrs = attrs.flatten 357 360 358 361 # Declare the validation. 359 362 send(validation_method(options[:on] || :save)) do |record| 360 # Don't validate when there is an :if condition and that condition is false 361 unless options[:if] && !evaluate_condition(options[:if], record)363 # 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 364 unless (options[:if] && !evaluate_condition(options[:if], record)) || (options[:unless] && evaluate_condition(options[:unless], record)) 362 365 attrs.each do |attr| 363 366 value = record.send(attr) 364 367 next if value.nil? && options[:allow_nil] … … 390 393 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 391 394 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 392 395 # method, proc or string should return or evaluate to a true or false value. 396 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 397 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 398 # method, proc or string should return or evaluate to a true or false value. 393 399 def validates_confirmation_of(*attr_names) 394 400 configuration = { :message => ActiveRecord::Errors.default_error_messages[:confirmation], :on => :save } 395 401 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 419 425 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 420 426 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 421 427 # method, proc or string should return or evaluate to a true or false value. 428 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 429 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 430 # method, proc or string should return or evaluate to a true or false value. 422 431 def validates_acceptance_of(*attr_names) 423 432 configuration = { :message => ActiveRecord::Errors.default_error_messages[:accepted], :on => :save, :allow_nil => true, :accept => "1" } 424 433 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 448 457 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 449 458 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 450 459 # method, proc or string should return or evaluate to a true or false value. 460 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 461 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 462 # method, proc or string should return or evaluate to a true or false value. 451 463 # 452 464 # === Warning 453 465 # Validate the presence of the foreign key, not the instance variable itself. … … 468 480 # while errors.add_on_empty can 469 481 attr_names.each do |attr_name| 470 482 send(validation_method(configuration[:on])) do |record| 471 unless configuration[:if] and not evaluate_condition(configuration[:if], record)483 unless (configuration[:if] && !evaluate_condition(configuration[:if], record)) || (configuration[:unless] && evaluate_condition(configuraton[:unless], record)) 472 484 record.errors.add_on_blank(attr_name,configuration[:message]) 473 485 end 474 486 end … … 502 514 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 503 515 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 504 516 # method, proc or string should return or evaluate to a true or false value. 517 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 518 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 519 # method, proc or string should return or evaluate to a true or false value. 505 520 def validates_length_of(*attrs) 506 521 # Merge given options with defaults. 507 522 options = { … … 587 602 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 588 603 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 589 604 # method, proc or string should return or evaluate to a true or false value. 605 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 606 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 607 # method, proc or string should return or evaluate to a true or false value. 590 608 def validates_uniqueness_of(*attr_names) 591 609 configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken], :case_sensitive => true } 592 610 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 635 653 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 636 654 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 637 655 # method, proc or string should return or evaluate to a true or false value. 656 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 657 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 658 # method, proc or string should return or evaluate to a true or false value. 638 659 def validates_format_of(*attr_names) 639 660 configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save, :with => nil } 640 661 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 660 681 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 661 682 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 662 683 # method, proc or string should return or evaluate to a true or false value. 684 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 685 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 686 # method, proc or string should return or evaluate to a true or false value. 663 687 def validates_inclusion_of(*attr_names) 664 688 configuration = { :message => ActiveRecord::Errors.default_error_messages[:inclusion], :on => :save } 665 689 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 687 711 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 688 712 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 689 713 # method, proc or string should return or evaluate to a true or false value. 714 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 715 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 716 # method, proc or string should return or evaluate to a true or false value. 690 717 def validates_exclusion_of(*attr_names) 691 718 configuration = { :message => ActiveRecord::Errors.default_error_messages[:exclusion], :on => :save } 692 719 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 727 754 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 728 755 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 729 756 # method, proc or string should return or evaluate to a true or false value. 757 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 758 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 759 # method, proc or string should return or evaluate to a true or false value. 730 760 def validates_associated(*attr_names) 731 761 configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save } 732 762 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 753 783 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 754 784 # occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The 755 785 # method, proc or string should return or evaluate to a true or false value. 786 # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 787 # not occur (e.g. :if => :disallow_validation, or :if => Proc.new { |user| user.signup_step <= 2 }). The 788 # method, proc or string should return or evaluate to a true or false value. 756 789 def validates_numericality_of(*attr_names) 757 790 configuration = { :message => ActiveRecord::Errors.default_error_messages[:not_a_number], :on => :save, 758 791 :only_integer => false, :allow_nil => false }