Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

When multiparameter date assignment fails due to an invalid date, fal…

…l 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
  • Loading branch information...
commit 6d8534acc032ab3a1ecbb88e9a0ce09d18734071 1 parent b1ea276
@jeremy jeremy authored
Showing with 17 additions and 1 deletion.
  1. +17 −1 activerecord/lib/active_record/base.rb
View
18 activerecord/lib/active_record/base.rb
@@ -2470,6 +2470,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|
@@ -2478,7 +2482,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
Please sign in to comment.
Something went wrong with that request. Please try again.