提交 e9fb6d04 编写于 作者: J Jon Leighton

Add test for read_attribute(:id) with non-standard PK.

Also make it actually work.

It slows down all read_attribute accesses to map 'id' to whatever the PK
actually is, inside read_attribute. So instead make sure the necessary
methods are defined and that they redirect wherever they need to go.
上级 f1a534af
...@@ -25,6 +25,20 @@ def id? ...@@ -25,6 +25,20 @@ def id?
end end
module ClassMethods module ClassMethods
def define_method_attribute(attr_name)
super
if attr_name == primary_key && attr_name != 'id'
generated_attribute_methods.send(:alias_method, :id, primary_key)
generated_attribute_methods.module_eval <<-CODE, __FILE__, __LINE__
def self.attribute_id(v, attributes, attributes_cache, attr_name)
attr_name = '#{primary_key}'
send(:'attribute_#{attr_name}', attributes[attr_name], attributes, attributes_cache, attr_name)
end
CODE
end
end
def dangerous_attribute_method?(method_name) def dangerous_attribute_method?(method_name)
super && !['id', 'id=', 'id?'].include?(method_name) super && !['id', 'id=', 'id?'].include?(method_name)
end end
......
...@@ -58,7 +58,7 @@ def #{attr_name} ...@@ -58,7 +58,7 @@ def #{attr_name}
#{internal} #{internal}
end end
def self.attribute_#{attr_name}(v, attributes_cache, attr_name) def self.attribute_#{attr_name}(v, attributes, attributes_cache, attr_name)
#{external} #{external}
end end
STR STR
...@@ -68,7 +68,7 @@ def self.attribute_#{attr_name}(v, attributes_cache, attr_name) ...@@ -68,7 +68,7 @@ def self.attribute_#{attr_name}(v, attributes_cache, attr_name)
eval(internal) eval(internal)
end end
singleton_class.send(:define_method, "attribute_#{attr_name}") do |v, attributes_cache, attr_name| singleton_class.send(:define_method, "attribute_#{attr_name}") do |v, attributes, attributes_cache, attr_name|
eval(external) eval(external)
end end
end end
...@@ -117,8 +117,8 @@ def read_attribute(attr_name) ...@@ -117,8 +117,8 @@ def read_attribute(attr_name)
methods = self.class.generated_attribute_methods methods = self.class.generated_attribute_methods
if methods.respond_to?(accessor) if methods.respond_to?(accessor)
if @attributes.has_key?(attr_name) if @attributes.has_key?(attr_name) || attr_name == 'id'
methods.send(accessor, @attributes[attr_name], @attributes_cache, attr_name) methods.send(accessor, @attributes[attr_name], @attributes, @attributes_cache, attr_name)
end end
elsif !self.class.attribute_methods_generated? elsif !self.class.attribute_methods_generated?
# If we haven't generated the caster methods yet, do that and # If we haven't generated the caster methods yet, do that and
......
...@@ -23,6 +23,11 @@ def test_to_key_with_customized_primary_key ...@@ -23,6 +23,11 @@ def test_to_key_with_customized_primary_key
assert_equal keyboard.to_key, [keyboard.id] assert_equal keyboard.to_key, [keyboard.id]
end end
def test_read_attribute_with_custom_primary_key
keyboard = Keyboard.create!
assert_equal keyboard.key_number, keyboard.read_attribute(:id)
end
def test_to_key_with_primary_key_after_destroy def test_to_key_with_primary_key_after_destroy
topic = Topic.find(1) topic = Topic.find(1)
topic.destroy topic.destroy
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册