Skip to content

Commit

Permalink
Fixed that resource_service.destroy would not return the resource whe…
Browse files Browse the repository at this point in the history
…n the resource_service is an association
  • Loading branch information
ianwhite committed Feb 10, 2010
1 parent 143b4e0 commit 078a830
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
13 changes: 13 additions & 0 deletions lib/ardes/resources_controller.rb
Expand Up @@ -757,6 +757,19 @@ def new(*args, &block)
enclosing_resource ? service.build(*args, &block) : service.new(*args, &block)
end

# find the resource
# If we have a resource service, we call destroy on it with the reosurce id, so that any callbacks can be triggered
# Otherwise, just call destroy on the resource
def destroy(*args)
resource = find(*args)
if enclosing_resource
service.destroy(*args)
resource
else
resource.destroy
end
end

def respond_to?(method, include_private = false)
super || service.respond_to?(method)
end
Expand Down
11 changes: 7 additions & 4 deletions spec/controllers/addresses_controller_spec.rb
Expand Up @@ -331,17 +331,20 @@ def do_update

before(:each) do
setup_mocks
@address = mock('Address', :null_object => true)
@user_addresses.stub!(:destroy).and_return(@address)
@address = mock('Address', :id => "1", :null_object => true)
@user_addresses.stub!(:find).and_return(@address)
@user_addresses.stub!(:destroy)
end

def do_delete
delete :destroy, :id => "1", :user_id => "dave"
end

it "should destroy the address requested" do
@user_addresses.should_receive(:destroy).with("1").and_return(@address)
it "should find and destroy the address requested" do
@user_addresses.should_receive(:find).with("1").and_return(@address)
@user_addresses.should_receive(:destroy).with("1")
do_delete
assigns(:address).should == @address
end

it "should redirect to the things list" do
Expand Down
11 changes: 7 additions & 4 deletions spec/controllers/comments_controller_spec.rb
Expand Up @@ -364,17 +364,20 @@ def do_update

before(:each) do
setup_mocks
@comment = mock('Comment', :null_object => true)
@post_comments.stub!(:destroy).and_return(@comment)
@comment = mock('Comment', :id => '1', :null_object => true)
@post_comments.stub!(:find).and_return(@comment)
@post_comments.stub!(:destroy)
end

def do_delete
delete :destroy, :id => "1", :forum_id => '3', :post_id => '2'
end

it "should destroy the comment requested" do
@post_comments.should_receive(:destroy).with("1").and_return(@comment)
it "should find and destroy the comment requested" do
@post_comments.should_receive(:find).with("1").and_return(@comment)
@post_comments.should_receive(:destroy).with("1")
do_delete
assigns['comment'].should == @comment
end

it "should redirect to the comments list" do
Expand Down
9 changes: 6 additions & 3 deletions spec/controllers/forum_posts_controller_spec.rb
Expand Up @@ -413,16 +413,19 @@ def do_update
before(:each) do
setup_mocks
@post = mock('Post', :null_object => true)
@forum_posts.stub!(:destroy).and_return(@post)
@forum_posts.stub!(:find).and_return(@post)
@forum_posts.stub!(:destroy)
end

def do_delete
delete :destroy, :id => "1", :forum_id => "2"
end

it "should destroy the post requested" do
@forum_posts.should_receive(:destroy).with("1").and_return(@post)
it "should find and destroy the post requested" do
@forum_posts.should_receive(:find).with("1").and_return(@post)
@forum_posts.should_receive(:destroy).with("1")
do_delete
assigns['post'].should == @post
end

it "should redirect to the things list" do
Expand Down
14 changes: 8 additions & 6 deletions spec/lib/resource_methods_spec.rb
Expand Up @@ -42,9 +42,10 @@ class MyController < ActionController::Base
@controller.send(:new_resource, {})
end

it "#destroy_resource(<id>) should call User.destroy(<id>)" do
User.should_receive(:destroy).with("42")
@controller.send(:destroy_resource, "42")
it "#destroy_resource(<id>) should call User.find(<id>).destroy" do
User.should_receive(:find).with("42").and_return(user = mock)
user.should_receive(:destroy).and_return(user)
@controller.send(:destroy_resource, "42").should == user
end
end

Expand All @@ -69,9 +70,10 @@ class MyController < ActionController::Base
@controller.send :new_resource, {}
end

it "#destroy_resource(<id>) should call forum.users.destroy(<id>)" do
@forum.users.should_receive(:destroy).with("42")
@controller.send(:destroy_resource, "42")
it "#destroy_resource(<id>) should call forum.users.find(<id>) and forum.users.destroy(<id>)" do
@forum.users.should_receive(:find).with("42").and_return(user = mock)
@forum.users.should_receive(:destroy).with("42").and_return(useless_array = mock)
@controller.send(:destroy_resource, "42").should == user
end
end
end
Expand Down

2 comments on commit 078a830

@jlsync
Copy link
Collaborator

@jlsync jlsync commented on 078a830 Feb 10, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for rapid fix!

@ianwhite
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No probs Jason, shame on me for not specing the code fully

Please sign in to comment.