From c2c0b86c3a6ded6fb8ccbd85d2faa5e2221d5c23 Mon Sep 17 00:00:00 2001 From: mccraig mccraig of the clan mccraig Date: Wed, 11 Jan 2012 17:23:35 +0000 Subject: [PATCH 1/4] initialise attributes after a resource is created by an InheritedResources controller --- lib/cancan/inherited_resource.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/cancan/inherited_resource.rb b/lib/cancan/inherited_resource.rb index 81bca5b..301724a 100644 --- a/lib/cancan/inherited_resource.rb +++ b/lib/cancan/inherited_resource.rb @@ -6,7 +6,13 @@ module CanCan @controller.send :association_chain @controller.instance_variable_get("@#{instance_name}") elsif new_actions.include? @params[:action].to_sym - @controller.send :build_resource + + resource = @controller.send :build_resource + initial_attributes.each do |attr_name, value| + resource.send("#{attr_name}=", value) + end + resource + else @controller.send :resource end From b965f5bab40071e7e8c0764994ba2a6524ad523f Mon Sep 17 00:00:00 2001 From: Mike Pack Date: Mon, 4 Jun 2012 22:13:57 -0600 Subject: [PATCH 2/4] Add specs for resource attributes. Remove inconsistent line breaks. --- lib/cancan/inherited_resource.rb | 2 -- spec/cancan/inherited_resource_spec.rb | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/cancan/inherited_resource.rb b/lib/cancan/inherited_resource.rb index 301724a..098675f 100644 --- a/lib/cancan/inherited_resource.rb +++ b/lib/cancan/inherited_resource.rb @@ -6,13 +6,11 @@ module CanCan @controller.send :association_chain @controller.instance_variable_get("@#{instance_name}") elsif new_actions.include? @params[:action].to_sym - resource = @controller.send :build_resource initial_attributes.each do |attr_name, value| resource.send("#{attr_name}=", value) end resource - else @controller.send :resource end diff --git a/spec/cancan/inherited_resource_spec.rb b/spec/cancan/inherited_resource_spec.rb index dc4cc58..3779fe6 100644 --- a/spec/cancan/inherited_resource_spec.rb +++ b/spec/cancan/inherited_resource_spec.rb @@ -39,4 +39,22 @@ describe CanCan::InheritedResource do CanCan::InheritedResource.new(@controller).load_resource @controller.instance_variable_get(:@projects).should == :projects end + + it "should build a new resource with attributes from current ability" do + @params[:action] = "new" + @ability.can(:create, Project, :name => "from conditions") + stub(@controller).build_resource { Struct.new(:name).new } + resource = CanCan::InheritedResource.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"}) + @ability.can(:create, Project, :name => "from conditions") + stub(@controller).build_resource { Struct.new(:name).new } + resource = CanCan::ControllerResource.new(@controller) + resource.load_resource + @controller.instance_variable_get(:@project).name.should == "from params" + end end From 88aba4664aa15b11d54c55ebb7c30af31a59b97b Mon Sep 17 00:00:00 2001 From: Mike Pack Date: Thu, 7 Jun 2012 20:11:17 -0600 Subject: [PATCH 3/4] Refactor out attribute assignment --- lib/cancan/inherited_resource.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/cancan/inherited_resource.rb b/lib/cancan/inherited_resource.rb index 098675f..61bd333 100644 --- a/lib/cancan/inherited_resource.rb +++ b/lib/cancan/inherited_resource.rb @@ -7,10 +7,7 @@ module CanCan @controller.instance_variable_get("@#{instance_name}") elsif new_actions.include? @params[:action].to_sym resource = @controller.send :build_resource - initial_attributes.each do |attr_name, value| - resource.send("#{attr_name}=", value) - end - resource + assign_attributes(resource) else @controller.send :resource end From a1254ca1c63276adab09d03d12413972da7ac837 Mon Sep 17 00:00:00 2001 From: Anuj Dutta Date: Tue, 19 Jun 2012 00:13:19 +0100 Subject: [PATCH 4/4] Fix pull request 640. For some reason github didn't allow a clean merge althought there weren't any conflicts. Fix it so that it's easier to just merge via the UI. --- lib/cancan/controller_resource.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 83a77d1..f91a10e 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -83,6 +83,10 @@ module CanCan def build_resource resource = resource_base.new(resource_params || {}) + assign_attributes(resource) + end + + def assign_attributes(resource) resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource initial_attributes.each do |attr_name, value| resource.send("#{attr_name}=", value)