Module: ActiveRecord::AttributeMethods
- Extended by:
- ActiveSupport::Autoload, ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeMethods
- Included in:
- Base
- Defined in:
- lib/active_record/attribute_methods.rb,
lib/active_record.rb,
lib/active_record/attribute_methods/read.rb,
lib/active_record/attribute_methods/dirty.rb,
lib/active_record/attribute_methods/query.rb,
lib/active_record/attribute_methods/write.rb,
lib/active_record/attribute_methods/primary_key.rb,
lib/active_record/attribute_methods/serialization.rb,
lib/active_record/attribute_methods/before_type_cast.rb,
lib/active_record/attribute_methods/time_zone_conversion.rb,
lib/active_record/attribute_methods/deprecated_underscore_read.rb
Overview
Active Record Attribute Methods
Defined Under Namespace
Modules: BeforeTypeCast, ClassMethods, DeprecatedUnderscoreRead, Dirty, PrimaryKey, Query, Read, Serialization, TimeZoneConversion, Write
Instance Method Summary collapse
-
#attribute_for_inspect(attr_name) ⇒ Object
Returns an
#inspect
-like string for the value of the attributeattr_name
. - #attribute_missing(match, *args, &block) ⇒ Object
-
#attribute_names ⇒ Object
Returns an array of names for the attributes available on this object.
-
#attribute_present?(attribute) ⇒ Boolean
Returns true if the specified
attribute
has been set by the user or by a database load and is neither nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings). -
#attributes ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
-
#column_for_attribute(name) ⇒ Object
Returns the column object for the named attribute.
-
#has_attribute?(attr_name) ⇒ Boolean
Returns true if the given attribute is in the attributes hash.
-
#method_missing(method, *args, &block) ⇒ Object
If we haven’t generated any methods yet, generate them, then see if we’ve created the method we’re looking for.
- #respond_to?(name, include_private = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
If we haven’t generated any methods yet, generate them, then see if we’ve created the method we’re looking for.
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_record/attribute_methods.rb', line 116 def method_missing(method, *args, &block) unless self.class.attribute_methods_generated? self.class.define_attribute_methods if respond_to_without_attributes?(method) send(method, *args, &block) else super end else super end end |
Instance Method Details
#attribute_for_inspect(attr_name) ⇒ Object
Returns an #inspect
-like string for the value of the attribute attr_name
. String attributes are truncated upto 50 characters, and Date and Time attributes are returned in the :db
format. Other attributes return the value of #inspect
without modification.
person = Person.create!(:name => "David Heinemeier Hansson " * 3)
person.attribute_for_inspect(:name)
# => '"David Heinemeier Hansson David Heinemeier Hansson D..."'
person.attribute_for_inspect(:created_at)
# => '"2009-01-12 04:48:57"'
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/active_record/attribute_methods.rb', line 177 def attribute_for_inspect(attr_name) value = read_attribute(attr_name) if value.is_a?(String) && value.length > 50 "#{value[0..50]}...".inspect elsif value.is_a?(Date) || value.is_a?(Time) %("#{value.to_s(:db)}") else value.inspect end end |
#attribute_missing(match, *args, &block) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/active_record/attribute_methods.rb', line 130 def attribute_missing(match, *args, &block) if self.class.columns_hash[match.attr_name] ActiveSupport::Deprecation.warn( "The method `#{match.method_name}', matching the attribute `#{match.attr_name}' has " \ "dispatched through method_missing. This shouldn't happen, because `#{match.attr_name}' " \ "is a column of the table. If this error has happened through normal usage of Active " \ "Record (rather than through your own code or external libraries), please report it as " \ "a bug." ) end super end |
#attribute_names ⇒ Object
Returns an array of names for the attributes available on this object.
155 156 157 |
# File 'lib/active_record/attribute_methods.rb', line 155 def attribute_names @attributes.keys end |
#attribute_present?(attribute) ⇒ Boolean
Returns true if the specified attribute
has been set by the user or by a database load and is neither nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings).
191 192 193 194 |
# File 'lib/active_record/attribute_methods.rb', line 191 def attribute_present?(attribute) value = read_attribute(attribute) !value.nil? || (value.respond_to?(:empty?) && !value.empty?) end |
#attributes ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
160 161 162 |
# File 'lib/active_record/attribute_methods.rb', line 160 def attributes Hash[@attributes.map { |name, _| [name, read_attribute(name)] }] end |
#column_for_attribute(name) ⇒ Object
Returns the column object for the named attribute.
197 198 199 |
# File 'lib/active_record/attribute_methods.rb', line 197 def column_for_attribute(name) self.class.columns_hash[name.to_s] end |
#has_attribute?(attr_name) ⇒ Boolean
Returns true if the given attribute is in the attributes hash
150 151 152 |
# File 'lib/active_record/attribute_methods.rb', line 150 def has_attribute?(attr_name) @attributes.has_key?(attr_name.to_s) end |
#respond_to?(name, include_private = false) ⇒ Boolean
144 145 146 147 |
# File 'lib/active_record/attribute_methods.rb', line 144 def respond_to?(name, include_private = false) self.class.define_attribute_methods unless self.class.attribute_methods_generated? super end |