From 1dfd01475d93b8801df0a2933094b437a607e3e2 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 17 Feb 2019 21:00:39 +0900 Subject: Extract duplicated `serialize` methods into helpers Since `serialize` is passed user input args (from `where`, schema default, etc), a helper should provide `serialize` if the helper also provide `cast`. Related #32624, 34cc301, a741208. --- activemodel/lib/active_model/type/date.rb | 4 ---- activemodel/lib/active_model/type/date_time.rb | 4 ---- activemodel/lib/active_model/type/decimal.rb | 4 ---- activemodel/lib/active_model/type/float.rb | 2 -- .../lib/active_model/type/helpers/accepts_multiparameter_time.rb | 4 ++++ activemodel/lib/active_model/type/helpers/numeric.rb | 4 ++++ activemodel/lib/active_model/type/integer.rb | 9 ++------- activemodel/lib/active_model/type/time.rb | 4 ++++ activerecord/test/cases/base_test.rb | 3 +++ 9 files changed, 17 insertions(+), 21 deletions(-) diff --git a/activemodel/lib/active_model/type/date.rb b/activemodel/lib/active_model/type/date.rb index da12041dee..a7e6122ff4 100644 --- a/activemodel/lib/active_model/type/date.rb +++ b/activemodel/lib/active_model/type/date.rb @@ -10,10 +10,6 @@ module ActiveModel :date end - def serialize(value) - cast(value) - end - def type_cast_for_schema(value) value.to_s(:db).inspect end diff --git a/activemodel/lib/active_model/type/date_time.rb b/activemodel/lib/active_model/type/date_time.rb index 62f28fc4a7..133410e821 100644 --- a/activemodel/lib/active_model/type/date_time.rb +++ b/activemodel/lib/active_model/type/date_time.rb @@ -13,10 +13,6 @@ module ActiveModel :datetime end - def serialize(value) - super(cast(value)) - end - private def cast_value(value) diff --git a/activemodel/lib/active_model/type/decimal.rb b/activemodel/lib/active_model/type/decimal.rb index b37dad1c41..e8ee18c00e 100644 --- a/activemodel/lib/active_model/type/decimal.rb +++ b/activemodel/lib/active_model/type/decimal.rb @@ -12,10 +12,6 @@ module ActiveModel :decimal end - def serialize(value) - cast(value) - end - def type_cast_for_schema(value) value.to_s.inspect end diff --git a/activemodel/lib/active_model/type/float.rb b/activemodel/lib/active_model/type/float.rb index 9dbe32e5a6..ea1987df7c 100644 --- a/activemodel/lib/active_model/type/float.rb +++ b/activemodel/lib/active_model/type/float.rb @@ -18,8 +18,6 @@ module ActiveModel end end - alias serialize cast - private def cast_value(value) diff --git a/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb b/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb index ad891f841e..e15d7b013f 100644 --- a/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb +++ b/activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb @@ -5,6 +5,10 @@ module ActiveModel module Helpers # :nodoc: all class AcceptsMultiparameterTime < Module def initialize(defaults: {}) + define_method(:serialize) do |value| + super(cast(value)) + end + define_method(:cast) do |value| if value.is_a?(Hash) value_from_multiparameter_assignment(value) diff --git a/activemodel/lib/active_model/type/helpers/numeric.rb b/activemodel/lib/active_model/type/helpers/numeric.rb index 473cdb0c67..444847a210 100644 --- a/activemodel/lib/active_model/type/helpers/numeric.rb +++ b/activemodel/lib/active_model/type/helpers/numeric.rb @@ -4,6 +4,10 @@ module ActiveModel module Type module Helpers # :nodoc: all module Numeric + def serialize(value) + cast(value) + end + def cast(value) value = \ case value diff --git a/activemodel/lib/active_model/type/integer.rb b/activemodel/lib/active_model/type/integer.rb index da74aaa3c5..5878b94171 100644 --- a/activemodel/lib/active_model/type/integer.rb +++ b/activemodel/lib/active_model/type/integer.rb @@ -24,7 +24,7 @@ module ActiveModel end def serialize(value) - result = cast(value) + result = super if result ensure_in_range(result) end @@ -35,12 +35,7 @@ module ActiveModel attr_reader :range def cast_value(value) - case value - when true then 1 - when false then 0 - else - value.to_i rescue nil - end + value.to_i rescue nil end def ensure_in_range(value) diff --git a/activemodel/lib/active_model/type/time.rb b/activemodel/lib/active_model/type/time.rb index 61847a4ce7..16d3efb728 100644 --- a/activemodel/lib/active_model/type/time.rb +++ b/activemodel/lib/active_model/type/time.rb @@ -13,6 +13,10 @@ module ActiveModel :time end + def serialize(value) + super || value + end + def user_input_in_time_zone(value) return unless value.present? diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 3168bf6977..63528d09d5 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -685,6 +685,9 @@ class BasicsTest < ActiveRecord::TestCase topic = Topic.find(1) topic.attributes = attributes assert_equal Time.local(2000, 1, 1, 5, 42, 0), topic.bonus_time + + topic.save! + assert_equal topic, Topic.find_by(attributes) end end -- cgit v1.2.3