have params hash override initial attributes when building a new resource

This commit is contained in:
Ryan Bates 2010-09-03 14:11:44 -07:00
parent 721939babd
commit 7c5243321f
2 changed files with 17 additions and 14 deletions

View File

@ -50,11 +50,11 @@ module CanCan
end end
def build_resource def build_resource
method_name = @options[:singleton] ? "build_#{name}" : "new" resource = resource_base.send(@options[:singleton] ? "build_#{name}" : "new")
resource = resource_base.send(*[method_name, @params[name]].compact)
initial_attributes.each do |name, value| initial_attributes.each do |name, value|
resource.send("#{name}=", value) resource.send("#{name}=", value)
end end
resource.attributes = @params[name] if @params[name]
resource resource
end end

View File

@ -41,23 +41,26 @@ describe CanCan::ControllerResource do
end end
it "should build a new resource with hash if params[:id] is not specified" do it "should build a new resource with hash if params[:id] is not specified" do
@params.merge!(:action => "create", :project => {:foo => "bar"}) @params.merge!(:action => "create", :project => {:name => "foobar"})
stub(Project).new("foo" => "bar") { :some_project }
resource = CanCan::ControllerResource.new(@controller) resource = CanCan::ControllerResource.new(@controller)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == :some_project @controller.instance_variable_get(:@project).name.should == "foobar"
end 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[:controller] = "projects" @params.merge!(:action => "new")
@params[:action] = "new" stub(@controller).current_ability.stub!.attributes_for(:new, Project) { {:name => "from conditions"} }
project = Object.new resource = CanCan::ControllerResource.new(@controller)
mock(Project).new { project } resource.load_resource
mock(project).name = "foobar" @controller.instance_variable_get(:@project).name.should == "from conditions"
end
it "should override initial attributes with params" do
@params.merge!(:action => "new", :project => {:name => "from params"})
stub(@controller).current_ability.stub!.attributes_for(:new, Project) { {:name => "foobar"} } stub(@controller).current_ability.stub!.attributes_for(:new, Project) { {:name => "foobar"} }
resource = CanCan::ControllerResource.new(@controller) resource = CanCan::ControllerResource.new(@controller)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).name.should == "from params"
end end
it "should not build a resource when on index action" do it "should not build a resource when on index action" do
@ -188,13 +191,13 @@ describe CanCan::ControllerResource do
end end
it "should build record through has_one association with :singleton option" do it "should build record through has_one association with :singleton option" do
@params.merge!(:action => "create", :project => :project_attributes) @params.merge!(:action => "create", :project => {:name => "foobar"})
category = Object.new category = Object.new
@controller.instance_variable_set(:@category, category) @controller.instance_variable_set(:@category, category)
stub(category).build_project(:project_attributes) { :new_project } stub(category).build_project { Project.new }
resource = CanCan::ControllerResource.new(@controller, :through => :category, :singleton => true) resource = CanCan::ControllerResource.new(@controller, :through => :category, :singleton => true)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).should == :new_project @controller.instance_variable_get(:@project).name.should == "foobar"
end end
it "should only authorize :read action on parent resource" do it "should only authorize :read action on parent resource" do