Skip to content

Commit

Permalink
added ResourceMethods#destroy_resource, which calls destroy on the re…
Browse files Browse the repository at this point in the history
…source_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)
  • Loading branch information
ianwhite committed Jan 27, 2010
1 parent 28a3aa9 commit cb77bc2
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 41 deletions.
6 changes: 6 additions & 0 deletions 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)

Expand Down
4 changes: 4 additions & 0 deletions lib/ardes/resources_controller.rb
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/ardes/resources_controller/actions.rb
Expand Up @@ -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."
Expand Down
3 changes: 1 addition & 2 deletions lib/ardes/resources_controller/singleton_actions.rb
Expand Up @@ -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."
Expand Down
11 changes: 3 additions & 8 deletions spec/controllers/addresses_controller_spec.rb
Expand Up @@ -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

Expand Down
11 changes: 3 additions & 8 deletions spec/controllers/comments_controller_spec.rb
Expand Up @@ -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

Expand Down
11 changes: 3 additions & 8 deletions spec/controllers/forum_posts_controller_spec.rb
Expand Up @@ -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

Expand Down
Expand Up @@ -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
5 changes: 0 additions & 5 deletions spec/controllers/resource_service_in_infos_controller_spec.rb
Expand Up @@ -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
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/lib/resource_methods_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit cb77bc2

Please sign in to comment.