Permalink
Browse files

added a last_modified header for the index action which is based on t…

…he last updated record in the list of results
  • Loading branch information...
1 parent 88891aa commit 23b4c86d68178c9e6279fe82a48bf3b3cf087f38 @nakajima committed Feb 1, 2009
Showing with 58 additions and 8 deletions.
  1. +5 −5 features/headers.feature
  2. +8 −1 lib/sinatras-hat/actions.rb
  3. +8 −0 lib/sinatras-hat/model.rb
  4. +23 −2 spec/actions/index_spec.rb
  5. +10 −0 spec/model_spec.rb
  6. +4 −0 spec/spec_helper.rb
View
@@ -9,8 +9,8 @@ Feature: Setting appropriate headers
When I get the show page for that record
Then "Last-Modified" should be the record "updated_at" time
- # Scenario: Setting the last_modified for index
- # Given a model that has a record
- # And I mount the model
- # When Make a GET request to the index without a format
- # Then "Last-Modified" should be the record "updated_at" time
+ Scenario: Setting the last_modified for index
+ Given a model that has a record
+ And I mount the model
+ When Make a GET request to the index without a format
+ Then "Last-Modified" should be the record "updated_at" time
@@ -33,7 +33,7 @@ def self.included(map)
map.action :show, '/:id' do |request|
record = model.find(request.params) || responder.not_found(request)
- request.last_modified(record.updated_at) if record.respond_to?(:updated_at)
+ set_last_modified(request, record)
responder.success(:show, request, record)
end
@@ -45,8 +45,15 @@ def self.included(map)
map.action :index, '/' do |request|
records = model.all(request.params)
+ set_last_modified(request, model.find_last_modified(records))
responder.success(:index, request, records)
end
+
+ private
+
+ def set_last_modified(request, record)
+ request.last_modified(record.updated_at) if record.respond_to?(:updated_at)
+ end
end
end
end
@@ -50,6 +50,14 @@ def foreign_key
"#{singular}_id".to_sym
end
+ def find_last_modified(records)
+ if records.all? { |r| r.respond_to?(:updated_at) }
+ records.sort_by { |r| r.updated_at }.last
+ else
+ records.last
+ end
+ end
+
private
def proxy(params)
View
@@ -26,14 +26,35 @@ def handle(*args)
describe "rendering a response" do
context "when there's a :format param" do
before(:each) do
+ @newest_article = Article.create!
@request = fake_request(:format => "yaml")
- stub(maker.model).all(anything).returns([:article])
+ stub(maker.model).all(anything).returns([@newest_article, @article])
end
it "serializes the data in the appropriate format" do
- mock.proxy(maker.responder).serialize(request, [:article])
+ mock.proxy(maker.responder).serialize(request, [@newest_article, @article])
handle(request)
end
+
+ describe "setting the last_modified params" do
+ context "when the last record was the most recently updated" do
+ it "sets last_modified param to the last updated record's updated_at" do
+ mock(request).last_modified(@newest_article.updated_at)
+ handle(request)
+ end
+ end
+
+ context "when the last record was not the most recently updated" do
+ before(:each) do
+ @article.update_attribute :name, "Updated recently"
+ end
+
+ it "sets last_modified param to the last updated record's updated_at" do
+ mock(request).last_modified(@article.updated_at)
+ handle(request)
+ end
+ end
+ end
end
context "when there's no :format param" do
View
@@ -20,6 +20,16 @@ def new_model(maker=new_maker)
end
end
+ describe "find_last_modified" do
+ before(:each) do
+ @last_modified_article = Article.create!
+ end
+
+ it "returns the last modified record" do
+ new_model.find_last_modified([@article, @last_modified_article]).should == @last_modified_article
+ end
+ end
+
describe "all()" do
before(:each) do
@maker = new_maker
View
@@ -32,6 +32,10 @@ def fixture(path)
timestamps
has_many :comments
+
+ def self.all
+ super(:order => 'created_at DESC')
+ end
end
build_model(:comments) do

0 comments on commit 23b4c86

Please sign in to comment.