Module: ActiveRecord::LawnchairExtension::ClassMethods

Defined in:
lib/active_record_extension.rb

Instance Method Summary collapse

Instance Method Details

#lawnchair_cache(method, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_record_extension.rb', line 11

def lawnchair_cache(method, options={})
  self.class_eval %{
    def #{method}_with_lawnchair(*args)
      ident = lambda { |obj| obj.class.respond_to?(:primary_key) ? obj.send(obj.class.primary_key) : obj.to_s }
      arg_keys = args.map(&ident).join(':')
      key = "#\{self.class.name\}:#{method}:#\{ident.call(self)\}:#\{arg_keys\}"
      Lawnchair.cache(key, #{options.inspect}) do
        self.#{method}_without_lawnchair(*args)
      end
    end
  }

  alias_method_chain method, :lawnchair
end