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.
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/active_record/attribute_methods.rb', line 138 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"'
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/active_record/attribute_methods.rb', line 199 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
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/active_record/attribute_methods.rb', line 152 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.
177 178 179 |
# File 'lib/active_record/attribute_methods.rb', line 177 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).
213 214 215 216 |
# File 'lib/active_record/attribute_methods.rb', line 213 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.
182 183 184 |
# File 'lib/active_record/attribute_methods.rb', line 182 def attributes Hash[@attributes.map { |name, _| [name, read_attribute(name)] }] end |
#column_for_attribute(name) ⇒ Object
Returns the column object for the named attribute.
219 220 221 |
# File 'lib/active_record/attribute_methods.rb', line 219 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
172 173 174 |
# File 'lib/active_record/attribute_methods.rb', line 172 def has_attribute?(attr_name) @attributes.has_key?(attr_name.to_s) end |
#respond_to?(name, include_private = false) ⇒ Boolean
166 167 168 169 |
# File 'lib/active_record/attribute_methods.rb', line 166 def respond_to?(name, include_private = false) self.class.define_attribute_methods unless self.class.attribute_methods_generated? super end |