Ticket #8003: add_unless_conditional_to_validations.3.diff
| File add_unless_conditional_to_validations.3.diff, 15.2 kB (added by kampers, 1 year ago) |
|---|
-
activerecord/test/validations_test.rb
old new 988 988 assert_equal "This string contains 'single' and \"double\" quotes", r.errors.on(:topic).last 989 989 end 990 990 991 def test_ conditional_validation_using_method_true991 def test_if_validation_using_method_true 992 992 # When the method returns true 993 993 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true ) 994 994 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") … … 997 997 assert_equal "hoo 5", t.errors["title"] 998 998 end 999 999 1000 def test_conditional_validation_using_method_false 1000 def test_unless_validation_using_method_true 1001 # When the method returns true 1002 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => :condition_is_true ) 1003 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1004 assert t.valid? 1005 assert !t.errors.on(:title) 1006 end 1007 1008 def test_if_validation_using_method_false 1001 1009 # When the method returns false 1002 1010 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => :condition_is_true_but_its_not ) 1003 1011 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") … … 1005 1013 assert !t.errors.on(:title) 1006 1014 end 1007 1015 1008 def test_conditional_validation_using_string_true 1016 def test_unless_validation_using_method_false 1017 # When the method returns false 1018 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => :condition_is_true_but_its_not ) 1019 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1020 assert !t.valid? 1021 assert t.errors.on(:title) 1022 assert_equal "hoo 5", t.errors["title"] 1023 end 1024 1025 def test_if_validation_using_string_true 1009 1026 # When the evaluated string returns true 1010 1027 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "a = 1; a == 1" ) 1011 1028 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") … … 1014 1031 assert_equal "hoo 5", t.errors["title"] 1015 1032 end 1016 1033 1017 def test_conditional_validation_using_string_false 1034 def test_unless_validation_using_string_true 1035 # When the evaluated string returns true 1036 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => "a = 1; a == 1" ) 1037 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1038 assert t.valid? 1039 assert !t.errors.on(:title) 1040 end 1041 1042 def test_if_validation_using_string_false 1018 1043 # When the evaluated string returns false 1019 1044 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :if => "false") 1020 1045 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") … … 1022 1047 assert !t.errors.on(:title) 1023 1048 end 1024 1049 1025 def test_conditional_validation_using_block_true 1050 def test_unless_validation_using_string_false 1051 # When the evaluated string returns false 1052 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", :unless => "false") 1053 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1054 assert !t.valid? 1055 assert t.errors.on(:title) 1056 assert_equal "hoo 5", t.errors["title"] 1057 end 1058 1059 def test_if_validation_using_block_true 1026 1060 # When the block returns true 1027 1061 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1028 1062 :if => Proc.new { |r| r.content.size > 4 } ) … … 1032 1066 assert_equal "hoo 5", t.errors["title"] 1033 1067 end 1034 1068 1035 def test_conditional_validation_using_block_false 1069 def test_unless_validation_using_block_true 1070 # When the block returns true 1071 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1072 :unless => Proc.new { |r| r.content.size > 4 } ) 1073 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1074 assert t.valid? 1075 assert !t.errors.on(:title) 1076 end 1077 1078 def test_if_validation_using_block_false 1036 1079 # When the block returns false 1037 1080 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1038 1081 :if => Proc.new { |r| r.title != "uhohuhoh"} ) … … 1041 1084 assert !t.errors.on(:title) 1042 1085 end 1043 1086 1087 def test_unless_validation_using_block_false 1088 # When the block returns false 1089 Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d", 1090 :unless => Proc.new { |r| r.title != "uhohuhoh"} ) 1091 t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 1092 assert !t.valid? 1093 assert t.errors.on(:title) 1094 assert_equal "hoo 5", t.errors["title"] 1095 end 1096 1044 1097 def test_validates_associated_missing 1045 1098 Reply.validates_presence_of(:topic) 1046 1099 r = Reply.create("title" => "A reply", "content" => "with content!") -
activerecord/lib/active_record/validations.rb
old new 362 362 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 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 : {} 367 370 attrs = attrs.flatten 368 371 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) 375 378 next if value.nil? && options[:allow_nil] … … 401 404 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 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 } 406 412 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 431 437 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 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" } 436 445 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 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 462 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 474 # method, proc or string should return or evaluate to a true or false value. 463 475 # 464 476 # === Warning 465 477 # Validate the presence of the foreign key, not the instance variable itself. … … 480 492 # while errors.add_on_empty can 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 486 498 end … … 514 526 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 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. 519 534 options = { … … 599 614 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 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 } 604 622 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 647 665 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 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 } 652 673 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 672 693 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 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 } 677 701 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 699 723 # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 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 } 704 731 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 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 741 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 771 # method, proc or string should return or evaluate to a true or false value. 742 772 def validates_associated(*attr_names) 743 773 configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save } 744 774 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) … … 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 774 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 807 # method, proc or string should return or evaluate to a true or false value. 775 808 def validates_numericality_of(*attr_names) 776 809 configuration = { :on => :save, :only_integer => false, :allow_nil => false } 777 810 configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)