aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-02-17 21:00:39 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-02-18 13:16:33 +0900
commit1dfd01475d93b8801df0a2933094b437a607e3e2 (patch)
treeda5f7773f1e4077a65180cfbc0bc475a6d55f882 /activemodel/lib
parent9379d553d8669a99b55368715ccc48645f0441d2 (diff)
downloadrails-1dfd01475d93b8801df0a2933094b437a607e3e2.tar.gz
rails-1dfd01475d93b8801df0a2933094b437a607e3e2.tar.bz2
rails-1dfd01475d93b8801df0a2933094b437a607e3e2.zip
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.
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/type/date.rb4
-rw-r--r--activemodel/lib/active_model/type/date_time.rb4
-rw-r--r--activemodel/lib/active_model/type/decimal.rb4
-rw-r--r--activemodel/lib/active_model/type/float.rb2
-rw-r--r--activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb4
-rw-r--r--activemodel/lib/active_model/type/helpers/numeric.rb4
-rw-r--r--activemodel/lib/active_model/type/integer.rb9
-rw-r--r--activemodel/lib/active_model/type/time.rb4
8 files changed, 14 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?