Module: CacheCow::CachedIdList::ClassMethods

Defined in:
lib/cache_cow/cached_id_list.rb

Instance Method Summary collapse

Instance Method Details

#cached_id_list(association_name, options = {}) ⇒ Object

For example:

cached_id_list :groups, :accessor => :member_of?


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cache_cow/cached_id_list.rb', line 9

def cached_id_list(association_name, options = {})
  accessor_name = options[:accessor]
  cache_key     = cached_id_list_cache_key(association_name)

  define_method(cache_key) do
    fetch_cache cache_key, :expires_in => 1.month do
      send(association_name).select("id").map(&:id)
    end
  end

  # if accessor_name
  #   self.class_eval <<-RUBY
  #     def #{accessor_name}(model_or_id)
  #       return false if model_or_id.nil?
  #       model_id = model_or_id.is_a?(Fixnum) ? model_or_id : model_or_id.id
  #       #{cache_key}.include?(model_id)
  #     end
  #   RUBY
  # end
end

#cached_id_list_cache_key(association_name) ⇒ Object



30
31
32
# File 'lib/cache_cow/cached_id_list.rb', line 30

def cached_id_list_cache_key(association_name)
  "cached_" + association_name.to_s.singularize + "_ids"
end