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) |
|---|
-
activerecord/test/invalid_date_test.rb
old new 1 require 'abstract_unit' 2 require 'fixtures/topic' 3 4 class 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 25 end -
activerecord/lib/active_record/base.rb
old new 2379 2379 end 2380 2380 2381 2381 # 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 2382 2386 def execute_callstack_for_multiparameter_attributes(callstack) 2383 2387 errors = [] 2384 2388 callstack.each do |name, values| … … 2387 2391 send(name + "=", nil) 2388 2392 else 2389 2393 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) 2391 2407 rescue => ex 2392 2408 errors << AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name) 2393 2409 end