Skip to content

Commit

Permalink
yes, now i respond to creation
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Jun 12, 2010
1 parent 3ab3507 commit 2f13e12
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index

def create
@item = Item.create(params[:item])
render :text => "", :status => 201, :location => item_url(@item)
respond_with @item, :status => :created
end

def show
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
member(@item) do |member, item|
partial "show", binding
end
2 changes: 2 additions & 0 deletions lib/restfulie/server/action_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Server
module ActionController #:nodoc:
if defined?(::ActionController) || defined?(::ApplicationController)
autoload :ParamsParser, 'restfulie/server/action_controller/params_parser'
autoload :CacheableResponder, 'restfulie/server/action_controller/cacheable_responder'
autoload :CreatedResponder, 'restfulie/server/action_controller/created_responder'
autoload :RestfulResponder, 'restfulie/server/action_controller/restful_responder'
autoload :Base, 'restfulie/server/action_controller/base'
end
Expand Down
19 changes: 19 additions & 0 deletions lib/restfulie/server/action_controller/created_responder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Restfulie
module Server
module ActionController

# Adds support to answering as a 201 when the resource has been just created
module CreatedResponder

def to_format
if [201, :created].include? options[:status]
head :status => 201, :location => controller.url_for(resource)
else
super
end
end

end
end
end
end
39 changes: 39 additions & 0 deletions spec/units/server/action_controller/created_responder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

class ResponderWithCreated < ::ActionController::Responder
include Restfulie::Server::ActionController::CreatedResponder
end

class CreationController < ApplicationController
self.responder = ResponderWithCreated
respond_to :atom

def create
@resource = Object.new
respond_with @resource, :status => :created
end

end

ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id'
end

describe CreationController, :type => :controller do

before(:each) do
request.accept = "application/atom+xml"
end

context "creating a resource" do

it "should return 201 with location" do
uri = "http://newlocation.com/uri"
controller.should_receive(:url_for).and_return uri
post :create
controller.response.code.should == "201"
controller.response.headers["Location"].should == uri
end

end
end

0 comments on commit 2f13e12

Please sign in to comment.