Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/cancan/controller_resource.rb
Expand Up @@ -143,6 +143,7 @@ def resource_class
when false then name.to_sym when false then name.to_sym
when nil then namespaced_name.to_s.camelize.constantize when nil then namespaced_name.to_s.camelize.constantize
when String then @options[:class].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] else @options[:class]
end end
end end
Expand Down Expand Up @@ -212,8 +213,13 @@ def name


def namespaced_name def namespaced_name
@name || @params[:controller].sub("Controller", "").singularize.camelize.constantize @name || @params[:controller].sub("Controller", "").singularize.camelize.constantize
rescue NameError rescue Exception => e
name #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 end


def name_from_controller def name_from_controller
Expand Down
16 changes: 16 additions & 0 deletions spec/cancan/controller_resource_spec.rb
Expand Up @@ -332,6 +332,22 @@ class Project < ::Project; end
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end 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 it "should load the model using a custom namespaced class" do
project = Sub::Project.create! project = Sub::Project.create!
@params.merge!(:action => "show", :id => project.id) @params.merge!(:action => "show", :id => project.id)
Expand Down