fixing behavior of load_and_authorize_resource for namespaced controllers - closes #3
This commit is contained in:
parent
766fe86a9f
commit
15a01a579c
|
@ -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
|
* 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
|
* adding "cannot?" method to ability, controller, and view which is inverse of "can?" - see issue #1
|
||||||
|
|
|
@ -21,17 +21,19 @@ module CanCan
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_resource # TODO this could use some refactoring
|
def load_resource # TODO this could use some refactoring
|
||||||
|
model_name = params[:controller].split('/').last.singularize
|
||||||
unless params[:action] == "index"
|
unless params[:action] == "index"
|
||||||
if params[:id]
|
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
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def authorize_resource # TODO this could use some refactoring
|
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
|
end
|
||||||
|
|
||||||
def load_and_authorize_resource
|
def load_and_authorize_resource
|
||||||
|
|
|
@ -82,4 +82,11 @@ describe CanCan::ControllerAdditions do
|
||||||
stub(@controller).authorize_resource
|
stub(@controller).authorize_resource
|
||||||
@controller.load_and_authorize_resource
|
@controller.load_and_authorize_resource
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user