Permalink
Browse files

fixing behavior of load_and_authorize_resource for namespaced control…

…lers - closes #3
  • Loading branch information...
1 parent 766fe86 commit 15a01a579cc4fbcf6356cb5bc53a0c8d44827a1a @ryanb committed Nov 17, 2009
Showing with 14 additions and 3 deletions.
  1. +2 −0 CHANGELOG.rdoc
  2. +5 −3 lib/cancan/controller_additions.rb
  3. +7 −0 spec/cancan/controller_additions_spec.rb
View
@@ -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
@@ -21,17 +21,19 @@ def cannot?(*args)
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
@@ -82,4 +82,11 @@ def initialize(user)
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

0 comments on commit 15a01a5

Please sign in to comment.