module ActiveRecord::AttributeMethods::Read::ClassMethods

Public Instance Methods

cache_attribute?(attr_name) click to toggle source

Returns true if the provided attribute is being cached.

# File lib/active_record/attribute_methods/read.rb, line 61
def cache_attribute?(attr_name)
  cached_attributes.include?(attr_name)
end
cache_attributes(*attribute_names) click to toggle source

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).

# File lib/active_record/attribute_methods/read.rb, line 50
def cache_attributes(*attribute_names)
  cached_attributes.merge attribute_names.map { |attr| attr.to_s }
end
cached_attributes() click to toggle source

Returns the attributes which are cached. By default time related columns with datatype :datetime, :timestamp, :time, :date are cached.

# File lib/active_record/attribute_methods/read.rb, line 56
def cached_attributes
  @cached_attributes ||= columns.select { |c| cacheable_column?(c) }.map { |col| col.name }.to_set
end

Protected Instance Methods

define_method_attribute(name) click to toggle source
# File lib/active_record/attribute_methods/read.rb, line 68
def define_method_attribute(name)
  method = ReaderMethodCache[name]
  generated_attribute_methods.module_eval { define_method name, method }
end

Private Instance Methods

cacheable_column?(column) click to toggle source
# File lib/active_record/attribute_methods/read.rb, line 95
def cacheable_column?(column)
  if attribute_types_cached_by_default == ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
    ! serialized_attributes.include? column.name
  else
    attribute_types_cached_by_default.include?(column.type)
  end
end