Sha256: 0cc54f0d8378143893cc75aa62f99f0fad1d7d2df6ffd4c41bb9903526028edd

Contents?: true

Size: 1.69 KB

Versions: 7

Compression:

Stored size: 1.69 KB

Contents

module PageletRails::Concerns::Cache
  extend ActiveSupport::Concern

  included do
    include ActionController::Caching::Actions

    pagelet_options cache_defaults: {
      expires_in: 5.seconds,
      cache_path: {}
    }

    around_action :pagelet_cache
  end

  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

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pagelet_rails-0.1.6 lib/pagelet_rails/concerns/cache.rb
pagelet_rails-0.1.5 lib/pagelet_rails/concerns/cache.rb
pagelet_rails-0.1.4 lib/pagelet_rails/concerns/cache.rb
pagelet_rails-0.1.3 lib/pagelet_rails/concerns/cache.rb
pagelet_rails-0.1.2 lib/pagelet_rails/concerns/cache.rb
pagelet_rails-0.1.1 lib/pagelet_rails/concerns/cache.rb
pagelet_rails-0.1.0 lib/pagelet_rails/concerns/cache.rb