Module: PageletRails::Concerns::Cache

Extended by:
ActiveSupport::Concern
Defined in:
lib/pagelet_rails/concerns/cache.rb

Instance Method Summary collapse

Instance Method Details

#pagelet_cache(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pagelet_rails/concerns/cache.rb', line 15

def pagelet_cache &block
  # puts 'pagelet_cache'.blue
  cache_enabled = pagelet_options.cache || pagelet_options.cache_path || pagelet_options.expires_in

  if !cache_enabled
    return yield
  end

  cache_defaults = (pagelet_options.cache_defaults || {}).to_h.symbolize_keys
  store_options = cache_defaults.except(:cache_path)
  store_options[:expires_in] = pagelet_options.expires_in if pagelet_options.expires_in

  cache_path = pagelet_options.cache_path || cache_defaults[:cache_path]

  cache_path =
    if cache_path.is_a?(Proc)
      self.instance_exec(self, &cache_path)
    elsif cache_path.respond_to?(:call)
      cache_path.call(self)
    elsif cache_path.is_a?(String)
      {
        custom: cache_path
      }
    else
      cache_path
    end

  cache_path ||= {}
  cache_path[:controller] = params[:controller]
  cache_path[:action] = params[:action]

  path_object = ActionController::Caching::Actions::ActionCachePath.new(self, cache_path)
  has_cache = fragment_exist?(path_object.path, store_options)
  pagelet_options has_cache: has_cache


  if (pagelet_render_remotely? && has_cache) || !pagelet_render_remotely?
    cache_options = {
      layout: false,
      store_options: store_options,
      cache_path: cache_path
    }

    filter = ActionController::Caching::Actions::ActionCacheFilter.new(cache_options)

    filter.around(self, &block)

  else
    yield
  end
end