aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
diff options
context:
space:
mode:
authorFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-09-21 22:47:08 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-09-21 22:47:37 -0500
commitc57064a8ab22906cc3f5b4cc6845ac5e1e1a384d (patch)
tree741ee0dc233f62a33288324e80802adc69036399 /activerecord/lib/active_record/attribute_methods
parentfe78e1dad6af88a08715abcad0e6e4e2d5c9b262 (diff)
downloadrails-c57064a8ab22906cc3f5b4cc6845ac5e1e1a384d.tar.gz
rails-c57064a8ab22906cc3f5b4cc6845ac5e1e1a384d.tar.bz2
rails-c57064a8ab22906cc3f5b4cc6845ac5e1e1a384d.zip
update AR/attribute_methods documentation [ci skip]
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r--activerecord/lib/active_record/attribute_methods/primary_key.rb26
-rw-r--r--activerecord/lib/active_record/attribute_methods/read.rb12
-rw-r--r--activerecord/lib/active_record/attribute_methods/serialization.rb19
-rw-r--r--activerecord/lib/active_record/attribute_methods/write.rb5
4 files changed, 36 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb
index aa6704d5c9..0857b02c8e 100644
--- a/activerecord/lib/active_record/attribute_methods/primary_key.rb
+++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb
@@ -5,28 +5,29 @@ module ActiveRecord
module PrimaryKey
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
key = self.id
[key] if key
end
- # Returns the primary key value
+ # Returns the primary key value.
def id
read_attribute(self.class.primary_key)
end
- # Sets the primary key value
+ # Sets the primary key value.
def id=(value)
write_attribute(self.class.primary_key, value) if self.class.primary_key
end
- # Queries the primary key value
+ # Queries the primary key value.
def id?
query_attribute(self.class.primary_key)
end
- # Returns the primary key value before type cast
+ # Returns the primary key value before type cast.
def id_before_type_cast
read_attribute_before_type_cast(self.class.primary_key)
end
@@ -52,14 +53,16 @@ module ActiveRecord
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
- # primary_key_prefix_type setting, though.
+ # Defines the primary key field -- can be overridden in subclasses.
+ # Overwriting will negate any effect of the +primary_key_prefix_type+
+ # setting, though.
def primary_key
@primary_key = reset_primary_key unless defined? @primary_key
@primary_key
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
@quoted_primary_key ||= connection.quote_column_name(primary_key)
end
@@ -92,16 +95,17 @@ module ActiveRecord
# Sets the name of the primary key column.
#
# class Project < ActiveRecord::Base
- # self.primary_key = "sysid"
+ # self.primary_key = 'sysid'
# end
#
- # You can also define the primary_key method yourself:
+ # You can also define the +primary_key+ method yourself:
#
# class Project < ActiveRecord::Base
# def self.primary_key
- # "foo_" + super
+ # 'foo_' + super
# end
# end
+ #
# Project.primary_key # => "foo_id"
def primary_key=(value)
@primary_key = value && value.to_s
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb
index 1a4cb25dd7..6213b5dcd5 100644
--- a/activerecord/lib/active_record/attribute_methods/read.rb
+++ b/activerecord/lib/active_record/attribute_methods/read.rb
@@ -14,9 +14,10 @@ module ActiveRecord
end
module ClassMethods
- # +cache_attributes+ allows you to declare which converted attribute values should
- # be cached. Usually caching only pays off for attributes with expensive conversion
- # methods, like time related columns (e.g. +created_at+, +updated_at+).
+ # +cache_attributes+ allows you to declare which converted attribute
+ # values should be cached. Usually caching only pays off for attributes
+ # with expensive conversion methods, like time related columns (e.g.
+ # +created_at+, +updated_at+).
def cache_attributes(*attribute_names)
cached_attributes.merge attribute_names.map { |attr| attr.to_s }
end
@@ -65,8 +66,9 @@ module ActiveRecord
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,
- # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
+ # Returns the value of the attribute identified by <tt>attr_name</tt> after
+ # 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)
return unless attr_name
name_sym = attr_name.to_sym
diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb
index bdda5bc009..d2cfcbbaf8 100644
--- a/activerecord/lib/active_record/attribute_methods/serialization.rb
+++ b/activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -4,17 +4,19 @@ module ActiveRecord
extend ActiveSupport::Concern
included do
- # Returns a hash of all the attributes that have been specified for serialization as
- # keys and their class restriction as values.
+ # Returns a hash of all the attributes that have been specified for
+ # serialization as keys and their class restriction as values.
class_attribute :serialized_attributes, instance_accessor: false
self.serialized_attributes = {}
end
module ClassMethods
- # If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object,
- # then specify the name of that attribute using this method and it will be handled automatically.
- # The serialization is done through YAML. If +class_name+ is specified, the serialized object must be of that
- # class on retrieval or SerializationTypeMismatch will be raised.
+ # If you have an attribute that needs to be saved to the database as an
+ # object, and retrieved as the same object, then specify the name of that
+ # attribute using this method and it will be handled automatically. The
+ # 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
#
@@ -22,7 +24,8 @@ module ActiveRecord
# * +class_name+ - Optional, class name that the object type should be equal to.
#
# ==== Example
- # # Serialize a preferences attribute
+ #
+ # # Serialize a preferences attribute.
# class User < ActiveRecord::Base
# serialize :preferences
# end
@@ -60,7 +63,7 @@ module ActiveRecord
end
end
- class Attribute < Struct.new(:coder, :value, :state)
+ class Attribute < Struct.new(:coder, :value, :state) # :nodoc:
def unserialized_value
state == :serialized ? unserialize : value
end
diff --git a/activerecord/lib/active_record/attribute_methods/write.rb b/activerecord/lib/active_record/attribute_methods/write.rb
index 5a39cb0125..6eb9e25fd9 100644
--- a/activerecord/lib/active_record/attribute_methods/write.rb
+++ b/activerecord/lib/active_record/attribute_methods/write.rb
@@ -20,8 +20,9 @@ module ActiveRecord
end
end
- # Updates the attribute identified by <tt>attr_name</tt> with the specified +value+. Empty strings
- # for fixnum and float columns are turned into +nil+.
+ # Updates the attribute identified by <tt>attr_name</tt> with the
+ # specified +value+. Empty strings for fixnum and float columns are
+ # turned into +nil+.
def write_attribute(attr_name, value)
attr_name = attr_name.to_s
attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key