diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index b417d5c6..8bd09df4 100644 --- a/lib/cancan/controller_resource.rb +++ b/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 diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index c89480c9..1c7c3e1d 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/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)