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

Changeset 241

Show
Ignore:
Timestamp:
12/21/04 23:41:07 (4 years ago)
Author:
david
Message:

Fixed validates_{confirmation,acceptance}_of to only happen when the virtual attributes are not nil #348 [dpiddy@gmail.com]

Files:

Legend:

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

    r233 r241  
    11*SVN* 
     2 
     3* Fixed validates_{confirmation,acceptance}_of to only happen when the virtual attributes are not nil #348 [dpiddy@gmail.com] 
    24 
    35* Added a require_association hook on const_missing that makes it possible to use any model class without requiring it first. This makes STI look like: 
  • trunk/activerecord/lib/active_record/validations.rb

    r226 r241  
    7575      # 
    7676      # The person has to already have a password attribute (a column in the people table), but the password_confirmation is virtual. 
    77       # It exists only as an in-memory variable for validating the password. This check is performed on save by default. 
     77      # It exists only as an in-memory variable for validating the password. This check is performed only if password_confirmation 
     78      # is not nil and by default on save. 
    7879      # 
    7980      # Configuration options: 
     
    8687        for attr_name in attr_names 
    8788          attr_accessor "#{attr_name}_confirmation" 
    88           class_eval(%(#{validation_method(configuration[:on])} %{errors.add('#{attr_name}', "#{configuration[:message]}") unless #{attr_name} == #{attr_name}_confirmation})) 
     89          class_eval(%(#{validation_method(configuration[:on])} %{errors.add('#{attr_name}', "#{configuration[:message]}") unless #{attr_name}_confirmation.nil? or #{attr_name} == #{attr_name}_confirmation})) 
    8990        end 
    9091      end 
     
    9798      #   end 
    9899      # 
    99       # The terms_of_service attribute is entirely virtual. No database column is needed. This check is performed on save by default. 
     100      # The terms_of_service attribute is entirely virtual. No database column is needed. This check is performed only if 
     101      # terms_of_service is not nil and by default on save. 
    100102      # 
    101103      # Configuration options: 
     
    110112        for attr_name in attr_names 
    111113          attr_accessor(attr_name) 
    112           class_eval(%(#{validation_method(configuration[:on])} %{errors.add('#{attr_name}', '#{configuration[:message]}') unless #{attr_name} == "1"})) 
     114          class_eval(%(#{validation_method(configuration[:on])} %{errors.add('#{attr_name}', '#{configuration[:message]}') unless #{attr_name}.nil? or #{attr_name} == "1"})) 
    113115        end 
    114116      end 
  • trunk/activerecord/test/validations_test.rb

    r217 r241  
    126126  end 
    127127 
     128  def test_title_confirmation_no_confirm 
     129    Topic.validates_confirmation_of(:title) 
     130     
     131    t = Topic.create("title" => "We should not be confirmed") 
     132    assert t.save 
     133  end 
     134   
    128135  def test_title_confirmation 
    129136    Topic.validates_confirmation_of(:title) 
    130137 
    131     t = Topic.create("title" => "We should be confirmed"
     138    t = Topic.create("title" => "We should be confirmed","title_confirmation" => ""
    132139    assert !t.save 
    133140 
    134141    t.title_confirmation = "We should be confirmed" 
     142    assert t.save 
     143  end 
     144 
     145  def test_terms_of_service_agreement_no_acceptance 
     146    Topic.validates_acceptance_of(:terms_of_service, :on => :create) 
     147 
     148    t = Topic.create("title" => "We should not be confirmed") 
    135149    assert t.save 
    136150  end 
     
    139153    Topic.validates_acceptance_of(:terms_of_service, :on => :create) 
    140154 
    141     t = Topic.create("title" => "We should be confirmed"
     155    t = Topic.create("title" => "We should be confirmed","terms_of_service" => ""
    142156    assert !t.save 
    143157    assert_equal "must be accepted", t.errors.on(:terms_of_service) 
     
    151165    Topic.validates_acceptance_of(:eula, :message => "must be abided", :on => :create) 
    152166 
    153     t = Topic.create("title" => "We should be confirmed"
     167    t = Topic.create("title" => "We should be confirmed","eula" => ""
    154168    assert !t.save 
    155169    assert_equal "must be abided", t.errors.on(:eula)