aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-02-02 03:37:25 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2008-02-02 03:37:25 +0000
commit6d8534acc032ab3a1ecbb88e9a0ce09d18734071 (patch)
tree9f1df9a93aea7bec3ab8d7b80ca935044eb10c5f /activerecord
parentb1ea27630df0f0639900b321d756091b42ed29f7 (diff)
downloadrails-6d8534acc032ab3a1ecbb88e9a0ce09d18734071.tar.gz
rails-6d8534acc032ab3a1ecbb88e9a0ce09d18734071.tar.bz2
rails-6d8534acc032ab3a1ecbb88e9a0ce09d18734071.zip
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
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/base.rb18
1 files changed, 17 insertions, 1 deletions
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