aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activemodel/lib/active_model/attribute_methods.rb2
-rw-r--r--activemodel/test/cases/attribute_methods_test.rb11
2 files changed, 12 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb
index 21ddef6b75..c985eb293b 100644
--- a/activemodel/lib/active_model/attribute_methods.rb
+++ b/activemodel/lib/active_model/attribute_methods.rb
@@ -109,7 +109,7 @@ module ActiveModel
# 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
diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb
index 5cf905bc1f..7fa5a2a7d7 100644
--- a/activemodel/test/cases/attribute_methods_test.rb
+++ b/activemodel/test/cases/attribute_methods_test.rb
@@ -9,6 +9,10 @@ class ModelWithAttributes
define_method(:bar) do
'original bar'
end
+
+ define_method(:zomg) do
+ 'original zomg'
+ end
end
def attributes
@@ -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')