Browse files

define_attr_method should be able to define methods that returns nil

  • Loading branch information...
1 parent 4532b39 commit 445241d713782262134ede64a967369f803076ff @spastorino spastorino committed Mar 17, 2011
View
4 activemodel/lib/active_model/attribute_methods.rb
@@ -106,8 +106,8 @@ def define_attr_method(name, value=nil, &block)
if block_given?
sing.send :define_method, name, &block
else
- value = value.nil? ? 'nil' : value.to_s
- sing.send(:define_method, name) { value.dup }
+ value = value.to_s if value
+ sing.send(:define_method, name) { value && value.dup }
end
end
View
2 activemodel/test/cases/attribute_methods_test.rb
@@ -115,6 +115,8 @@ def test_defined_methods_always_return_duped_string
assert_respond_to ModelWithAttributes, :bar
assert_equal "original bar", ModelWithAttributes.original_bar
assert_equal "bar", ModelWithAttributes.bar
+ ModelWithAttributes.define_attr_method(:bar)
+ assert !ModelWithAttributes.bar
end
test '#define_attr_method generates attribute method with invalid identifier characters' do

0 comments on commit 445241d

Please sign in to comment.