aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSantiago Pastorino <santiago@wyeworks.com>2011-03-16 21:19:35 -0300
committerSantiago Pastorino <santiago@wyeworks.com>2011-03-16 21:20:44 -0300
commitc834a751d2acbd55b580cbba2e96dd29c5d9a452 (patch)
tree423e181f132e2977ac124b83a59032d21ac7bca8
parentfda45f4fc493f5596375199fefe854df24b1ffbf (diff)
downloadrails-c834a751d2acbd55b580cbba2e96dd29c5d9a452.tar.gz
rails-c834a751d2acbd55b580cbba2e96dd29c5d9a452.tar.bz2
rails-c834a751d2acbd55b580cbba2e96dd29c5d9a452.zip
define_attr_method correctly defines methods with invalid identifiers
-rw-r--r--activemodel/lib/active_model/attribute_methods.rb5
-rw-r--r--activemodel/test/cases/attribute_methods_test.rb6
2 files changed, 6 insertions, 5 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb
index 2a99450a3d..21ddef6b75 100644
--- a/activemodel/lib/active_model/attribute_methods.rb
+++ b/activemodel/lib/active_model/attribute_methods.rb
@@ -108,9 +108,8 @@ module ActiveModel
else
# use eval instead of a block to work around a memory leak in dev
# mode in fcgi
- sing.class_eval <<-eorb, __FILE__, __LINE__ + 1
- def #{name}; #{value.nil? ? 'nil' : value.to_s.inspect}; end
- eorb
+ value = value.nil? ? 'nil' : value.to_s
+ sing.send(:define_method, name) { value }
end
end
diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb
index 13f29a8fd9..5cf905bc1f 100644
--- a/activemodel/test/cases/attribute_methods_test.rb
+++ b/activemodel/test/cases/attribute_methods_test.rb
@@ -7,7 +7,7 @@ class ModelWithAttributes
class << self
define_method(:bar) do
- 'bar'
+ 'original bar'
end
end
@@ -49,7 +49,7 @@ class ModelWithWeirdNamesAttributes
class << self
define_method(:'c?d') do
- 'c?d'
+ 'original c?d'
end
end
@@ -102,6 +102,7 @@ class AttributeMethodsTest < ActiveModel::TestCase
ModelWithAttributes.define_attr_method(:bar, 'bar')
assert_respond_to ModelWithAttributes, :bar
+ assert_equal "original bar", ModelWithAttributes.original_bar
assert_equal "bar", ModelWithAttributes.bar
end
@@ -109,6 +110,7 @@ class AttributeMethodsTest < ActiveModel::TestCase
ModelWithWeirdNamesAttributes.define_attr_method(:'c?d', 'c?d')
assert_respond_to ModelWithWeirdNamesAttributes, :'c?d'
+ assert_equal "original c?d", ModelWithWeirdNamesAttributes.send('original_c?d')
assert_equal "c?d", ModelWithWeirdNamesAttributes.send('c?d')
end