Fixed bug with params for actions that build new instances with namespaced models
This commit is contained in:
		
							parent
							
								
									9eebeb2155
								
							
						
					
					
						commit
						baadcb923b
					
				@ -82,7 +82,10 @@ module CanCan
 | 
				
			|||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def build_resource
 | 
					    def build_resource
 | 
				
			||||||
      resource = resource_base.new(@params[name] || {})
 | 
					      params = @options[:class] \
 | 
				
			||||||
 | 
					        ? @params[@options[:class].to_s.underscore.gsub('/', '_')] \
 | 
				
			||||||
 | 
					        : @params[name] || {}
 | 
				
			||||||
 | 
					      resource = resource_base.new(params)
 | 
				
			||||||
      resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource
 | 
					      resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource
 | 
				
			||||||
      initial_attributes.each do |attr_name, value|
 | 
					      initial_attributes.each do |attr_name, value|
 | 
				
			||||||
        resource.send("#{attr_name}=", value)
 | 
					        resource.send("#{attr_name}=", value)
 | 
				
			||||||
 | 
				
			|||||||
@ -62,6 +62,14 @@ describe CanCan::ControllerResource do
 | 
				
			|||||||
    @controller.instance_variable_get(:@project).name.should == "foobar"
 | 
					    @controller.instance_variable_get(:@project).name.should == "foobar"
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it "should build a new resource for namespaced model with hash if params[:id] is not specified" do
 | 
				
			||||||
 | 
					    project = Sub::Project.create!
 | 
				
			||||||
 | 
					    @params.merge!(:action => "create", 'sub_project' => {:name => "foobar"})
 | 
				
			||||||
 | 
					    resource = CanCan::ControllerResource.new(@controller, :class => ::Sub::Project)
 | 
				
			||||||
 | 
					    resource.load_resource
 | 
				
			||||||
 | 
					    @controller.instance_variable_get(:@project).name.should == "foobar"
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it "should build a new resource with attributes from current ability" do
 | 
					  it "should build a new resource with attributes from current ability" do
 | 
				
			||||||
    @params.merge!(:action => "new")
 | 
					    @params.merge!(:action => "new")
 | 
				
			||||||
    @ability.can(:create, Project, :name => "from conditions")
 | 
					    @ability.can(:create, Project, :name => "from conditions")
 | 
				
			||||||
@ -324,6 +332,14 @@ describe CanCan::ControllerResource do
 | 
				
			|||||||
    @controller.instance_variable_get(:@project).should == project
 | 
					    @controller.instance_variable_get(:@project).should == project
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it "should load the model using a custom namespaced class" do
 | 
				
			||||||
 | 
					    project = Sub::Project.create!
 | 
				
			||||||
 | 
					    @params.merge!(:action => "show", :id => project.id)
 | 
				
			||||||
 | 
					    resource = CanCan::ControllerResource.new(@controller, :class => ::Sub::Project)
 | 
				
			||||||
 | 
					    resource.load_resource
 | 
				
			||||||
 | 
					    @controller.instance_variable_get(:@project).should == project
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it "should authorize based on resource name if class is false" do
 | 
					  it "should authorize based on resource name if class is false" do
 | 
				
			||||||
    @params.merge!(:action => "show", :id => 123)
 | 
					    @params.merge!(:action => "show", :id => 123)
 | 
				
			||||||
    stub(@controller).authorize!(:show, :project) { raise CanCan::AccessDenied }
 | 
					    stub(@controller).authorize!(:show, :project) { raise CanCan::AccessDenied }
 | 
				
			||||||
 | 
				
			|||||||
@ -31,6 +31,21 @@ class Category < SuperModel::Base
 | 
				
			|||||||
  has_many :projects
 | 
					  has_many :projects
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module Sub
 | 
				
			||||||
 | 
					  class Project < SuperModel::Base
 | 
				
			||||||
 | 
					    belongs_to :category
 | 
				
			||||||
 | 
					    attr_accessor :category # why doesn't SuperModel do this automatically?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def self.respond_to?(method, include_private = false)
 | 
				
			||||||
 | 
					      if method.to_s == "find_by_name!" # hack to simulate ActiveRecord
 | 
				
			||||||
 | 
					        true
 | 
				
			||||||
 | 
					      else
 | 
				
			||||||
 | 
					        super
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Project < SuperModel::Base
 | 
					class Project < SuperModel::Base
 | 
				
			||||||
  belongs_to :category
 | 
					  belongs_to :category
 | 
				
			||||||
  attr_accessor :category # why doesn't SuperModel do this automatically?
 | 
					  attr_accessor :category # why doesn't SuperModel do this automatically?
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user