aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-07-05 15:37:19 -0400
committerGitHub <noreply@github.com>2017-07-05 15:37:19 -0400
commite3ed2434f223402f899e3b635a220532d0e1330e (patch)
treec4b72285d2695071e49accdc1db171fab08b5869
parent81492d8f914b39cba4ddb0569af28ebbbed73fa6 (diff)
parent7b2dfdeab6e4ef096e4dc1fe313056f08ccf7dc5 (diff)
downloadrails-e3ed2434f223402f899e3b635a220532d0e1330e.tar.gz
rails-e3ed2434f223402f899e3b635a220532d0e1330e.tar.bz2
rails-e3ed2434f223402f899e3b635a220532d0e1330e.zip
Merge pull request #29676 from lugray/fix_date_time_serialize
Fix `ActiveModel::Type::DateTime#serialize`
-rw-r--r--activemodel/lib/active_model/type/date_time.rb4
-rw-r--r--activerecord/test/cases/date_time_test.rb13
2 files changed, 17 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/type/date_time.rb b/activemodel/lib/active_model/type/date_time.rb
index 5cb0077e45..8ad0daa9a6 100644
--- a/activemodel/lib/active_model/type/date_time.rb
+++ b/activemodel/lib/active_model/type/date_time.rb
@@ -10,6 +10,10 @@ module ActiveModel
:datetime
end
+ def serialize(value)
+ super(cast(value))
+ end
+
private
def cast_value(value)
diff --git a/activerecord/test/cases/date_time_test.rb b/activerecord/test/cases/date_time_test.rb
index ad7da9de70..6cd98fe254 100644
--- a/activerecord/test/cases/date_time_test.rb
+++ b/activerecord/test/cases/date_time_test.rb
@@ -58,4 +58,17 @@ class DateTimeTest < ActiveRecord::TestCase
assert_equal now, task.starting
end
end
+
+ def test_date_time_with_string_value_with_subsecond_precision
+ skip unless subsecond_precision_supported?
+ string_value = "2017-07-04 14:19:00.5"
+ topic = Topic.create(written_on: string_value)
+ assert_equal topic, Topic.find_by(written_on: string_value)
+ end
+
+ def test_date_time_with_string_value_with_non_iso_format
+ string_value = "04/07/2017 2:19pm"
+ topic = Topic.create(written_on: string_value)
+ assert_equal topic, Topic.find_by(written_on: string_value)
+ end
end