Permalink
Browse files

have params hash override initial attributes when building a new reso…

…urce
  • Loading branch information...
1 parent 721939b commit 7c5243321f97c3055c2adb4532a920a99b9bbce7 @ryanb committed Sep 3, 2010
Showing with 17 additions and 14 deletions.
  1. +2 −2 lib/cancan/controller_resource.rb
  2. +15 −12 spec/cancan/controller_resource_spec.rb
@@ -50,11 +50,11 @@ def load_resource_instance
end
def build_resource
- method_name = @options[:singleton] ? "build_#{name}" : "new"
- resource = resource_base.send(*[method_name, @params[name]].compact)
+ resource = resource_base.send(@options[:singleton] ? "build_#{name}" : "new")
initial_attributes.each do |name, value|
resource.send("#{name}=", value)
end
+ resource.attributes = @params[name] if @params[name]
resource
end
@@ -41,23 +41,26 @@
end
it "should build a new resource with hash if params[:id] is not specified" do
- @params.merge!(:action => "create", :project => {:foo => "bar"})
- stub(Project).new("foo" => "bar") { :some_project }
+ @params.merge!(:action => "create", :project => {:name => "foobar"})
resource = CanCan::ControllerResource.new(@controller)
resource.load_resource
- @controller.instance_variable_get(:@project).should == :some_project
+ @controller.instance_variable_get(:@project).name.should == "foobar"
end
it "should build a new resource with attributes from current ability" do
- @params[:controller] = "projects"
- @params[:action] = "new"
- project = Object.new
- mock(Project).new { project }
- mock(project).name = "foobar"
+ @params.merge!(:action => "new")
+ stub(@controller).current_ability.stub!.attributes_for(:new, Project) { {:name => "from conditions"} }
+ resource = CanCan::ControllerResource.new(@controller)
+ resource.load_resource
+ @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"} }
resource = CanCan::ControllerResource.new(@controller)
resource.load_resource
- @controller.instance_variable_get(:@project).should == project
+ @controller.instance_variable_get(:@project).name.should == "from params"
end
it "should not build a resource when on index action" do
@@ -188,13 +191,13 @@
end
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
@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.load_resource
- @controller.instance_variable_get(:@project).should == :new_project
+ @controller.instance_variable_get(:@project).name.should == "foobar"
end
it "should only authorize :read action on parent resource" do

0 comments on commit 7c52433

Please sign in to comment.