fixing behavior of load_and_authorize_resource for namespaced controllers - closes #3

This commit is contained in:
Ryan Bates 2009-11-17 11:59:59 -08:00
parent 766fe86a9f
commit 15a01a579c
3 changed files with 14 additions and 3 deletions

View File

@ -1,3 +1,5 @@
* fix behavior of load_and_authorize_resource for namespaced controllers - see issue #3
* support arrays being passed to "can" to specify multiple actions or classes - see issue #2
* adding "cannot?" method to ability, controller, and view which is inverse of "can?" - see issue #1

View File

@ -21,17 +21,19 @@ module CanCan
end
def load_resource # TODO this could use some refactoring
model_name = params[:controller].split('/').last.singularize
unless params[:action] == "index"
if params[:id]
instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.find(params[:id]))
instance_variable_set("@#{model_name}", model_name.camelcase.constantize.find(params[:id]))
else
instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.new(params[params[:controller].singularize.to_sym]))
instance_variable_set("@#{model_name}", model_name.camelcase.constantize.new(params[model_name.to_sym]))
end
end
end
def authorize_resource # TODO this could use some refactoring
unauthorized! unless can?(params[:action].to_sym, instance_variable_get("@#{params[:controller].singularize}") || params[:controller].singularize.camelcase.constantize)
model_name = params[:controller].split('/').last.singularize
unauthorized! unless can?(params[:action].to_sym, instance_variable_get("@#{model_name}") || model_name.camelcase.constantize)
end
def load_and_authorize_resource

View File

@ -82,4 +82,11 @@ describe CanCan::ControllerAdditions do
stub(@controller).authorize_resource
@controller.load_and_authorize_resource
end
it "should properly load resource for namespaced controller" do
stub(@controller).params { {:controller => "admin/abilities", :action => "show", :id => 123} }
stub(Ability).find(123) { :some_resource }
@controller.load_resource
@controller.instance_variable_get(:@ability).should == :some_resource
end
end