Permalink
Browse files

load collection resources in custom controller actions with no id par…

…am - closes #296
  • Loading branch information...
1 parent ba99997 commit 37102fe6f80ab257b6dd6c8f42cb794dc525ade1 @ryanb committed Mar 9, 2011
@@ -113,7 +113,7 @@ def id_param
end
def member_action?
- !collection_actions.include? @params[:action].to_sym
+ new_actions.include?(@params[:action].to_sym) || (@params[:id] && !collection_actions.include?(@params[:action].to_sym))
end
# Returns the class used for this resource. This can be overriden by the :class option.
@@ -110,35 +110,44 @@
end
it "should perform authorization using controller action and loaded model" do
- @params[:action] = "show"
+ @params.merge!(:action => "show", :id => 123)
@controller.instance_variable_set(:@project, :some_project)
stub(@controller).authorize!(:show, :some_project) { raise CanCan::AccessDenied }
resource = CanCan::ControllerResource.new(@controller)
lambda { resource.authorize_resource }.should raise_error(CanCan::AccessDenied)
end
it "should perform authorization using controller action and non loaded model" do
- @params[:action] = "show"
+ @params.merge!(:action => "show", :id => 123)
stub(@controller).authorize!(:show, Project) { raise CanCan::AccessDenied }
resource = CanCan::ControllerResource.new(@controller)
lambda { resource.authorize_resource }.should raise_error(CanCan::AccessDenied)
end
it "should call load_resource and authorize_resource for load_and_authorize_resource" do
- @params[:action] = "show"
+ @params.merge!(:action => "show", :id => 123)
resource = CanCan::ControllerResource.new(@controller)
mock(resource).load_resource
mock(resource).authorize_resource
resource.load_and_authorize_resource
end
- it "should not build a resource when on custom collection action" do
- @params[:action] = "sort"
+ it "should not build a single resource when on custom collection action even with id" do
+ @params.merge!(:action => "sort", :id => 123)
resource = CanCan::ControllerResource.new(@controller, :collection => [:sort, :list])
resource.load_resource
@controller.instance_variable_get(:@project).should be_nil
end
+ it "should load a collection resource when on custom action with no id param" do
+ stub(Project).accessible_by(@ability, :sort) { :found_projects }
+ @params[:action] = "sort"
+ resource = CanCan::ControllerResource.new(@controller)
+ resource.load_resource
+ @controller.instance_variable_get(:@project).should be_nil
+ @controller.instance_variable_get(:@projects).should == :found_projects
+ end
+
it "should build a resource when on custom new action even when params[:id] exists" do
@params.merge!(:action => "build", :id => 123)
stub(Project).new { :some_project }
@@ -250,7 +259,7 @@
end
it "should find record through has_one association with :singleton option" do
- @params.merge!(:action => "show")
+ @params.merge!(:action => "show", :id => 123)
category = Object.new
@controller.instance_variable_set(:@category, category)
stub(category).project { :some_project }
@@ -12,7 +12,7 @@
end
it "show should load resource through @controller.resource" do
- @params[:action] = "show"
+ @params.merge!(:action => "show", :id => 123)
stub(@controller).resource { :project_resource }
CanCan::InheritedResource.new(@controller).load_resource
@controller.instance_variable_get(:@project).should == :project_resource

0 comments on commit 37102fe

Please sign in to comment.