Sha256: 68f785291a93dd8f60cdb4e83d3ebc1b19a95f83c3d03b4ff27c7a62a7cd8d41

Contents?: true

Size: 1.36 KB

Versions: 9

Compression:

Stored size: 1.36 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.flatten.map do |resource|
        (resource.updated_at || Time.now).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 &&
        !new_record? && controller.response.last_modified.nil?
    end

    def new_record?
      resource.respond_to?(:new_record?) && resource.new_record?
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
responders-0.4.7 lib/responders/http_cache_responder.rb
responders-0.6.0 lib/responders/http_cache_responder.rb
responders-0.5.5 lib/responders/http_cache_responder.rb
responders-0.4.6 lib/responders/http_cache_responder.rb
responders-0.5.4 lib/responders/http_cache_responder.rb
responders-0.4.5 lib/responders/http_cache_responder.rb
responders-0.5.3 lib/responders/http_cache_responder.rb
responders-0.5.2 lib/responders/http_cache_responder.rb
responders-0.4.4 lib/responders/http_cache_responder.rb