From 6d8534acc032ab3a1ecbb88e9a0ce09d18734071 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 2 Feb 2008 03:37:25 +0000 Subject: When multiparameter date assignment fails due to an invalid date, fall back to create a Time and convert to_date. Closes #10556 [leikind] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8777 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/base.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6cf5957027..f1759aa68f 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2470,6 +2470,10 @@ module ActiveRecord #:nodoc: end # Includes an ugly hack for Time.local instead of Time.new because the latter is reserved by Time itself. + def instantiate_time_object(*values) + @@default_timezone == :utc ? Time.utc(*values) : Time.local(*values) + end + def execute_callstack_for_multiparameter_attributes(callstack) errors = [] callstack.each do |name, values| @@ -2478,7 +2482,19 @@ module ActiveRecord #:nodoc: send(name + "=", nil) else begin - send(name + "=", Time == klass ? (@@default_timezone == :utc ? klass.utc(*values) : klass.local(*values)) : klass.new(*values)) + value = if Time == klass + instantiate_time_object(*values) + elsif Date == klass + begin + Date.new(*values) + rescue ArgumentError => ex # if Date.new raises an exception on an invalid date + 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 + end + else + klass.new(*values) + end + + send(name + "=", value) rescue => ex errors << AttributeAssignmentError.new("error on assignment #{values.inspect} to #{name}", ex, name) end -- cgit v1.2.3