From c57064a8ab22906cc3f5b4cc6845ac5e1e1a384d Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Fri, 21 Sep 2012 22:47:08 -0500 Subject: [PATCH] update AR/attribute_methods documentation [ci skip] --- .../attribute_methods/primary_key.rb | 26 +++++++++++-------- .../active_record/attribute_methods/read.rb | 12 +++++---- .../attribute_methods/serialization.rb | 19 ++++++++------ .../active_record/attribute_methods/write.rb | 5 ++-- 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 AttributeMethods 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 @@ def dangerous_attribute_method?(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 - # 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 @@ def get_primary_key(base_name) #:nodoc: # 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 Read 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 @@ def cacheable_column?(column) ActiveRecord::Model.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT - # Returns the value of the attribute identified by attr_name 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 attr_name 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 Serialization 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 + # SerializationTypeMismatch will be raised. # # ==== Parameters # @@ -22,7 +24,8 @@ module ClassMethods # * +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 @@ def type 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 @@ def define_method_attribute=(attr_name) end end - # Updates the attribute identified by attr_name with the specified +value+. Empty strings - # for fixnum and float columns are turned into +nil+. + # Updates the attribute identified by attr_name 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 -- GitLab