Module: RestrictCache::ActiveRecordExt::Relation

Defined in:
lib/restrict_cache/active_record_ext.rb

Constant Summary collapse

AR_CACHE_KEY =
Cacheable::CacheKey::ACTIVERECORD

Instance Method Summary collapse

Instance Method Details

#find_and_restrict_cache(arg) ⇒ Object



18
19
20
21
22
# File 'lib/restrict_cache/active_record_ext.rb', line 18

def find_and_restrict_cache(arg)
  records = find(arg)
  Array(records).each(&:restrict_cache)
  records
end

#find_from_restrict_cache(arg) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/restrict_cache/active_record_ext.rb', line 24

def find_from_restrict_cache(arg)
  contents = RestrictCache.send(AR_CACHE_KEY).contents(self.table_name)
  return nil unless contents

  case arg
  when Integer
    contents[arg]
  when String
    contents[arg.to_i]
  when Array
    args.map {|index| contents[index.to_i] }
  else
    raise "unknown argument: #{arg.inspect}"
  end
end

#find_with_restrict_cache(arg) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/restrict_cache/active_record_ext.rb', line 40

def find_with_restrict_cache(arg)
  if restrict_cached?(Array(arg))
    records = Array(arg).map {|index| find_from_restrict_cache(index) }
    records.size > 1 ? records : records.first
  else
    find_and_restrict_cache(arg)
  end
end

#with_restrict_cacheObject



49
50
51
52
# File 'lib/restrict_cache/active_record_ext.rb', line 49

def with_restrict_cache
  self.each(&:restrict_cache)
  self
end