Module: HoboCacheHelper

Defined in:
app/helpers/hobo_cache_helper.rb

Instance Method Summary collapse

Instance Method Details

#hobo_cache_key(namespace = :views, route_on = nil, query_params = nil, attributes = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/hobo_cache_helper.rb', line 2

def hobo_cache_key(namespace=:views, route_on=nil, query_params=nil, attributes=nil)
  attributes ||= {}

  if route_on == true
    route_on = this
  end

  if route_on.is_a?(ActiveRecord::Base)
    route_on = url_for(route_on)
  end

  if route_on
    attributes.reverse_merge!(Rails.application.routes.recognize_path(route_on))
  elsif params[:page_path]
    # it's quite possible that our page was rendered by a different action, so normalize
    attributes.reverse_merge!(Rails.application.routes.recognize_path(params[:page_path]))
  end

  key_attrs = attributes
  key_attrs[:only_path] = false
  comma_split(query_params || "").each do |qp|
    key_attrs["#{qp}"] = params[qp] || ""
  end

  key = ActiveSupport::Cache.expand_cache_key(url_for(key_attrs).split('://').last, namespace)
  Digest::MD5.hexdigest(key)
end

#item_cache(*args, &block) ⇒ Object



30
31
32
33
34
35
36
37
# File 'app/helpers/hobo_cache_helper.rb', line 30

def item_cache(*args, &block)
  unless Rails.configuration.action_controller.perform_caching
    yield if block_given?
  else
    key = hobo_cache_key(:item, *args)
    Rails.cache.fetch(key, &block)
  end
end