1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

move id_before_type_cast to PrimaryKey module

This commit is contained in:
Sergey Nartimov 2012-02-16 19:49:38 +03:00
parent a5874dd118
commit ec4759d205
3 changed files with 19 additions and 6 deletions

View file

@ -20,11 +20,7 @@ module ActiveRecord
# Handle *_before_type_cast for method_missing.
def attribute_before_type_cast(attribute_name)
if attribute_name == 'id'
read_attribute_before_type_cast(self.class.primary_key)
else
read_attribute_before_type_cast(attribute_name)
end
read_attribute_before_type_cast(attribute_name)
end
end
end

View file

@ -1,3 +1,5 @@
require 'set'
module ActiveRecord
module AttributeMethods
module PrimaryKey
@ -24,6 +26,11 @@ module ActiveRecord
query_attribute(self.class.primary_key)
end
# Returns the primary key value before type cast
def id_before_type_cast
read_attribute_before_type_cast(self.class.primary_key)
end
protected
def attribute_method?(attr_name)
@ -45,8 +52,10 @@ module ActiveRecord
end
end
ID_ATTRIBUTE_METHODS = %w(id id= id? id_before_type_cast).to_set
def dangerous_attribute_method?(method_name)
super && !['id', 'id=', 'id?'].include?(method_name)
super && !ID_ATTRIBUTE_METHODS.include?(method_name)
end
# Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the

View file

@ -121,6 +121,14 @@ class AttributeMethodsTest < ActiveRecord::TestCase
assert keyboard.respond_to?('id')
end
def test_id_before_type_cast_with_custom_primary_key
keyboard = Keyboard.create
keyboard.key_number = '10'
assert_equal '10', keyboard.id_before_type_cast
assert_equal nil, keyboard.read_attribute_before_type_cast('id')
assert_equal '10', keyboard.read_attribute_before_type_cast('key_number')
end
# Syck calls respond_to? before actually calling initialize
def test_respond_to_with_allocated_object
topic = Topic.allocate