mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Move id attribute methods into their related concern
This commit is contained in:
@@ -11,6 +11,18 @@ module ActiveRecord
|
||||
@attributes[attr_name]
|
||||
end
|
||||
|
||||
# Returns a hash of attributes before typecasting and deserialization.
|
||||
def attributes_before_type_cast
|
||||
self.attribute_names.inject({}) do |attrs, name|
|
||||
attrs[name] = read_attribute_before_type_cast(name)
|
||||
attrs
|
||||
end
|
||||
end
|
||||
|
||||
def id_before_type_cast #:nodoc:
|
||||
read_attribute_before_type_cast(self.class.primary_key)
|
||||
end
|
||||
|
||||
private
|
||||
# Handle *_before_type_cast for method_missing.
|
||||
def attribute_before_type_cast(attribute_name)
|
||||
|
||||
@@ -77,6 +77,17 @@ module ActiveRecord
|
||||
end
|
||||
end
|
||||
|
||||
# A model instance's primary key is always available as model.id
|
||||
# whether you name it the default 'id' or set it to something else.
|
||||
def id
|
||||
attr_name = self.class.primary_key
|
||||
column = column_for_attribute(attr_name)
|
||||
|
||||
self.class.send(:define_read_method, :id, attr_name, column)
|
||||
# now that the method exists, call it
|
||||
self.send attr_name.to_sym
|
||||
end
|
||||
|
||||
# Returns true if the attribute is of a text column and marked for serialization.
|
||||
def unserializable_attribute?(attr_name, column)
|
||||
column.text? && self.class.serialized_attributes[attr_name]
|
||||
|
||||
@@ -26,6 +26,11 @@ module ActiveRecord
|
||||
end
|
||||
end
|
||||
|
||||
# Sets the primary ID.
|
||||
def id=(value)
|
||||
write_attribute(self.class.primary_key, value)
|
||||
end
|
||||
|
||||
private
|
||||
# Handle *= for method_missing.
|
||||
def attribute=(attribute_name, value)
|
||||
|
||||
@@ -2508,18 +2508,6 @@ module ActiveRecord #:nodoc:
|
||||
result
|
||||
end
|
||||
|
||||
# A model instance's primary key is always available as model.id
|
||||
# whether you name it the default 'id' or set it to something else.
|
||||
def id
|
||||
attr_name = self.class.primary_key
|
||||
column = column_for_attribute(attr_name)
|
||||
|
||||
self.class.send(:define_read_method, :id, attr_name, column)
|
||||
# now that the method exists, call it
|
||||
self.send attr_name.to_sym
|
||||
|
||||
end
|
||||
|
||||
# Returns a String, which Action Pack uses for constructing an URL to this
|
||||
# object. The default implementation returns this record's id as a String,
|
||||
# or nil if this record's unsaved.
|
||||
@@ -2565,19 +2553,10 @@ module ActiveRecord #:nodoc:
|
||||
end
|
||||
end
|
||||
|
||||
def id_before_type_cast #:nodoc:
|
||||
read_attribute_before_type_cast(self.class.primary_key)
|
||||
end
|
||||
|
||||
def quoted_id #:nodoc:
|
||||
quote_value(id, column_for_attribute(self.class.primary_key))
|
||||
end
|
||||
|
||||
# Sets the primary ID.
|
||||
def id=(value)
|
||||
write_attribute(self.class.primary_key, value)
|
||||
end
|
||||
|
||||
# Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet; otherwise, returns false.
|
||||
def new_record?
|
||||
@new_record || false
|
||||
@@ -2822,14 +2801,6 @@ module ActiveRecord #:nodoc:
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a hash of attributes before typecasting and deserialization.
|
||||
def attributes_before_type_cast
|
||||
self.attribute_names.inject({}) do |attrs, name|
|
||||
attrs[name] = read_attribute_before_type_cast(name)
|
||||
attrs
|
||||
end
|
||||
end
|
||||
|
||||
# Returns an <tt>#inspect</tt>-like string for the value of the
|
||||
# attribute +attr_name+. String attributes are elided after 50
|
||||
# characters, and Date and Time attributes are returned in the
|
||||
|
||||
Reference in New Issue
Block a user