This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding support for loading through Inherited Resources - closes #23
- Loading branch information
Showing
6 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module CanCan | ||
# For use with Inherited Resources | ||
class InheritedResource < ControllerResource # :nodoc: | ||
def load_resource_instance | ||
if parent? | ||
@controller.parent | ||
elsif new_actions.include? @params[:action].to_sym | ||
@controller.build_resource | ||
else | ||
@controller.resource | ||
end | ||
end | ||
|
||
def resource_base | ||
@controller.end_of_association_chain | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "spec_helper" | ||
|
||
describe CanCan::InheritedResource do | ||
before(:each) do | ||
@params = HashWithIndifferentAccess.new(:controller => "projects") | ||
@controller = Object.new # simple stub for now | ||
@ability = Ability.new(nil) | ||
stub(@controller).params { @params } | ||
stub(@controller).current_ability { @ability } | ||
end | ||
|
||
it "show should load resource through @controller.resource" do | ||
@params[:action] = "show" | ||
stub(@controller).resource { :project_resource } | ||
CanCan::InheritedResource.new(@controller).load_resource | ||
@controller.instance_variable_get(:@project).should == :project_resource | ||
end | ||
|
||
it "new should load through @controller.build_resource" do | ||
@params[:action] = "new" | ||
stub(@controller).build_resource { :project_resource } | ||
CanCan::InheritedResource.new(@controller).load_resource | ||
@controller.instance_variable_get(:@project).should == :project_resource | ||
end | ||
|
||
it "index should load through @controller.parent when parent" do | ||
@params[:action] = "index" | ||
stub(@controller).parent { :project_resource } | ||
CanCan::InheritedResource.new(@controller, :parent => true).load_resource | ||
@controller.instance_variable_get(:@project).should == :project_resource | ||
end | ||
|
||
it "index should load through @controller.end_of_association_chain" do | ||
@params[:action] = "index" | ||
stub(Project).accessible_by(@ability) { :projects } | ||
stub(@controller).end_of_association_chain { Project } | ||
CanCan::InheritedResource.new(@controller).load_resource | ||
@controller.instance_variable_get(:@projects).should == :projects | ||
end | ||
end |