Sha256: 6165ccf26f145ac6fb2f2b797c8ed385351aba2d0a39d57069c163689d1838ac

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module PagesCore
  module StaticCacheController
    extend ActiveSupport::Concern

    included do
      helper_method :static_cached?
    end

    module ClassMethods
      def static_cache(*actions, permanent: false)
        before_action :prepare_static_cache
        if permanent
          after_action :cache_static_page_permanently, only: actions
        else
          after_action :cache_static_page, only: actions
        end
      end

      alias caches_page static_cache
    end

    def disable_static_cache!
      @static_cache_disabled = true
    end

    def static_cached?
      !@static_cache_disabled && @static_cached ? true : false
    end

    private

    def cache_static_page
      return unless static_cache_allowed?

      PagesCore::StaticCache.handler.cache_page(
        self, request, response
      )
    end

    def cache_static_page_permanently
      return unless static_cache_allowed?

      PagesCore::StaticCache.handler.cache_page_permanently(
        self, request, response
      )
    end

    def prepare_static_cache
      @static_cached = true
    end

    def static_cache_allowed?
      (request.get? || request.head?) && response.status == 200 &&
        perform_caching && !@static_cache_disabled
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pages_core-3.15.5 app/controllers/concerns/pages_core/static_cache_controller.rb
pages_core-3.15.4 app/controllers/concerns/pages_core/static_cache_controller.rb
pages_core-3.15.3 app/controllers/concerns/pages_core/static_cache_controller.rb
pages_core-3.15.2 app/controllers/concerns/pages_core/static_cache_controller.rb
pages_core-3.15.1 app/controllers/concerns/pages_core/static_cache_controller.rb
pages_core-3.14.0 app/controllers/concerns/pages_core/static_cache_controller.rb
pages_core-3.13.0 app/controllers/concerns/pages_core/static_cache_controller.rb