Sha256: 98a3244f405dcf0eec370077356b588cca4c3d6e794832182a05ce4592c9f241

Contents?: true

Size: 1.53 KB

Versions: 48

Compression:

Stored size: 1.53 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 |action_view_base|
        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

48 entries across 47 versions & 8 rubygems

Version Path
actionpack-4.2.11.3 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.11.2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.11.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.11 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.10 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.10.rc1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.9 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.9.rc2 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.9.rc1 lib/action_controller/metal/etag_with_template_digest.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/actionpack-4.2.8/lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.8 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.8.rc1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.7.1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.7 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.7.rc1 lib/action_controller/metal/etag_with_template_digest.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.6/lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.6 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.6.rc1 lib/action_controller/metal/etag_with_template_digest.rb
actionpack-4.2.5.2 lib/action_controller/metal/etag_with_template_digest.rb
activejob-lock-0.0.2 rails/actionpack/lib/action_controller/metal/etag_with_template_digest.rb