Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9574: validates_length_of_in.diff

File validates_length_of_in.diff, 9.8 kB (added by lawrence, 1 year ago)

[PATCH] validates_length_of_in.diff

  • activerecord/test/validations_test.rb

    old new  
    568568    assert t.valid? 
    569569  end 
    570570 
     571  def test_validates_length_of_using_in 
     572    Topic.validates_length_of(:title, :content, :in => 3..5) 
     573 
     574    t = Topic.new("title" => "a!", "content" => "I'm ooooooooh so very long") 
     575    assert !t.valid? 
     576    assert_equal "is too short (minimum is 3 characters)", t.errors.on(:title) 
     577    assert_equal "is too long (maximum is 5 characters)", t.errors.on(:content) 
     578 
     579    t.title = nil 
     580    t.content = nil 
     581    assert !t.valid? 
     582    assert_equal "is too short (minimum is 3 characters)", t.errors.on(:title) 
     583    assert_equal "is too short (minimum is 3 characters)", t.errors.on(:content) 
     584 
     585    t.title = "abe" 
     586    t.content  = "mad" 
     587    assert t.valid? 
     588  end 
     589 
    571590  def test_optionally_validates_length_of_using_within 
    572591    Topic.validates_length_of :title, :content, :within => 3..5, :allow_nil => true 
    573592 
     
    684703    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :maximum=>9) } 
    685704    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :minimum=>9) } 
    686705    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :is=>9) } 
     706    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>-1) } 
     707    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum=>-1) } 
     708    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is=>-1) } 
    687709    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>"a") } 
    688710    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum=>"a") } 
    689     assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>"a") } 
     711    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>"a".."z") } 
    690712    assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is=>"a") } 
    691713  end 
    692714 
     715  def test_validates_length_of_range_option_provided 
     716    assert_raise(ArgumentError) { Topic.validates_length_of(:title) } 
     717  end 
     718 
     719  def test_validates_length_of_custom_errors_for_in_with_message 
     720    Topic.validates_length_of( :title, :in=>2..5, :message=>"booin' must be between 2 and 5 characters" ) 
     721    t = Topic.create("title" => "uhohuhoh", "content" => "whatever") 
     722    assert !t.valid? 
     723    assert t.errors.on(:title) 
     724    assert_equal "booin' must be between 2 and 5 characters", t.errors["title"] 
     725  end 
     726 
    693727  def test_validates_length_of_custom_errors_for_minimum_with_message 
    694728    Topic.validates_length_of( :title, :minimum=>5, :message=>"boo %d" ) 
    695729    t = Topic.create("title" => "uhoh", "content" => "whatever") 
  • activerecord/lib/active_record/validations.rb

    old new  
    479479 
    480480        # can't use validates_each here, because it cannot cope with nonexistent attributes, 
    481481        # while errors.add_on_empty can 
    482       send(validation_method(configuration[:on])) do |record| 
    483         unless (configuration[:if] && !evaluate_condition(configuration[:if], record)) || (configuration[:unless] && evaluate_condition(configuration[:unless], record)) 
    484           record.errors.add_on_blank(attr_names, configuration[:message]) 
    485         end 
    486       end 
     482              send(validation_method(configuration[:on])) do |record| 
     483                unless (configuration[:if] && !evaluate_condition(configuration[:if], record)) || (configuration[:unless] && evaluate_condition(configuration[:unless], record)) 
     484                  record.errors.add_on_blank(attr_names, configuration[:message]) 
     485                end 
     486              end 
    487487      end 
    488488 
    489       # Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time: 
     489      # Validates that the specified attribute matches the length restriction supplied. 
     490      # Only one length restriction can be used at a time: 
    490491      # 
    491492      #   class Person < ActiveRecord::Base 
    492493      #     validates_length_of :first_name, :maximum=>30 
    493494      #     validates_length_of :last_name, :maximum=>30, :message=>"less than %d if you don't mind" 
    494495      #     validates_length_of :fax, :in => 7..32, :allow_nil => true 
    495496      #     validates_length_of :phone, :in => 7..32, :allow_blank => true 
    496       #     validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name" 
     497      #     validates_length_of :user_name, :in => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name" 
    497498      #     validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character" 
    498499      #     validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me." 
    499500      #   end 
    500501      # 
    501       # Configuration options: 
     502      # Length restriction configuration options: 
    502503      # * <tt>minimum</tt> - The minimum size of the attribute 
    503504      # * <tt>maximum</tt> - The maximum size of the attribute 
    504505      # * <tt>is</tt> - The exact size of the attribute 
    505       # * <tt>within</tt> - A range specifying the minimum and maximum size of the attribute 
    506       # * <tt>in</tt> - A synonym(or alias) for :within 
     506      # * <tt>in</tt> - A range specifying the minimum and maximum size of the attribute 
     507      # 
     508      # Other configuration options: 
    507509      # * <tt>allow_nil</tt> - Attribute may be nil; skip validation. 
    508510      # * <tt>allow_blank</tt> - Attribute may be blank; skip validation. 
    509       # 
    510511      # * <tt>too_long</tt> - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)") 
    511512      # * <tt>too_short</tt> - The error message if the attribute goes under the minimum (default is: "is too short (min is %d characters)") 
    512513      # * <tt>wrong_length</tt> - The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)") 
    513       # * <tt>message</tt> - The error message to use for a :minimum, :maximum, or :is violation.  An alias of the appropriate too_long/too_short/wrong_length message 
     514      # * <tt>message</tt> - Specifies a custom error message (default is nil). When defined it overrides the too_long/too_short/wrong_length messages. 
    514515      # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update) 
    515516      # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should 
    516517      #   occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }).  The 
    517518      #   method, proc or string should return or evaluate to a true or false value. 
    518519      # * <tt>unless</tt> - Specifies a method, proc or string to call to determine if the validation should 
    519520      #   not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }).  The 
    520       #   method, proc or string should return or evaluate to a true or false value.       
     521      #   method, proc or string should return or evaluate to a true or false value. 
    521522      def validates_length_of(*attrs) 
    522523        # Merge given options with defaults. 
    523524        options = { 
     
    531532        range_options = ALL_RANGE_OPTIONS & options.keys 
    532533        case range_options.size 
    533534          when 0 
    534             raise ArgumentError, 'Range unspecified.  Specify the :within, :maximum, :minimum, or :is option.' 
     535            raise ArgumentError, 'Range unspecified.  Specify the :in, :maximum, :minimum, or :is option.' 
    535536          when 1 
    536537            # Valid number of options; do nothing. 
    537538          else 
     
    543544        option_value = options[range_options.first] 
    544545 
    545546        case option 
    546           when :within, :in 
     547          when :in, :within 
    547548            raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range) 
    548549 
    549550            too_short = options[:too_short] % option_value.begin 
     
    551552 
    552553            validates_each(attrs, options) do |record, attr, value| 
    553554              if value.nil? or value.split(//).size < option_value.begin 
    554                 record.errors.add(attr, too_short) 
     555                record.errors.add(attr, options[:message] || too_short) 
    555556              elsif value.split(//).size > option_value.end 
    556                 record.errors.add(attr, too_long) 
     557                record.errors.add(attr, options[:message] || too_long) 
    557558              end 
    558559            end 
    559560          when :is, :minimum, :maximum 
     
    693694 
    694695        enum = configuration[:in] || configuration[:within] 
    695696 
    696         raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") 
     697        raise(ArgumentError, "An object with the method include? must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") 
    697698 
    698699        validates_each(attr_names, configuration) do |record, attr_name, value| 
    699700          record.errors.add(attr_name, configuration[:message]) unless enum.include?(value) 
     
    724725 
    725726        enum = configuration[:in] || configuration[:within] 
    726727 
    727         raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") 
     728        raise(ArgumentError, "An object with the method include? must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?") 
    728729 
    729730        validates_each(attr_names, configuration) do |record, attr_name, value| 
    730731          record.errors.add(attr_name, configuration[:message]) if enum.include?(value)