From 445241d713782262134ede64a967369f803076ff Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 17 Mar 2011 23:17:20 -0300 Subject: define_attr_method should be able to define methods that returns nil --- activemodel/lib/active_model/attribute_methods.rb | 4 ++-- activemodel/test/cases/attribute_methods_test.rb | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 76bfd4b77d..73468afe55 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -106,8 +106,8 @@ module ActiveModel 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 diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index 7fa5a2a7d7..e814666d99 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -115,6 +115,8 @@ class AttributeMethodsTest < ActiveModel::TestCase 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 -- cgit v1.2.3