Skip to content

Commit

Permalink
Specs added for calling destroy on associations, which will trigger c…
Browse files Browse the repository at this point in the history
…allbacks
  • Loading branch information
ianwhite committed Jan 27, 2010
1 parent 72c154f commit 35bee60
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 80 deletions.
4 changes: 2 additions & 2 deletions lib/ardes/resources_controller/resource_methods.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/app.rb
Expand Up @@ -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

Expand Down
26 changes: 0 additions & 26 deletions spec/controllers/forums_controller_spec.rb
Expand Up @@ -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' }
Expand Down
35 changes: 0 additions & 35 deletions spec/controllers/interests_controller_via_forum_spec.rb
Expand Up @@ -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
Expand Down
96 changes: 79 additions & 17 deletions spec/lib/resource_methods_spec.rb
Expand Up @@ -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(<id>) should call User.find(<id>)" 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(<id>) should call User.destroy(<id>)" 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(<id>) should call forum.users.find(<id>)" 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(<id>) should call forum.users.destroy(<id>)" 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"
Expand All @@ -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
Expand All @@ -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

0 comments on commit 35bee60

Please sign in to comment.