Skip to content

Commit

Permalink
dup strings on return so that in place modifications do not break any…
Browse files Browse the repository at this point in the history
…thing. I am looking at you "compute_table_name"
  • Loading branch information
tenderlove committed Mar 17, 2011
1 parent c834a75 commit 00f0879
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def define_attr_method(name, value=nil, &block)
# use eval instead of a block to work around a memory leak in dev
# mode in fcgi
value = value.nil? ? 'nil' : value.to_s
sing.send(:define_method, name) { value }
sing.send(:define_method, name) { value.dup }
end
end

Expand Down
11 changes: 11 additions & 0 deletions activemodel/test/cases/attribute_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class << self
define_method(:bar) do
'original bar'
end

define_method(:zomg) do
'original zomg'
end
end

def attributes
Expand Down Expand Up @@ -98,6 +102,13 @@ class AttributeMethodsTest < ActiveModel::TestCase
assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.send(:'foo bar')
end

def test_defined_methods_always_return_duped_string
ModelWithAttributes.define_attr_method(:zomg, 'lol')
assert_equal 'lol', ModelWithAttributes.zomg
ModelWithAttributes.zomg << 'bbq'
assert_equal 'lol', ModelWithAttributes.zomg
end

test '#define_attr_method generates attribute method' do
ModelWithAttributes.define_attr_method(:bar, 'bar')

Expand Down

0 comments on commit 00f0879

Please sign in to comment.