Sha256: 4f7c8ab0849d4ddc4fd5f54bb67bdd3fdd46aab9f184f0c7842a10705cf45fc2

Contents?: true

Size: 1.51 KB

Versions: 11

Compression:

Stored size: 1.51 KB

Contents

module ActionController
  # When our views change, they should bubble up into HTTP cache freshness
  # and bust browser caches. So the template digest for the current action
  # is automatically included in the ETag.
  #
  # Enabled by default for apps that use Action View. Disable by setting
  #
  #   config.action_controller.etag_with_template_digest = false
  #
  # Override the template to digest by passing +:template+ to +fresh_when+
  # and +stale?+ calls. For example:
  #
  #   # We're going to render widgets/show, not posts/show
  #   fresh_when @post, template: 'widgets/show'
  #
  #   # We're not going to render a template, so omit it from the ETag.
  #   fresh_when @post, template: false
  #
  module EtagWithTemplateDigest
    extend ActiveSupport::Concern

    include ActionController::ConditionalGet

    included do
      class_attribute :etag_with_template_digest
      self.etag_with_template_digest = true

      ActiveSupport.on_load :action_view, yield: true do
        etag do |options|
          determine_template_etag(options) if etag_with_template_digest
        end
      end
    end

    private
    def determine_template_etag(options)
      if template = pick_template_for_etag(options)
        lookup_and_digest_template(template)
      end
    end

    def pick_template_for_etag(options)
      options.fetch(:template) { "#{controller_name}/#{action_name}" }
    end

    def lookup_and_digest_template(template)
      ActionView::Digestor.digest name: template, finder: lookup_context
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
abaci-0.3.0 vendor/bundle/gems/actionpack-5.0.0/lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0.rc2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0.racecar1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0.rc1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0.beta4 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0.beta3 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0.beta2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0.beta1.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-5.0.0.beta1 lib/action_controller/metal/etag_with_template_digest.rb