Skip to content
This repository

Init attributes in InheritedResources controller w/ specs #653

Merged
merged 4 commits into from almost 2 years ago

4 participants

Anuj Dutta Ryan Bates mccraigmccraig of the clan mccraig Mike Pack
Anuj Dutta
Collaborator

Includes the same changes as #546. Thanks to @mikepack and @mccraigmccraig for fixing it initially. I just pulled their changes and ensured that it was automerging. Making it easier to be pulled into master.

Ryan Bates ryanb merged commit 2b89dbb into from June 19, 2012
Ryan Bates ryanb closed this June 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 4 unique commits by 3 authors.

Jun 19, 2012
mccraigmccraig of the clan mccraig initialise attributes after a resource is created by an InheritedReso…
…urces controller
c2c0b86
Mike Pack Add specs for resource attributes.
Remove inconsistent line breaks.
b965f5b
Mike Pack Refactor out attribute assignment 88aba46
Anuj Dutta Fix pull request 640. For some reason github didn't allow a clean mer…
…ge althought there weren't any conflicts. Fix it so that it's easier to just merge via the UI.
a1254ca
This page is out of date. Refresh to see the latest.
4  lib/cancan/controller_resource.rb
@@ -83,6 +83,10 @@ def load_collection
83 83
 
84 84
     def build_resource
85 85
       resource = resource_base.new(resource_params || {})
  86
+      assign_attributes(resource)
  87
+    end
  88
+
  89
+    def assign_attributes(resource)
86 90
       resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource
87 91
       initial_attributes.each do |attr_name, value|
88 92
         resource.send("#{attr_name}=", value)
3  lib/cancan/inherited_resource.rb
@@ -6,7 +6,8 @@ def load_resource_instance
6 6
         @controller.send :association_chain
7 7
         @controller.instance_variable_get("@#{instance_name}")
8 8
       elsif new_actions.include? @params[:action].to_sym
9  
-        @controller.send :build_resource
  9
+        resource = @controller.send :build_resource
  10
+        assign_attributes(resource)
10 11
       else
11 12
         @controller.send :resource
12 13
       end
18  spec/cancan/inherited_resource_spec.rb
@@ -39,4 +39,22 @@
39 39
     CanCan::InheritedResource.new(@controller).load_resource
40 40
     @controller.instance_variable_get(:@projects).should == :projects
41 41
   end
  42
+
  43
+  it "should build a new resource with attributes from current ability" do
  44
+    @params[:action] = "new"
  45
+    @ability.can(:create, Project, :name => "from conditions")
  46
+    stub(@controller).build_resource { Struct.new(:name).new }
  47
+    resource = CanCan::InheritedResource.new(@controller)
  48
+    resource.load_resource
  49
+    @controller.instance_variable_get(:@project).name.should == "from conditions"
  50
+  end
  51
+
  52
+  it "should override initial attributes with params" do
  53
+    @params.merge!(:action => "new", :project => {:name => "from params"})
  54
+    @ability.can(:create, Project, :name => "from conditions")
  55
+    stub(@controller).build_resource { Struct.new(:name).new }
  56
+    resource = CanCan::ControllerResource.new(@controller)
  57
+    resource.load_resource
  58
+    @controller.instance_variable_get(:@project).name.should == "from params"
  59
+  end
42 60
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.