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

Changeset 9129

Show
Ignore:
Timestamp:
03/29/08 17:53:44 (4 months ago)
Author:
david
Message:

Fixed that validates_size_of :within works in associations (closes #11295, #10019) [cavalle]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r9127 r9129  
    11*SVN* 
     2 
     3* Fixed that validates_size_of :within works in associations #11295, #10019 [cavalle] 
    24 
    35* Track changes to unsaved attributes.  [Jeremy Kemper] 
  • trunk/activerecord/lib/active_record/validations.rb

    r9055 r9129  
    554554 
    555555            validates_each(attrs, options) do |record, attr, value| 
    556               if value.nil? or value.split(//).size < option_value.begin 
     556              value = value.split(//) if value.kind_of?(String) 
     557              if value.nil? or value.size < option_value.begin 
    557558                record.errors.add(attr, too_short) 
    558               elsif value.split(//).size > option_value.end 
     559              elsif value.size > option_value.end 
    559560                record.errors.add(attr, too_long) 
    560561              end 
     
    570571 
    571572            validates_each(attrs, options) do |record, attr, value| 
    572               if value.kind_of?(String) 
    573                 record.errors.add(attr, message) unless !value.nil? and value.split(//).size.method(validity_checks[option])[option_value] 
    574               else 
    575                 record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value] 
    576               end 
     573              value = value.split(//) if value.kind_of?(String) 
     574              record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value] 
    577575            end 
    578576        end 
  • trunk/activerecord/test/cases/validations_test.rb

    r9084 r9129  
    806806    reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain') 
    807807    assert t.valid? 
     808  end 
     809   
     810  def test_validates_size_of_association_using_within 
     811    assert_nothing_raised { Topic.validates_size_of :replies, :within => 1..2 } 
     812    t = Topic.new('title' => 'noreplies', 'content' => 'whatever') 
     813    assert !t.save 
     814    assert t.errors.on(:replies) 
     815     
     816    reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain') 
     817    assert t.valid? 
     818     
     819    2.times { t.replies.build('title' => 'areply', 'content' => 'whateveragain') } 
     820    assert !t.save 
     821    assert t.errors.on(:replies) 
    808822  end 
    809823