From 35bee606cbf8fa50872a1a168758ed5f21a46ba9 Mon Sep 17 00:00:00 2001 From: Ian White Date: Wed, 27 Jan 2010 11:18:04 +0000 Subject: [PATCH] Specs added for calling destroy on associations, which will trigger callbacks --- .../resources_controller/resource_methods.rb | 4 +- spec/app.rb | 1 + spec/controllers/forums_controller_spec.rb | 26 ----- .../interests_controller_via_forum_spec.rb | 35 ------- spec/lib/resource_methods_spec.rb | 96 +++++++++++++++---- 5 files changed, 82 insertions(+), 80 deletions(-) diff --git a/lib/ardes/resources_controller/resource_methods.rb b/lib/ardes/resources_controller/resource_methods.rb index d309b76..8df9cbd 100644 --- a/lib/ardes/resources_controller/resource_methods.rb +++ b/lib/ardes/resources_controller/resource_methods.rb @@ -10,14 +10,14 @@ def find_resources # finds the resource, using the passed id, defaults to the current params[:id] def find_resource(id = nil) - id ||= respond_to?(:params) && params[:id] + id ||= respond_to?(:params) && params.present? && params[:id] resource_service.find id end # makes a new resource, if attributes are not supplied, determine them from the # params hash and the current resource_class, or resource_name (the latter left in for BC) def new_resource(attributes = nil, &block) - if attributes.blank? && respond_to?(:params) + if attributes.blank? && respond_to?(:params) && params.present? resource_form_name = ActionController::RecordIdentifier.singular_class_name(resource_class) attributes = params[resource_form_name] || params[resource_name] || {} end diff --git a/spec/app.rb b/spec/app.rb index b7a6cb7..8d0a37d 100644 --- a/spec/app.rb +++ b/spec/app.rb @@ -151,6 +151,7 @@ class Forum < ActiveRecord::Base has_many :posts has_many :tags, :as => :taggable has_many :interests, :as => :interested_in + has_many :users, :through => :posts belongs_to :owner, :class_name => "User" end diff --git a/spec/controllers/forums_controller_spec.rb b/spec/controllers/forums_controller_spec.rb index dd35337..7c2030d 100644 --- a/spec/controllers/forums_controller_spec.rb +++ b/spec/controllers/forums_controller_spec.rb @@ -150,32 +150,6 @@ end end -describe "resource_service in ForumsController" do - controller_name :forums - - before(:each) do - @forum = Forum.create - - get :index - @resource_service = controller.send :resource_service - end - - it "should build new forum with new" do - resource = @resource_service.new - resource.should be_kind_of(Forum) - end - - it "should find @forum with find(@forum.id)" do - resource = @resource_service.find(@forum.id) - resource.should == @forum - end - - it "should find all forums with find(:all)" do - resources = @resource_service.find(:all) - resources.should == Forum.find(:all) - end -end - describe ForumsController, " requesting / (testing resource_path)" do it "should generate params { :controller => 'forums', :action => 'index', :resource_path => '/forums' } from GET /" do params_from(:get, "/").should == { :controller => 'forums', :action => 'index', :resource_path => '/forums' } diff --git a/spec/controllers/interests_controller_via_forum_spec.rb b/spec/controllers/interests_controller_via_forum_spec.rb index eec634f..edf0262 100644 --- a/spec/controllers/interests_controller_via_forum_spec.rb +++ b/spec/controllers/interests_controller_via_forum_spec.rb @@ -49,41 +49,6 @@ def setup_mocks end end -describe "resource_service in InterestsController via Forum" do - controller_name :interests - - before(:each) do - @forum = Forum.create - @interest = Interest.create :interested_in_id => @forum.id, :interested_in_type => 'Forum' - @other_forum = Forum.create - @other_interest = Interest.create :interested_in_id => @other_forum.id, :interested_in_type => 'Forum' - - get :index, :forum_id => @forum.id - @resource_service = controller.send :resource_service - end - - it "should build new interest with @forum fk and type with new" do - resource = @resource_service.new - resource.should be_kind_of(Interest) - resource.interested_in_id.should == @forum.id - resource.interested_in_type.should == 'Forum' - end - - it "should find @interest with find(@interest.id)" do - resource = @resource_service.find(@interest.id) - resource.should == @interest - end - - it "should raise RecordNotFound with find(@other_interest.id)" do - lambda{ @resource_service.find(@other_interest.id) }.should raise_error(ActiveRecord::RecordNotFound) - end - - it "should find only interests belonging to @forum with find(:all)" do - resources = @resource_service.find(:all) - resources.should be == Interest.find(:all, :conditions => "interested_in_id = #{@forum.id} AND interested_in_type = 'Forum'") - end -end - describe "Requesting /forums/1/interests using GET" do include InterestsViaForumSpecHelper controller_name :interests diff --git a/spec/lib/resource_methods_spec.rb b/spec/lib/resource_methods_spec.rb index 8ff0497..e9e1029 100644 --- a/spec/lib/resource_methods_spec.rb +++ b/spec/lib/resource_methods_spec.rb @@ -6,8 +6,77 @@ class MyController < ActionController::Base resources_controller_for :users end - module MyResourceMethods + if Rails.respond_to?(:version) && Rails.version >= "2.3" + describe "#new_resource" do + it "should accept block syntax" do + c = MyController.new + c.resource_service = User + c.stub!(:params).and_return({}) + r = c.send(:new_resource) do |u| + u.login = "Fred" + end + r.should be_kind_of User + r.login.should == "Fred" + end + end + end + + describe "An rc for collection :users" do + before do + @controller = MyController.new + end + + describe "when no enclosing resource" do + it "#find_resource() should call User.find()" do + User.should_receive(:find).with("42") + @controller.send(:find_resource, "42") + end + + it "#find_resources should call User.find(:all)" do + User.should_receive(:find).with(:all) + @controller.send(:find_resources) + end + + it "#new_resource({}) should call User.new({})" do + User.should_receive(:new).with({}) + @controller.send(:new_resource, {}) + end + + it "#destroy_resource() should call User.destroy()" do + User.should_receive(:destroy).with("42") + @controller.send(:destroy_resource, "42") + end + end + + describe "when an enclosing resource is added (a forum)" do + before do + @forum = Forum.create! + @controller.send :add_enclosing_resource, @forum + end + + it "#find_resource() should call forum.users.find()" do + @forum.users.should_receive(:find).with("42") + @controller.send(:find_resource, "42") + end + it "#find_resources should call forum.users.find(:all)" do + @forum.users.should_receive(:find).with(:all) + @controller.send(:find_resources) + end + + it "#new_resource({}) should call forum.users.build({})" do + @forum.users.should_receive(:build).with({}) + @controller.send :new_resource, {} + end + + it "#destroy_resource() should call forum.users.destroy()" do + @forum.users.should_receive(:destroy).with("42") + @controller.send(:destroy_resource, "42") + end + end + end + + module MyResourceMethods protected def new_resource(attrs = (params[resource_name] || {}), &block) "my new_resource" @@ -21,28 +90,17 @@ def find_resource(id = params[:id]) def find_resources "my find_resources" end + + def destroy_resource(id = params[:id]) + "my destroy_resource" + end end class MyControllerWithMyResourceMethods < ActionController::Base resources_controller_for :users include MyResourceMethods end - - if Rails.respond_to?(:version) && Rails.version >= "2.3" - describe "#new_resource" do - it "should accept block syntax" do - c = MyController.new - c.resource_service = User - c.stub!(:params).and_return({}) - r = c.send(:new_resource) do |u| - u.login = "Fred" - end - r.should be_kind_of User - r.login.should == "Fred" - end - end - end - + describe "A controller with resource methods mixed in after resources_controller_for" do before do @controller = MyControllerWithMyResourceMethods.new @@ -60,5 +118,9 @@ class MyControllerWithMyResourceMethods < ActionController::Base it "#find_resources should call MyResourceMethods#new_resource" do @controller.send(:find_resources).should == 'my find_resources' end + + it "#destroy_resource should call MyResourceMethods#destroy_resource" do + @controller.send(:destroy_resource).should == 'my destroy_resource' + end end end \ No newline at end of file