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

Ticket #10556: fix_for_instantiatiion_of_an_object_with_an_invalid_date_in_multiparameter_attributes2.diff

File fix_for_instantiatiion_of_an_object_with_an_invalid_date_in_multiparameter_attributes2.diff, 2.8 kB (added by leikind, 2 years ago)

I removed some redundant comment from the test

  • activerecord/test/invalid_date_test.rb

    old new  
     1require 'abstract_unit' 
     2require 'fixtures/topic' 
     3 
     4class InvalidDateTest < Test::Unit::TestCase 
     5  def test_assign_valid_dates 
     6     
     7    valid_dates = [[2007, 11, 30], [1993, 2, 28], [2008, 2, 29]] 
     8     
     9    invalid_dates = [[2007, 11, 31], [1993, 2, 29], [2007, 2, 29]] 
     10     
     11    topic = Topic.new 
     12 
     13    valid_dates.each do |date_src| 
     14      topic = Topic.new("last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s) 
     15      assert_equal(topic.last_read, Date.new(*date_src)) 
     16    end 
     17 
     18    invalid_dates.each do |date_src| 
     19      assert_nothing_raised do 
     20        topic = Topic.new({"last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s}) 
     21        assert_equal(topic.last_read, Time.local(*date_src).to_date, "The date should be modified according to the behaviour of the Time object") 
     22      end 
     23    end 
     24  end 
     25end 
  • activerecord/lib/active_record/base.rb

    old new  
    23792379      end 
    23802380 
    23812381      # Includes an ugly hack for Time.local instead of Time.new because the latter is reserved by Time itself. 
     2382      def instantiate_time_object(*values) 
     2383        @@default_timezone == :utc ? Time.utc(*values) : Time.local(*values) 
     2384      end 
     2385 
    23822386      def execute_callstack_for_multiparameter_attributes(callstack) 
    23832387        errors = [] 
    23842388        callstack.each do |name, values| 
     
    23872391            send(name + "=", nil) 
    23882392          else 
    23892393            begin 
    2390               send(name + "=", Time == klass ? (@@default_timezone == :utc ? klass.utc(*values) : klass.local(*values)) : klass.new(*values)) 
     2394              value = if Time == klass  
     2395                instantiate_time_object(*values) 
     2396              elsif Date == klass 
     2397                begin           
     2398                  Date.new(*values) 
     2399                rescue ArgumentError => ex # if Date.new raises an exception on an invalid date 
     2400                  instantiate_time_object(*values).to_date # we instantiate Time object and convert it back to a date thus using Time's logic in handling invalid dates 
     2401                end 
     2402              else 
     2403                klass.new(*values) 
     2404              end 
     2405               
     2406              send(name + "=", value) 
    23912407            rescue => ex 
    23922408              errors << AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name) 
    23932409            end