Class: LastModifieds

Inherits:
Object
  • Object
show all
Defined in:
lib/restfulie/server/action_controller/trait/cacheable.rb

Instance Method Summary collapse

Instance Method Details

#accepts?(responder) ⇒ Boolean

default implementation that will check whether caching can be applied

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/restfulie/server/action_controller/trait/cacheable.rb', line 14

def accepts?(responder)
  responder.resources.flatten.select do |resource|
    resource.respond_to?(:updated_at)
  end &&
    responder.controller.response.last_modified.nil? &&
    !new_record?(responder)
end

#do_http_cache(responder, headers) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/restfulie/server/action_controller/trait/cacheable.rb', line 22

def do_http_cache(responder, headers)
  return false unless accepts?(responder)
  
  timestamps = responder.resources.flatten.select do |resource|
    resource.respond_to?(:updated_at)
  end.map do |resource|
    (resource.updated_at || Time.now).utc
  end
  
  timestamp = timestamps.max

  responder.controller.response.last_modified = timestamp if timestamp
end

#new_record?(responder) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/restfulie/server/action_controller/trait/cacheable.rb', line 36

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