Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion activemodel/lib/active_model/type/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def changed_in_place?(raw_old_value, new_value)
private

def cast_value(value)
::String.new(super)
case value
when ::String then ::String.new(value)
when true then "t".freeze
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just sucks that we have to duplicate the conditionals and the true and false cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but it seems the rate of change on these branches is fairly low.

when false then "f".freeze
else value.to_s
end
end
end
end
Expand Down
13 changes: 11 additions & 2 deletions activemodel/test/cases/type/string_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ class StringTest < ActiveModel::TestCase
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
Expand Down