1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #28729 from matthewd/dont-freeze-inputs

Don't freeze input strings
This commit is contained in:
Matthew Draper 2017-04-12 07:33:11 +09:30
parent 531219fdbd
commit 1fb87ea559
3 changed files with 31 additions and 4 deletions

View file

@ -1,3 +1,10 @@
* The original string assigned to a model attribute is no longer incorrectly
frozen.
Fixes #24185, #28718.
*Matthew Draper*
* Avoid converting integer as a string into float.
*namusyaka*

View file

@ -11,9 +11,14 @@ module ActiveModel
private
def cast_value(value)
::String.new(super)
end
def cast_value(value)
case value
when ::String then ::String.new(value)
when true then "t".freeze
when false then "f".freeze
else value.to_s
end
end
end
end
end

View file

@ -10,6 +10,18 @@ module ActiveModel
assert_equal "123", type.cast(123)
end
test "cast strings are mutable" do
type = Type::String.new
s = "foo"
assert_equal false, type.cast(s).frozen?
assert_equal false, s.frozen?
f = "foo".freeze
assert_equal false, type.cast(f).frozen?
assert_equal true, f.frozen?
end
test "immutable strings are not duped coming out" do
s = "foo"
type = Type::ImmutableString.new
@ -18,10 +30,13 @@ module ActiveModel
end
test "values are duped coming out" do
s = "foo"
type = Type::String.new
s = "foo"
assert_not_same s, type.cast(s)
assert_equal s, type.cast(s)
assert_not_same s, type.deserialize(s)
assert_equal s, type.deserialize(s)
end
end
end