module KaminariCache::ClassMethods
Public Instance Methods
fetch_page(options = {})
click to toggle source
# File lib/kaminari-cache.rb, line 38 def fetch_page(options = {}) # Default options options.reverse_merge!( :page => 1, :per => Kaminari.config.max_per_page || Kaminari.config.default_per_page ) key = cache_key(options) entries = Rails.cache.fetch(key) do Rails.logger.info "Cache miss: #{key.to_s}" scope = self.send(options[:scope]) if options[:scope] scope = (scope || self).page(options[:page]).per(options[:per]) scope = apply_locale(scope, options[:locale]) if options[:locale] scope = apply_includes(scope, options[:includes]) if options[:includes] scope = apply_sort(scope, options[:order]) if options[:order] Kaminari::PaginatableArray.new(scope.to_a, :limit => scope.limit_value, :offset => scope.offset_value, :total_count => scope.total_count ) end end
Private Instance Methods
apply_includes(scope, includes)
click to toggle source
# File lib/kaminari-cache.rb, line 108 def apply_includes(scope, includes) includes.each do |i| scope = scope.includes(i) end scope end
apply_locale(scope, locale)
click to toggle source
# File lib/kaminari-cache.rb, line 104 def apply_locale(scope, locale) scope = scope.with_translations(locale) if scope.respond_to? :with_translations scope end
apply_sort(scope, order)
click to toggle source
# File lib/kaminari-cache.rb, line 87 def apply_sort(scope, order) case order when Symbol scope.order! order when String scope.order! order when Hash order.each do |k, v| if v.empty? scope.order! k else scope.order! "#{k.to_s} #{v.to_s}" end end end scope end
cache_key(options)
click to toggle source
# File lib/kaminari-cache.rb, line 61 def cache_key(options) key = [:kaminari, self.name.pluralize.downcase] key << options[:page] key << options[:per] key << [:locale, options[:locale]] if options[:locale] key << [:order, options[:order]] if options[:order] key << [:scope, options[:scope]] if options[:scope] key << [:includes, options[:includes]] if options[:includes] expanded_key(key) end
expanded_key(key)
click to toggle source
# File lib/kaminari-cache.rb, line 72 def expanded_key(key) return key.cache_key.to_s if key.respond_to?(:cache_key) case key when Array if key.size > 1 key = key.collect{|element| expanded_key(element)} else key = key.first end when Hash key = key.sort_by { |k,_| k.to_s }.collect{|k,v| "#{k}=#{v}"} end key.to_param end