From 236cece3b3d6b9ea58a52698d183c733bb750d51 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Fri, 6 Aug 2010 11:18:54 -0700 Subject: [PATCH] adding :find_by option to load_resource - closes #19 --- lib/cancan/controller_additions.rb | 5 +++++ lib/cancan/controller_resource.rb | 6 +++++- spec/cancan/controller_resource_spec.rb | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/cancan/controller_additions.rb b/lib/cancan/controller_additions.rb index 4106544..3ce9d44 100644 --- a/lib/cancan/controller_additions.rb +++ b/lib/cancan/controller_additions.rb @@ -80,6 +80,11 @@ module CanCan # [:+instance_name+] # The name of the instance variable to load the resource into. # + # [:+find_by+] + # Find using a different attribute other than id. For example. + # + # load_resource :find_by => :permalink # will use find_by_permlink!(params[:id]) + # # [:+collection+] # Specify which actions are resource collection actions in addition to :+index+. This # is usually not necessary because it will try to guess depending on if the id param is present. diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 0c289d4..3d4dcc6 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -53,7 +53,11 @@ module CanCan end def find_resource - @options[:singular] ? resource_base.send(name) : resource_base.find(id_param) + if @options[:singular] + resource_base.send(name) + else + @options[:find_by] ? resource_base.send("find_by_#{@options[:find_by]}!", id_param) : resource_base.find(id_param) + end end def authorization_action diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index fe6728a..70b8279 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/spec/cancan/controller_resource_spec.rb @@ -218,6 +218,14 @@ describe CanCan::ControllerResource do @controller.instance_variable_get(:@custom_ability).should == :some_ability end + it "should load resource using custom find_by attribute" do + @params.merge!(:action => "show", :id => 123) + stub(Ability).find_by_name!(123) { :some_resource } + resource = CanCan::ControllerResource.new(@controller, :find_by => :name) + resource.load_resource + @controller.instance_variable_get(:@ability).should == :some_resource + end + it "should raise ImplementationRemoved when adding :name option" do lambda { CanCan::ControllerResource.new(@controller, :name => :foo)