Module: ActiveModelCachers::ActiveRecord::Extension

Defined in:
lib/active_model_cachers/patches/patch_rails_3.rb,
lib/active_model_cachers/active_record/extension.rb

Instance Method Summary collapse

Instance Method Details

#cache_at(column, query = nil, expire_by: nil, on: nil, foreign_key: nil, primary_key: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_model_cachers/active_record/extension.rb', line 16

def cache_at(column, query = nil, expire_by: nil, on: nil, foreign_key: nil, primary_key: nil)
  attr = AttrModel.new(self, column, foreign_key: foreign_key, primary_key: primary_key)
  return cache_belongs_to(attr) if attr.belongs_to?

  loaded = false
  class_name, *infos = get_expire_infos(attr, expire_by, foreign_key)
  set_klass_to_mapping(attr, class_name) do
    next if !loaded
    cache_at(column, query, expire_by: expire_by, on: on, foreign_key: foreign_key, primary_key: primary_key)
  end
  loaded = true

  query ||= ->(id){ attr.query_model(self, id) }
  service_klass = CacheServiceFactory.create_for_active_model(attr, query)
  Cacher.define_cacher_method(attr, attr.primary_key || :id, [service_klass])

  if class_name
    with_id = (expire_by.is_a?(Symbol) || query.parameters.size == 1)
    service_klass.define_callback_for_cleaning_cache(class_name, *infos, with_id, on: on)
  end

  return service_klass
end

#cache_self(by: :id) ⇒ Object



12
13
14
# File 'lib/active_model_cachers/active_record/extension.rb', line 12

def cache_self(by: :id)
  cache_at(nil, expire_by: self.name, primary_key: by, foreign_key: by)
end

#find_by(*args) ⇒ Object

define #find_by for Rails 3



6
7
8
# File 'lib/active_model_cachers/patches/patch_rails_3.rb', line 6

def find_by(*args)
  where(*args).order('').first
end

#has_cacher?(column = nil) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/active_model_cachers/active_record/extension.rb', line 40

def has_cacher?(column = nil)
  attr = AttrModel.new(self, column)
  return CacheServiceFactory.has_cacher?(attr)
end