Permalink
Browse files

destroy support on restfulie controller

  • Loading branch information...
1 parent e24eb6a commit 9175884d695f0539c74d7da9e2f727de362e64da @guilhermesilveira guilhermesilveira committed Dec 23, 2009
Showing with 38 additions and 7 deletions.
  1. +12 −4 lib/restfulie/server/restfulie_controller.rb
  2. +26 −3 spec/server/restfulie_controller_spec.rb
@@ -2,13 +2,14 @@ module Restfulie
module Server
+ # Controller which adds default CRUD + search + other operations.
module Controller
# creates a model based on the request media-type extracted from its content-type
#
def create
- type = model_for_this_controller
+ type = model_type
return head 415 unless fits_content(type, request.headers['CONTENT_TYPE'])
@model = type.from_xml request.body.string
@@ -22,16 +23,23 @@ def create
# renders this resource
def show
- @model = model_for_this_controller.find(params[:id])
+ @model = model_type.find(params[:id])
+ @model ? render_resource(@model) : head(404)
+ end
+
+ # destroys this resource
+ def destroy
+ @model = model_type.find(params[:id])
if @model
- render_resource @model
+ @model.destroy
+ head :ok
else
head 404
end
end
# returns the model for this controller
- def model_for_this_controller
+ def model_type
self.class.name[/(.*)Controller/,1].singularize.constantize
end
@@ -16,7 +16,7 @@ class Client
context "while preparing the request info" do
it "should extract the model part from the Controller name" do
- @controller.model_for_this_controller.should eql(Client)
+ @controller.model_type.should eql(Client)
end
it "should not fit content if it is not registered" do
@@ -78,7 +78,7 @@ class Client
it "should return 404 if resource is not found" do
id = 15
- @controller.should_receive(:model_for_this_controller).and_return(Client)
+ @controller.should_receive(:model_type).and_return(Client)
Client.should_receive(:find).with(id)
@controller.should_receive(:head).with(404)
@controller.should_receive(:params).and_return({:id=>id})
@@ -88,12 +88,35 @@ class Client
it "should render the resource if it exists" do
resource = Client.new
id = 15
- @controller.should_receive(:model_for_this_controller).and_return(Client)
+ @controller.should_receive(:model_type).and_return(Client)
Client.should_receive(:find).with(id).and_return(resource)
@controller.should_receive(:render_resource).with(resource)
@controller.should_receive(:params).and_return({:id=>id})
@controller.show
end
end
+ context "when destroying a resource" do
+
+ it "should return 404 if resource is not found" do
+ id = 15
+ @controller.should_receive(:model_type).and_return(Client)
+ Client.should_receive(:find).with(id)
+ @controller.should_receive(:head).with(404)
+ @controller.should_receive(:params).and_return({:id=>id})
+ @controller.destroy
+ end
+
+ it "should delete the resource if it exists" do
+ resource = Client.new
+ id = 15
+ @controller.should_receive(:model_type).and_return(Client)
+ Client.should_receive(:find).with(id).and_return(resource)
+ resource.should_receive(:destroy)
+ @controller.should_receive(:head).with(:ok)
+ @controller.should_receive(:params).and_return({:id=>id})
+ @controller.destroy
+ end
+ end
+
end

0 comments on commit 9175884

Please sign in to comment.