diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index ba17d1f..59be270 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -2,9 +2,11 @@ module CanCan # Handle the load and authorization controller logic so we don't clutter up all controllers with non-interface methods. # This class is used internally, so you do not need to call methods directly on it. class ControllerResource # :nodoc: - def self.add_before_filter(controller_class, method, options = {}) + def self.add_before_filter(controller_class, method, *args) + options = args.extract_options! + resource_name = args.first controller_class.before_filter(options.slice(:only, :except)) do |controller| - ControllerResource.new(controller, options.except(:only, :except)).send(method) + ControllerResource.new(controller, resource_name, options.except(:only, :except)).send(method) end end diff --git a/spec/cancan/controller_additions_spec.rb b/spec/cancan/controller_additions_spec.rb index ffac620..1c654cc 100644 --- a/spec/cancan/controller_additions_spec.rb +++ b/spec/cancan/controller_additions_spec.rb @@ -53,19 +53,25 @@ describe CanCan::ControllerAdditions do end it "load_and_authorize_resource should setup a before filter which passes call to ControllerResource" do - stub(CanCan::ControllerResource).new(@controller, :foo => :bar).mock!.load_and_authorize_resource + stub(CanCan::ControllerResource).new(@controller, nil, :foo => :bar).mock!.load_and_authorize_resource mock(@controller_class).before_filter({}) { |options, block| block.call(@controller) } @controller_class.load_and_authorize_resource :foo => :bar end + it "load_and_authorize_resource should properly pass first argument as the resource name" do + stub(CanCan::ControllerResource).new(@controller, :project, :foo => :bar).mock!.load_and_authorize_resource + mock(@controller_class).before_filter({}) { |options, block| block.call(@controller) } + @controller_class.load_and_authorize_resource :project, :foo => :bar + end + it "authorize_resource should setup a before filter which passes call to ControllerResource" do - stub(CanCan::ControllerResource).new(@controller, :foo => :bar).mock!.authorize_resource + stub(CanCan::ControllerResource).new(@controller, nil, :foo => :bar).mock!.authorize_resource mock(@controller_class).before_filter(:except => :show) { |options, block| block.call(@controller) } @controller_class.authorize_resource :foo => :bar, :except => :show end it "load_resource should setup a before filter which passes call to ControllerResource" do - stub(CanCan::ControllerResource).new(@controller, :foo => :bar).mock!.load_resource + stub(CanCan::ControllerResource).new(@controller, nil, :foo => :bar).mock!.load_resource mock(@controller_class).before_filter(:only => [:show, :index]) { |options, block| block.call(@controller) } @controller_class.load_resource :foo => :bar, :only => [:show, :index] end