Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow to read and write AR attributes with non valid identifiers

  • Loading branch information...
commit 1b7b7243062519a58286cf88411bec69cd84fa24 1 parent 7717fc3
@spastorino spastorino authored
View
2  activerecord/lib/active_record/attribute_methods/read.rb
@@ -66,7 +66,7 @@ def define_read_method(symbol, attr_name, column)
if cache_attribute?(attr_name)
access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
end
- generated_attribute_methods.module_eval("def #{symbol}; #{access_code}; end", __FILE__, __LINE__)
+ generated_attribute_methods.send(:define_method, symbol) { eval(access_code) }
end
end
View
4 activerecord/lib/active_record/attribute_methods/write.rb
@@ -10,7 +10,9 @@ module Write
module ClassMethods
protected
def define_method_attribute=(attr_name)
- generated_attribute_methods.module_eval("def #{attr_name}=(new_value); write_attribute('#{attr_name}', new_value); end", __FILE__, __LINE__)
+ generated_attribute_methods.send(:define_method, "#{attr_name}=") do |new_value|
+ write_attribute(attr_name, new_value)
+ end
end
end
View
12 activerecord/test/cases/base_test.rb
@@ -43,6 +43,8 @@ class ReadonlyTitlePost < Post
attr_readonly :title
end
+class Weird < ActiveRecord::Base; end
+
class Boolean < ActiveRecord::Base; end
class BasicsTest < ActiveRecord::TestCase
@@ -461,6 +463,16 @@ def test_readonly_attributes
assert_equal "changed", post.body
end
+ def test_non_valid_identifier_column_name
+ weird = Weird.create('a$b' => 'value')
+ weird.reload
+ assert_equal 'value', weird.send('a$b')
+
+ weird.update_attribute('a$b', 'value2')
+ weird.reload
+ assert_equal 'value2', weird.send('a$b')
+ end
+
def test_multiparameter_attributes_on_date
attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" }
topic = Topic.find(1)
View
3  activerecord/test/schema/schema.rb
@@ -651,6 +651,9 @@ def create_table(*args, &block)
t.integer :molecule_id
t.string :name
end
+ create_table :weirds, :force => true do |t|
+ t.string 'a$b'
+ end
except 'SQLite' do
Please sign in to comment.
Something went wrong with that request. Please try again.