Permalink
Browse files

2-0-stable: When multiparameter date assignment fails due to an inval…

…id date, fall back to create a Time and convert to_date. References #10556 [leikind]

Merging [8777]


git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/2-0-stable@8839 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 0dad92a commit 8b63dd09c0f207f5e21673da5f4f1f563b65c220 @NZKoz NZKoz committed Feb 10, 2008
Showing with 17 additions and 1 deletion.
  1. +17 −1 activerecord/lib/active_record/base.rb
View
18 activerecord/lib/active_record/base.rb
@@ -2391,6 +2391,10 @@ def assign_multiparameter_attributes(pairs)
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|
@@ -2399,7 +2403,19 @@ def execute_callstack_for_multiparameter_attributes(callstack)
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

0 comments on commit 8b63dd0

Please sign in to comment.