File: http_cache_responder.rb

package info (click to toggle)
ruby-responders 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348 kB
  • sloc: ruby: 1,648; makefile: 6
file content (46 lines) | stat: -rw-r--r-- 1,405 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

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