From cb77bc2325b1f165d08b0171fe501de70ef759ff Mon Sep 17 00:00:00 2001 From: Ian White Date: Wed, 27 Jan 2010 13:31:21 +0000 Subject: [PATCH] added ResourceMethods#destroy_resource, which calls destroy on the resource_service, which in turn finds the best way to destroy the resource to trigger any association callbacks. Changed actions to use the destroy_resource method. (reported by Jason Lee) --- History.txt | 6 ++++++ lib/ardes/resources_controller.rb | 4 ++++ lib/ardes/resources_controller/actions.rb | 3 +-- lib/ardes/resources_controller/singleton_actions.rb | 3 +-- spec/controllers/addresses_controller_spec.rb | 11 +++-------- spec/controllers/comments_controller_spec.rb | 11 +++-------- spec/controllers/forum_posts_controller_spec.rb | 11 +++-------- .../resource_service_in_forums_controller_spec.rb | 4 ++++ .../resource_service_in_infos_controller_spec.rb | 5 ----- ..._service_in_interests_controller_via_forum_spec.rb | 7 +------ spec/lib/resource_methods_spec.rb | 5 +++-- 11 files changed, 29 insertions(+), 41 deletions(-) diff --git a/History.txt b/History.txt index 80b1bf6..69721f7 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,9 @@ +* added ResourceMethods#destroy_resource, which calls destroy on the resource_service, which in turn finds the best way to + destroy the resource to trigger any association callbacks. Changed actions to use the destroy_resource method. (reported by Jason Lee) + + NOTE: this may break some of your controller specs, they may report an unexpected :destroy message sent to an association. + You just need to expect this to happen when you have an enclosing resource. + * ResourceMethods#new_resource looks in params[singular_resource_class_name] for attributes, which makes RC much more compatible with form_for (reported by Jason Lee) diff --git a/lib/ardes/resources_controller.rb b/lib/ardes/resources_controller.rb index d0384bf..066e9c4 100644 --- a/lib/ardes/resources_controller.rb +++ b/lib/ardes/resources_controller.rb @@ -782,6 +782,10 @@ def new(*args, &block) enclosing_resource ? enclosing_resource.send("build_#{resource_specification.source}", *args, &block) : service.new(*args, &block) end + def destroy(*args) + find.destroy + end + def service resource_class end diff --git a/lib/ardes/resources_controller/actions.rb b/lib/ardes/resources_controller/actions.rb index a9729e8..0a62cbd 100644 --- a/lib/ardes/resources_controller/actions.rb +++ b/lib/ardes/resources_controller/actions.rb @@ -134,8 +134,7 @@ def update # DELETE /events/1 # DELETE /events/1.xml def destroy - self.resource = find_resource - resource.destroy + self.resource = destroy_resource respond_to do |format| format.html do flash[:notice] = "#{resource_name.humanize} was successfully destroyed." diff --git a/lib/ardes/resources_controller/singleton_actions.rb b/lib/ardes/resources_controller/singleton_actions.rb index 46c9b63..3eb9d1b 100644 --- a/lib/ardes/resources_controller/singleton_actions.rb +++ b/lib/ardes/resources_controller/singleton_actions.rb @@ -8,8 +8,7 @@ module SingletonActions # DELETE /event # DELETE /event.xml def destroy - self.resource = find_resource - resource.destroy + self.resource = destroy_resource respond_to do |format| format.html do flash[:notice] = "#{resource_name.humanize} was successfully destroyed." diff --git a/spec/controllers/addresses_controller_spec.rb b/spec/controllers/addresses_controller_spec.rb index a38b02d..ee0d732 100644 --- a/spec/controllers/addresses_controller_spec.rb +++ b/spec/controllers/addresses_controller_spec.rb @@ -332,20 +332,15 @@ def do_update before(:each) do setup_mocks @address = mock('Address', :null_object => true) - @user_addresses.stub!(:find).and_return(@address) + @user_addresses.stub!(:destroy).and_return(@address) end def do_delete delete :destroy, :id => "1", :user_id => "dave" end - it "should find the address requested" do - @user_addresses.should_receive(:find).with("1").and_return(@address) - do_delete - end - - it "should call destroy on the found thing" do - @address.should_receive(:destroy) + it "should destroy the address requested" do + @user_addresses.should_receive(:destroy).with("1").and_return(@address) do_delete end diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index 605992b..5d3b7c8 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -365,20 +365,15 @@ def do_update before(:each) do setup_mocks @comment = mock('Comment', :null_object => true) - @post_comments.stub!(:find).and_return(@comment) + @post_comments.stub!(:destroy).and_return(@comment) end def do_delete delete :destroy, :id => "1", :forum_id => '3', :post_id => '2' end - it "should find the comment requested" do - @post_comments.should_receive(:find).with("1").and_return(@comment) - do_delete - end - - it "should call destroy on the found comment" do - @comment.should_receive(:destroy) + it "should destroy the comment requested" do + @post_comments.should_receive(:destroy).with("1").and_return(@comment) do_delete end diff --git a/spec/controllers/forum_posts_controller_spec.rb b/spec/controllers/forum_posts_controller_spec.rb index 6ad9a46..3a9e717 100644 --- a/spec/controllers/forum_posts_controller_spec.rb +++ b/spec/controllers/forum_posts_controller_spec.rb @@ -413,20 +413,15 @@ def do_update before(:each) do setup_mocks @post = mock('Post', :null_object => true) - @forum_posts.stub!(:find).and_return(@post) + @forum_posts.stub!(:destroy).and_return(@post) end def do_delete delete :destroy, :id => "1", :forum_id => "2" end - it "should find the post requested" do - @forum_posts.should_receive(:find).with("1").and_return(@post) - do_delete - end - - it "should call destroy on the found thing" do - @post.should_receive(:destroy) + it "should destroy the post requested" do + @forum_posts.should_receive(:destroy).with("1").and_return(@post) do_delete end diff --git a/spec/controllers/resource_service_in_forums_controller_spec.rb b/spec/controllers/resource_service_in_forums_controller_spec.rb index 19abccf..6d2cc42 100644 --- a/spec/controllers/resource_service_in_forums_controller_spec.rb +++ b/spec/controllers/resource_service_in_forums_controller_spec.rb @@ -30,4 +30,8 @@ lambda { @resource_service.destroy(@forum.id) }.should change(Forum, :count).by(-1) lambda { Forum.find(@forum.id) }.should raise_error(ActiveRecord::RecordNotFound) end + + it "should return the destroyed forum with destroy(@forum.id)" do + @resource_service.destroy(@forum.id).should == @forum + end end \ No newline at end of file diff --git a/spec/controllers/resource_service_in_infos_controller_spec.rb b/spec/controllers/resource_service_in_infos_controller_spec.rb index 368a437..7ae0dd3 100644 --- a/spec/controllers/resource_service_in_infos_controller_spec.rb +++ b/spec/controllers/resource_service_in_infos_controller_spec.rb @@ -29,9 +29,4 @@ lambda { @resource_service.destroy }.should change(Info, :count).by(-1) lambda { Info.find(@info.id) }.should raise_error(ActiveRecord::RecordNotFound) end - - it "should call destroy_info on the account" do - @account.should_receive(:destroy_info) - @resource_service.destroy - end end \ No newline at end of file diff --git a/spec/controllers/resource_service_in_interests_controller_via_forum_spec.rb b/spec/controllers/resource_service_in_interests_controller_via_forum_spec.rb index 87649c1..9fe567a 100644 --- a/spec/controllers/resource_service_in_interests_controller_via_forum_spec.rb +++ b/spec/controllers/resource_service_in_interests_controller_via_forum_spec.rb @@ -40,12 +40,7 @@ lambda { Interest.find(@interest.id) }.should raise_error(ActiveRecord::RecordNotFound) end - it "should call destory(id) on the forum's interests assoc" do - @forum.interests.should_receive(:destroy).with(@interest.id) - @resource_service.destroy(@interest.id) - end - - it "should NOT destory the otehr interest with destroy(@other_interest.id)" do + it "should NOT destory the other interest with destroy(@other_interest.id)" do lambda { @resource_service.destroy(@other_interest.id) }.should raise_error Interest.find(@other_interest.id).should == @other_interest end diff --git a/spec/lib/resource_methods_spec.rb b/spec/lib/resource_methods_spec.rb index 67f014f..520da16 100644 --- a/spec/lib/resource_methods_spec.rb +++ b/spec/lib/resource_methods_spec.rb @@ -101,8 +101,9 @@ class MySingletonController < ActionController::Base @controller.send :new_resource, {} end - it "#destroy_resource should call users.destroy_info" do - @user.should_receive(:destroy_info) + it "#destroy_resource should call user.info.destroy" do + @user.should_receive(:info).and_return(info = mock) + info.should_receive(:destroy) @controller.send(:destroy_resource) end end