diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/type/string.rb | 7 | ||||
-rw-r--r-- | activemodel/test/cases/type/string_test.rb | 13 |
2 files changed, 17 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/type/string.rb b/activemodel/lib/active_model/type/string.rb index c7e0208a5a..850cab962b 100644 --- a/activemodel/lib/active_model/type/string.rb +++ b/activemodel/lib/active_model/type/string.rb @@ -12,7 +12,12 @@ module ActiveModel private def cast_value(value) - ::String.new(super) + 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 diff --git a/activemodel/test/cases/type/string_test.rb b/activemodel/test/cases/type/string_test.rb index 222083817e..47d412e27e 100644 --- a/activemodel/test/cases/type/string_test.rb +++ b/activemodel/test/cases/type/string_test.rb @@ -12,16 +12,25 @@ module ActiveModel end test "cast strings are mutable" do - s = "foo" 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 "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 |