module RestrictCache::RailsExt::ActiveRecord::Relation

Public Instance Methods

find_and_restrict_cache(*args) click to toggle source
# File lib/restrict_cache/rails_ext/active_record/relation.rb, line 5
def find_and_restrict_cache(*args)
  records = find(*args)
  Array(records).each(&:restrict_cache)
  records
end
find_from_restrict_cache(*args) click to toggle source
# File lib/restrict_cache/rails_ext/active_record/relation.rb, line 11
def find_from_restrict_cache(*args)
  return nil unless cached_contents

  case args.first
  when Integer
    cached_contents[args.first]
  when String
    cached_contents[args.first.to_i]
  when Array
    args.first.map {|index| cached_contents[index.to_i] }
  else
    raise "unknown argument: #{args.inspect}"
  end
end
find_with_rc(*args, &block)
find_with_restrict_cache(*args, &block) click to toggle source
# File lib/restrict_cache/rails_ext/active_record/relation.rb, line 26
def find_with_restrict_cache(*args, &block)
  if restrict_cached?(*args)
    records = find_from_restrict_cache(*args)
  else
    records = find_and_restrict_cache(*args)
  end

  block_given? ? records.each {|record| block.call(record) } : records
end
Also aliased as: find_with_rc
with_restrict_cache() click to toggle source
# File lib/restrict_cache/rails_ext/active_record/relation.rb, line 37
def with_restrict_cache
  self.each(&:restrict_cache) && self
end

Private Instance Methods

cached_contents() click to toggle source
# File lib/restrict_cache/rails_ext/active_record/relation.rb, line 42
def cached_contents
  RestrictCache.send(CacheCollection::CacheKey::ACTIVERECORD).contents(self.table_name)
end
restrict_cached?(*args) click to toggle source
# File lib/restrict_cache/rails_ext/active_record/relation.rb, line 46
def restrict_cached?(*args)
  return false unless cached_contents
  args.all? {|index| cached_contents.include?(index) }
end