提交 baa237c9 编写于 作者: S Santiago Pastorino

Allow to read and write AR attributes with non valid identifiers

上级 da6c7bd4
......@@ -70,7 +70,10 @@ 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; alias #{symbol} _#{symbol}", __FILE__, __LINE__)
generated_attribute_methods.module_eval do
define_method("_#{symbol}") { eval(access_code) }
alias_method(symbol, "_#{symbol}")
end
end
end
......
......@@ -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
......
......@@ -45,6 +45,8 @@ class ReadonlyTitlePost < Post
attr_readonly :title
end
class Weird < ActiveRecord::Base; end
class Boolean < ActiveRecord::Base; end
class BasicsTest < ActiveRecord::TestCase
......@@ -477,6 +479,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)
......
......@@ -690,6 +690,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
# fk_test_has_fk should be before fk_test_has_pk
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册