Skip to content

Commit

Permalink
Do not cache new resources in HttpCacheResponder
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Dec 24, 2009
1 parent 83127ab commit 18284ec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/responders/flash_responder.rb
Expand Up @@ -88,7 +88,7 @@ def initialize(controller, resources, options={})
def to_html
super

unless !respond_to?(:"to_#{format}") || get? || @flash == false
if set_i18n_flash?
if has_errors?
controller.flash[:alert] ||= @alert if @alert
status = Responders::FlashResponder.flash_keys.last
Expand All @@ -107,6 +107,10 @@ def to_html

protected

def set_i18n_flash? #:nodoc:
!get? && @flash != false
end

def mount_i18n_options(status) #:nodoc:
resource_name = if resource.class.respond_to?(:human_name)
resource.class.human_name
Expand Down
14 changes: 12 additions & 2 deletions lib/responders/http_cache_responder.rb
Expand Up @@ -15,9 +15,9 @@ def initialize(controller, resources, options={})
end

def to_format
if get? && @http_cache != false && controller.response.last_modified.nil?
if do_http_cache?
timestamp = resources.flatten.map do |resource|
resource.updated_at.utc if resource.respond_to?(:updated_at)
(resource.updated_at || Time.now).utc if resource.respond_to?(:updated_at)
end.compact.max

controller.response.last_modified = timestamp if timestamp
Expand All @@ -29,5 +29,15 @@ def to_format

super
end

protected

def do_http_cache?
get? && @http_cache != false && !new_record? && controller.response.last_modified.nil?
end

def new_record?
resource.respond_to?(:new_record?) && resource.new_record?
end
end
end
18 changes: 18 additions & 0 deletions test/http_cache_responder_test.rb
Expand Up @@ -17,6 +17,12 @@ def collection
respond_with [Model.new(Time.utc(2009)), Model.new(Time.utc(2008))]
end

def new_record
model = Model.new(Time.utc(2009))
model.new_record = true
respond_with(model)
end

def empty
respond_with []
end
Expand Down Expand Up @@ -101,4 +107,16 @@ def test_work_with_an_empty_array
assert_match /xml/, @response.body
assert_equal 200, @response.status
end

def test_it_does_not_set_body_etag
get :collection
assert_nil @response.headers["ETag"]
end

def test_does_not_set_cache_for_new_records
get :new_record
assert_nil @response.headers["Last-Modified"]
assert_equal "<xml />", @response.body
assert_equal 200, @response.status
end
end
6 changes: 6 additions & 0 deletions test/test_helper.rb
Expand Up @@ -43,6 +43,12 @@ class ApplicationController < ActionController::Base
end

class Model < Struct.new(:updated_at)
attr_writer :new_record

def new_record?
@new_record || false
end

def to_xml(*args)
"<xml />"
end
Expand Down

0 comments on commit 18284ec

Please sign in to comment.