diff options
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/attribute_methods.rb | 10 | ||||
-rw-r--r-- | activemodel/test/cases/attribute_methods_test.rb | 11 |
2 files changed, 8 insertions, 13 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 73468afe55..be55581c66 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -106,8 +106,14 @@ module ActiveModel if block_given? sing.send :define_method, name, &block else - value = value.to_s if value - sing.send(:define_method, name) { value && value.dup } + if name =~ /^[a-zA-Z_]\w*[!?=]?$/ + sing.class_eval <<-eorb, __FILE__, __LINE__ + 1 + def #{name}; #{value.nil? ? 'nil' : value.to_s.inspect}; end + eorb + else + value = value.to_s if value + sing.send(:define_method, name) { value } + end end end diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index e814666d99..022c6716bd 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -9,10 +9,6 @@ class ModelWithAttributes define_method(:bar) do 'original bar' end - - define_method(:zomg) do - 'original zomg' - end end def attributes @@ -102,13 +98,6 @@ 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') |