Skip to content

Commit

Permalink
dup String in strip_string
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbeck committed Nov 17, 2022
1 parent 37c1cb2 commit 7004e43
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/strip_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def self.strip_record(record, options = {})

def self.strip_string(value, options = {})
return value unless value.is_a?(String)
return value if value.frozen?
value = value.dup

allow_empty = options[:allow_empty]
collapse_spaces = options[:collapse_spaces]
Expand Down
8 changes: 5 additions & 3 deletions test/strip_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,12 @@ def test_should_strip_and_allow_empty
assert_equal "", record.bang
end

def test_should_skip_frozen_values
record = StripAllMockRecord.new(frozen: " ice ".freeze)
def test_should_not_mutate_values
record = StripAllMockRecord.new(foo: " foo ")
old_value = record.foo
record.valid?
assert_equal " ice ", record.frozen
assert_equal "foo", record.foo
refute_equal old_value, record.foo
end

def test_should_collapse_duplicate_spaces
Expand Down

0 comments on commit 7004e43

Please sign in to comment.