Skip to content

Commit

Permalink
added last_modified header for show action, presuming the record resp…
Browse files Browse the repository at this point in the history
…onds to :updated_at
  • Loading branch information
nakajima committed Feb 1, 2009
1 parent f5741be commit 88891aa
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 5 deletions.
16 changes: 16 additions & 0 deletions features/headers.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Feature: Setting appropriate headers
In order to understand more about a response
As a developer
I want Sinatra's Hat to set useful headers

Scenario: Setting the last_modified for show
Given a model that has a record
And I mount the model
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
2 changes: 2 additions & 0 deletions features/steps/common_steps.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Before do
build_model(:people) do
string :name
timestamps

has_many :comments

Expand All @@ -10,6 +11,7 @@
build_model(:comments) do
integer :person_id
string :name
timestamps

belongs_to :person
end
Expand Down
3 changes: 3 additions & 0 deletions features/steps/header_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Then /^"(.*)" should be the record "(.*)" time$/ do |header, key|
DateTime.parse(response.headers[header].to_s).should == @record.send(key)
end
1 change: 1 addition & 0 deletions lib/sinatras-hat/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +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)
responder.success(:show, request, record)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/sinatras-hat/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def initialize(maker, request)
@request = request
end

def render(action)
def render(action, options={})
begin
options.each { |sym, value| @request.send(sym, value) }
@request.erb "#{maker.prefix}/#{action}".to_sym
rescue Errno::ENOENT
no_template! "Can't find #{File.expand_path(File.join(views, action.to_s))}.erb"
Expand Down
23 changes: 19 additions & 4 deletions spec/actions/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ def handle(*args)
before(:each) do
params = { :format => "yaml", :id => @article.to_param }
@request = fake_request(params)
stub(maker.model).find(params).returns(:article)
stub(maker.model).find(params).returns(@article)
end

it "serializes the data in the appropriate format" do
mock.proxy(maker.responder).serialize(request, :article)
mock.proxy(maker.responder).serialize(request, @article)
handle(request)
end

it "sets last_modified param" do
mock(request).last_modified(@article.updated_at)
handle(request)
end
end
Expand All @@ -41,11 +46,16 @@ def handle(*args)
before(:each) do
params = { :id => @article.to_param }
@request = fake_request(params)
stub(maker.model).find(params).returns(:article)
stub(maker.model).find(params).returns(@article)
end

it "renders the show template" do
mock.proxy(maker.responder).success(:show, request, :article)
mock.proxy(maker.responder).success(:show, request, @article)
handle(request)
end

it "sets last_modified param" do
mock(request).last_modified(@article.updated_at)
handle(request)
end
end
Expand All @@ -61,5 +71,10 @@ def handle(*args)
mock.proxy(maker.responder).not_found(request)
handle(request)
end

it "does not set last_modified param" do
mock(request).last_modified(@article.updated_at).never
handle(request)
end
end
end
7 changes: 7 additions & 0 deletions spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def new_response
end
end

context "when there are options passed" do
it "sends the options to the request" do
mock(request).last_modified :yesterday
new_response.render(:show, :last_modified => :yesterday)
end
end

context "when there is no views dir" do
before(:each) do
stub(request.options).views { nil }
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def fixture(path)
build_model(:articles) do
string :name
string :description
timestamps

has_many :comments
end
Expand Down Expand Up @@ -68,6 +69,7 @@ def fake_request(options={})
stub(request).env.returns({ })
stub(request).params.returns(options)
stub(request).response.returns(Sinatra::Response.new)
stub(request).last_modified(anything)
request
end

0 comments on commit 88891aa

Please sign in to comment.