提交 c57064a8 编写于 作者: F Francesco Rodriguez

update AR/attribute_methods documentation [ci skip]

上级 fe78e1da
...@@ -5,28 +5,29 @@ module AttributeMethods ...@@ -5,28 +5,29 @@ module AttributeMethods
module PrimaryKey module PrimaryKey
extend ActiveSupport::Concern extend ActiveSupport::Concern
# Returns this record's primary key value wrapped in an Array if one is available # Returns this record's primary key value wrapped in an Array if one is
# available.
def to_key def to_key
key = self.id key = self.id
[key] if key [key] if key
end end
# Returns the primary key value # Returns the primary key value.
def id def id
read_attribute(self.class.primary_key) read_attribute(self.class.primary_key)
end end
# Sets the primary key value # Sets the primary key value.
def id=(value) def id=(value)
write_attribute(self.class.primary_key, value) if self.class.primary_key write_attribute(self.class.primary_key, value) if self.class.primary_key
end end
# Queries the primary key value # Queries the primary key value.
def id? def id?
query_attribute(self.class.primary_key) query_attribute(self.class.primary_key)
end end
# Returns the primary key value before type cast # Returns the primary key value before type cast.
def id_before_type_cast def id_before_type_cast
read_attribute_before_type_cast(self.class.primary_key) read_attribute_before_type_cast(self.class.primary_key)
end end
...@@ -52,14 +53,16 @@ def dangerous_attribute_method?(method_name) ...@@ -52,14 +53,16 @@ def dangerous_attribute_method?(method_name)
super && !ID_ATTRIBUTE_METHODS.include?(method_name) super && !ID_ATTRIBUTE_METHODS.include?(method_name)
end end
# Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the # Defines the primary key field -- can be overridden in subclasses.
# primary_key_prefix_type setting, though. # Overwriting will negate any effect of the +primary_key_prefix_type+
# setting, though.
def primary_key def primary_key
@primary_key = reset_primary_key unless defined? @primary_key @primary_key = reset_primary_key unless defined? @primary_key
@primary_key @primary_key
end end
# Returns a quoted version of the primary key name, used to construct SQL statements. # Returns a quoted version of the primary key name, used to construct
# SQL statements.
def quoted_primary_key def quoted_primary_key
@quoted_primary_key ||= connection.quote_column_name(primary_key) @quoted_primary_key ||= connection.quote_column_name(primary_key)
end end
...@@ -92,16 +95,17 @@ def get_primary_key(base_name) #:nodoc: ...@@ -92,16 +95,17 @@ def get_primary_key(base_name) #:nodoc:
# Sets the name of the primary key column. # Sets the name of the primary key column.
# #
# class Project < ActiveRecord::Base # class Project < ActiveRecord::Base
# self.primary_key = "sysid" # self.primary_key = 'sysid'
# end # end
# #
# You can also define the primary_key method yourself: # You can also define the +primary_key+ method yourself:
# #
# class Project < ActiveRecord::Base # class Project < ActiveRecord::Base
# def self.primary_key # def self.primary_key
# "foo_" + super # 'foo_' + super
# end # end
# end # end
#
# Project.primary_key # => "foo_id" # Project.primary_key # => "foo_id"
def primary_key=(value) def primary_key=(value)
@primary_key = value && value.to_s @primary_key = value && value.to_s
......
...@@ -14,9 +14,10 @@ module Read ...@@ -14,9 +14,10 @@ module Read
end end
module ClassMethods module ClassMethods
# +cache_attributes+ allows you to declare which converted attribute values should # +cache_attributes+ allows you to declare which converted attribute
# be cached. Usually caching only pays off for attributes with expensive conversion # values should be cached. Usually caching only pays off for attributes
# methods, like time related columns (e.g. +created_at+, +updated_at+). # with expensive conversion methods, like time related columns (e.g.
# +created_at+, +updated_at+).
def cache_attributes(*attribute_names) def cache_attributes(*attribute_names)
cached_attributes.merge attribute_names.map { |attr| attr.to_s } cached_attributes.merge attribute_names.map { |attr| attr.to_s }
end end
...@@ -65,8 +66,9 @@ def cacheable_column?(column) ...@@ -65,8 +66,9 @@ def cacheable_column?(column)
ActiveRecord::Model.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT ActiveRecord::Model.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example, # Returns the value of the attribute identified by <tt>attr_name</tt> after
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). # it has been typecast (for example, "2004-12-12" in a data column is cast
# to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name) def read_attribute(attr_name)
return unless attr_name return unless attr_name
name_sym = attr_name.to_sym name_sym = attr_name.to_sym
......
...@@ -4,17 +4,19 @@ module Serialization ...@@ -4,17 +4,19 @@ module Serialization
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
# Returns a hash of all the attributes that have been specified for serialization as # Returns a hash of all the attributes that have been specified for
# keys and their class restriction as values. # serialization as keys and their class restriction as values.
class_attribute :serialized_attributes, instance_accessor: false class_attribute :serialized_attributes, instance_accessor: false
self.serialized_attributes = {} self.serialized_attributes = {}
end end
module ClassMethods module ClassMethods
# If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object, # If you have an attribute that needs to be saved to the database as an
# then specify the name of that attribute using this method and it will be handled automatically. # object, and retrieved as the same object, then specify the name of that
# The serialization is done through YAML. If +class_name+ is specified, the serialized object must be of that # attribute using this method and it will be handled automatically. The
# class on retrieval or SerializationTypeMismatch will be raised. # serialization is done through YAML. If +class_name+ is specified, the
# serialized object must be of that class on retrieval or
# <tt>SerializationTypeMismatch</tt> will be raised.
# #
# ==== Parameters # ==== Parameters
# #
...@@ -22,7 +24,8 @@ module ClassMethods ...@@ -22,7 +24,8 @@ module ClassMethods
# * +class_name+ - Optional, class name that the object type should be equal to. # * +class_name+ - Optional, class name that the object type should be equal to.
# #
# ==== Example # ==== Example
# # Serialize a preferences attribute #
# # Serialize a preferences attribute.
# class User < ActiveRecord::Base # class User < ActiveRecord::Base
# serialize :preferences # serialize :preferences
# end # end
...@@ -60,7 +63,7 @@ def type ...@@ -60,7 +63,7 @@ def type
end end
end end
class Attribute < Struct.new(:coder, :value, :state) class Attribute < Struct.new(:coder, :value, :state) # :nodoc:
def unserialized_value def unserialized_value
state == :serialized ? unserialize : value state == :serialized ? unserialize : value
end end
......
...@@ -20,8 +20,9 @@ def define_method_attribute=(attr_name) ...@@ -20,8 +20,9 @@ def define_method_attribute=(attr_name)
end end
end end
# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+. Empty strings # Updates the attribute identified by <tt>attr_name</tt> with the
# for fixnum and float columns are turned into +nil+. # specified +value+. Empty strings for fixnum and float columns are
# turned into +nil+.
def write_attribute(attr_name, value) def write_attribute(attr_name, value)
attr_name = attr_name.to_s attr_name = attr_name.to_s
attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册