Module: Fortitude::Caching::ClassMethods

Defined in:
lib/fortitude/caching.rb

Instance Method Summary collapse

Instance Method Details

#cacheable(opts = {}) ⇒ Object



9
10
11
12
13
14
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
# File 'lib/fortitude/caching.rb', line 9

def cacheable(opts = {})
  if extra_assigns == :use
    extra_assigns :ignore
  end

  static_keys = Array(opts.fetch(:key, []))
  options = opts.fetch(:options, {})

  define_method(:cache_contents) do |&block|
    if !cacheable_by_fortitude?
      raise(
        NotCacheableError,
        "Can't digest the widget #{self}, since it's being rendered " \
        "outside of an ActionView context."
      )
    end

    cache calculate_cache_dependencies(assigns, static_keys), options do
      block.call
    end
  end

  define_method(:cacheable_by_fortitude?) do
    respond_to?(:cache) &&
    (
      invoke_helper(:instance_variable_get, :@virtual_path) ||
      options[:skip_digest]
    )
  end

  around_content :cache_contents
end