module Cacheable::ClassMethods

Public Class Methods

respond_to?(method_sym, include_private = false) click to toggle source
Calls superclass method
# File lib/model_cache/cacheable.rb, line 75
def self.respond_to?(method_sym, include_private = false)
  if method.to_s =~ /^cached_(.*)$/
    true
  else
    super
  end
end

Public Instance Methods

cache(cache_key, options = {}, &block) click to toggle source
# File lib/model_cache/cacheable.rb, line 56
def cache(cache_key, options = {}, &block)
  cache_key = ActiveSupport::Cache.expand_cache_key (Array(cache_key) + [self, updated_at])
  Rails.cache.fetch cache_key, options, &block
end
method_missing(method, *arguments, &block) click to toggle source
Calls superclass method
# File lib/model_cache/cacheable.rb, line 62
def method_missing(method, *arguments, &block)
  if method.to_s =~ /^cached_(.*)$/
    cache $1 do
      value = send $1.to_sym
      value = value.to_a if value.is_a? ActiveRecord::Relation
      value
    end
  else
    super
  end
end
updated_at(options = {}) click to toggle source
# File lib/model_cache/cacheable.rb, line 41
def updated_at(options = {})
  value = Rails.cache.fetch "#{name}_updated_at"
  if value.blank? || options[:force]
    value = maximum :updated_at
    Rails.cache.write "#{name}_updated_at", value
  end
  value
end
updated_at=(value) click to toggle source
# File lib/model_cache/cacheable.rb, line 51
def updated_at=(value)
  Rails.cache.write "#{name}_updated_at", value
end