Merge pull request #424 from whilefalse/master
Support for namespaced models and engines
This commit is contained in:
		
						commit
						cfb801ed8d
					
				@ -134,7 +134,7 @@ module CanCan
 | 
				
			|||||||
    def resource_class
 | 
					    def resource_class
 | 
				
			||||||
      case @options[:class]
 | 
					      case @options[:class]
 | 
				
			||||||
      when false  then name.to_sym
 | 
					      when false  then name.to_sym
 | 
				
			||||||
      when nil    then 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
 | 
				
			||||||
      else @options[:class]
 | 
					      else @options[:class]
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
@ -203,6 +203,12 @@ module CanCan
 | 
				
			|||||||
      @name || name_from_controller
 | 
					      @name || name_from_controller
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def namespaced_name
 | 
				
			||||||
 | 
					      @params[:controller].sub("Controller", "").singularize.camelize.constantize
 | 
				
			||||||
 | 
					    rescue NameError
 | 
				
			||||||
 | 
					      name
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def name_from_controller
 | 
					    def name_from_controller
 | 
				
			||||||
      @params[:controller].sub("Controller", "").underscore.split('/').last.singularize
 | 
					      @params[:controller].sub("Controller", "").underscore.split('/').last.singularize
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
				
			|||||||
@ -35,6 +35,18 @@ describe CanCan::ControllerResource do
 | 
				
			|||||||
    @controller.instance_variable_get(:@project).should == project
 | 
					    @controller.instance_variable_get(:@project).should == project
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it "should attempt to load a resource with the same namespace as the controller when using :: for namespace" do
 | 
				
			||||||
 | 
					    module MyEngine
 | 
				
			||||||
 | 
					      class Project < ::Project; end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    project = MyEngine::Project.create!
 | 
				
			||||||
 | 
					    @params.merge!(:controller => "MyEngine::ProjectsController", :action => "show", :id => project.id)
 | 
				
			||||||
 | 
					    resource = CanCan::ControllerResource.new(@controller)
 | 
				
			||||||
 | 
					    resource.load_resource
 | 
				
			||||||
 | 
					    @controller.instance_variable_get(:@project).should == project
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it "should properly load resource for namespaced controller when using '::' for namespace" do
 | 
					  it "should properly load resource for namespaced controller when using '::' for namespace" do
 | 
				
			||||||
    project = Project.create!
 | 
					    project = Project.create!
 | 
				
			||||||
    @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
 | 
					    @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user