Namespaced controllers and Object is not missing constants XXX exception #530

Open
wants to merge 4 commits into
from
View
10 lib/cancan/controller_resource.rb
@@ -143,6 +143,7 @@ def resource_class
when false then name.to_sym
when nil then namespaced_name.to_s.camelize.constantize
when String then @options[:class].constantize
+ when Proc then klass = @options[:class].call(@controller); klass.is_a?(String) ? klass.constantize : klass
else @options[:class]
end
end
@@ -212,8 +213,13 @@ def name
def namespaced_name
@name || @params[:controller].sub("Controller", "").singularize.camelize.constantize
- rescue NameError
- name
+ rescue Exception => e
+ #Object is not missing Constant means we actually do have that model, un-namespaced
+ if [NameError,ArgumentError].include?(e.class)
+ name
+ else
+ raise e
+ end
end
def name_from_controller
View
16 spec/cancan/controller_resource_spec.rb
@@ -332,6 +332,22 @@ class Project < ::Project; end
@controller.instance_variable_get(:@project).should == project
end
+ it "should load the model using a custom class that is a proced constant" do
+ project = Project.create!
+ @params.merge!(:action => "show", :id => project.id)
+ resource = CanCan::ControllerResource.new(@controller, :class => lambda{ Project })
+ resource.load_resource
+ @controller.instance_variable_get(:@project).should == project
+ end
+
+ it "should load the model using a custom class that is a proced string" do
+ project = Project.create!
+ @params.merge!(:action => "show", :id => project.id)
+ resource = CanCan::ControllerResource.new(@controller, :class => lambda{ "Project" })
+ resource.load_resource
+ @controller.instance_variable_get(:@project).should == project
+ end
+
it "should load the model using a custom namespaced class" do
project = Sub::Project.create!
@params.merge!(:action => "show", :id => project.id)