Skip to content

Commit 4244685

Browse files
authored
Merge pull request rails#54435 from xodene/xodene-default-mutability-attribute
Make Value type default to mutable
2 parents 17b9ca8 + 36de5ad commit 4244685

File tree

11 files changed

+51
-9
lines changed

11 files changed

+51
-9
lines changed

activemodel/lib/active_model/type/boolean.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Type
1212
# - Empty strings are coerced to +nil+.
1313
# - All other values will be coerced to +true+.
1414
class Boolean < Value
15+
include Helpers::Immutable
1516
FALSE_VALUES = [
1617
false, 0,
1718
"0", :"0",

activemodel/lib/active_model/type/date.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Type
2424
# String values are parsed using the ISO 8601 date format. Any other values
2525
# are cast using their +to_date+ method, if it exists.
2626
class Date < Value
27+
include Helpers::Immutable
2728
include Helpers::Timezone
2829
include Helpers::AcceptsMultiparameterTime.new
2930

activemodel/lib/active_model/type/decimal.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module Type
4343
# attribute :weight, :decimal, precision: 24
4444
# end
4545
class Decimal < Value
46+
include Helpers::Immutable
4647
include Helpers::Numeric
4748
BIGDECIMAL_PRECISION = 18
4849

activemodel/lib/active_model/type/float.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module Type
3434
# - <tt>"-Infinity"</tt> is cast to <tt>-Float::INFINITY</tt>.
3535
# - <tt>"NaN"</tt> is cast to +Float::NAN+.
3636
class Float < Value
37+
include Helpers::Immutable
3738
include Helpers::Numeric
3839

3940
def type

activemodel/lib/active_model/type/helpers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
require "active_model/type/helpers/accepts_multiparameter_time"
44
require "active_model/type/helpers/numeric"
55
require "active_model/type/helpers/mutable"
6+
require "active_model/type/helpers/immutable"
67
require "active_model/type/helpers/time_value"
78
require "active_model/type/helpers/timezone"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveModel
4+
module Type
5+
module Helpers # :nodoc: all
6+
module Immutable
7+
def mutable? # :nodoc:
8+
false
9+
end
10+
end
11+
end
12+
end
13+
end

activemodel/lib/active_model/type/immutable_string.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ module Type
3535
#
3636
# person.active # => "aye"
3737
class ImmutableString < Value
38+
include Helpers::Immutable
39+
3840
def initialize(**args)
3941
@true = -(args.delete(:true)&.to_s || "t")
4042
@false = -(args.delete(:false)&.to_s || "f")

activemodel/lib/active_model/type/integer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module Type
4242
# attribute :age, :integer, limit: 6
4343
# end
4444
class Integer < Value
45+
include Helpers::Immutable
4546
include Helpers::Numeric
4647

4748
# Column storage size in bytes.

activemodel/lib/active_model/type/time.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ def type
4646
:time
4747
end
4848

49-
def mutable? # :nodoc:
50-
# Time#zone can be mutated by #utc or #localtime
51-
# However when serializing the time zone will always
52-
# be coerced and even if the zone was mutated Time instances
53-
# remain equal, so we don't need to implement `#changed_in_place?`
54-
true
55-
end
56-
5749
def user_input_in_time_zone(value)
5850
return unless value.present?
5951

activemodel/lib/active_model/type/value.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def serialized? # :nodoc:
138138
end
139139

140140
def mutable? # :nodoc:
141-
false
141+
true
142142
end
143143

144144
def as_json(*)

0 commit comments

Comments
 (0)