Module: RestrictCache::RailsExt::ActiveRecord::Relation
- Defined in:
- lib/restrict_cache/rails_ext/active_record/relation.rb
Instance Method Summary collapse
- #find_and_restrict_cache(*args) ⇒ Object
- #find_from_restrict_cache(*args) ⇒ Object
- #find_with_restrict_cache(*args, &block) ⇒ Object (also: #find_with_rc)
- #with_restrict_cache ⇒ Object
Instance Method Details
#find_and_restrict_cache(*args) ⇒ Object
5 6 7 8 9 |
# 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) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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_restrict_cache(*args, &block) ⇒ Object Also known as: find_with_rc
26 27 28 29 30 31 32 33 34 |
# 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 |
#with_restrict_cache ⇒ Object
37 38 39 |
# File 'lib/restrict_cache/rails_ext/active_record/relation.rb', line 37 def with_restrict_cache self.each(&:restrict_cache) && self end |