Sha256: b9f9669b6625925642f3f4769d8a881138f69553ef02b9926a9fc99a95a51065

Contents?: true

Size: 1.34 KB

Versions: 15

Compression:

Stored size: 1.34 KB

Contents

module Responders
  # Set HTTP Last-Modified headers based on the given resource. It's used only
  # on API behavior (to_format) and is useful for a client to check in the server
  # if a resource changed after a specific date or not.
  #
  # This is not usually not used in html requests because pages contains a lot
  # information besides the resource information, as current_user, flash messages,
  # widgets... that are better handled with other strategies, as fragment caches and
  # the digest of the body.
  #
  module HttpCacheResponder
    def initialize(controller, resources, options={})
      super
      @http_cache = options.delete(:http_cache)
    end

    def to_format
      return if do_http_cache? && do_http_cache!
      super
    end

  protected

    def do_http_cache!
      timestamp = resources.map do |resource|
        resource.updated_at.try(:utc) if resource.respond_to?(:updated_at)
      end.compact.max

      controller.response.last_modified ||= timestamp if timestamp

      head :not_modified if fresh = request.fresh?(controller.response)
      fresh
    end

    def do_http_cache?
      get? && @http_cache != false && ActionController::Base.perform_caching &&
        persisted? && resource.respond_to?(:updated_at)
    end

    def persisted?
      resource.respond_to?(:persisted?) ? resource.persisted? : true
    end
  end
end

Version data entries

15 entries across 15 versions & 5 rubygems

Version Path
responders-2.4.1 lib/responders/http_cache_responder.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/responders-2.4.0/lib/responders/http_cache_responder.rb
responders-2.4.0 lib/responders/http_cache_responder.rb
responders-2.3.0 lib/responders/http_cache_responder.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/responders-2.2.0/lib/responders/http_cache_responder.rb
responders-2.2.0 lib/responders/http_cache_responder.rb
responders-2.1.2 lib/responders/http_cache_responder.rb
responders-2.1.1 lib/responders/http_cache_responder.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/responders-2.1.0/lib/responders/http_cache_responder.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/responders-2.1.0/lib/responders/http_cache_responder.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/responders-2.1.0/lib/responders/http_cache_responder.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/responders-2.1.0/lib/responders/http_cache_responder.rb
responders-2.1.0 lib/responders/http_cache_responder.rb
responders-2.0.2 lib/responders/http_cache_responder.rb
responders-2.0.1 lib/responders/http_cache_responder.rb