Skip to content
This repository
Browse code

Merge pull request #670 from andhapp/fix-issue-664

Namespaced Controllers not building new resource from params(regression 1.6.8)
  • Loading branch information...
commit 2db73e60c674b18621d86b51d0effd012aaf3f91 2 parents cad4db2 + 60bc9e9
Ryan Bates authored July 02, 2012
17  lib/cancan/controller_resource.rb
@@ -213,10 +213,15 @@ def name
213 213
 
214 214
     def resource_params
215 215
       if @options[:class]
216  
-        @params[@options[:class].to_s.underscore.gsub('/', '_')]
217  
-      else
218  
-        @params[namespaced_name.to_s.underscore.gsub("/", "_")]
  216
+        params_key = extract_key(@options[:class])
  217
+        return @params[params_key] if @params[params_key]
219 218
       end
  219
+
  220
+      resource_params_by_namespaced_name
  221
+    end
  222
+
  223
+    def resource_params_by_namespaced_name
  224
+      @params[extract_key(namespaced_name)]
220 225
     end
221 226
 
222 227
     def namespace
@@ -244,5 +249,11 @@ def collection_actions
244 249
     def new_actions
245 250
       [:new, :create] + [@options[:new]].flatten
246 251
     end
  252
+
  253
+    private
  254
+
  255
+    def extract_key(value)
  256
+       value.to_s.underscore.gsub('/', '_')
  257
+    end
247 258
   end
248 259
 end
8  spec/cancan/controller_resource_spec.rb
@@ -75,13 +75,19 @@ class Project < ::Project; end
75 75
   end
76 76
 
77 77
   it "should build a new resource for namespaced model with hash if params[:id] is not specified" do
78  
-    project = Sub::Project.create!
79 78
     @params.merge!(:action => "create", 'sub_project' => {:name => "foobar"})
80 79
     resource = CanCan::ControllerResource.new(@controller, :class => ::Sub::Project)
81 80
     resource.load_resource
82 81
     @controller.instance_variable_get(:@project).name.should == "foobar"
83 82
   end
84 83
 
  84
+  it "should build a new resource for namespaced controller and namespaced model with hash if params[:id] is not specified" do
  85
+    @params.merge!(:controller => "Admin::SubProjectsController", :action => "create", 'sub_project' => {:name => "foobar"})
  86
+    resource = CanCan::ControllerResource.new(@controller, :class => Project)
  87
+    resource.load_resource
  88
+    @controller.instance_variable_get(:@sub_project).name.should == "foobar"
  89
+  end
  90
+
85 91
   it "should build a new resource with attributes from current ability" do
86 92
     @params.merge!(:action => "new")
87 93
     @ability.can(:create, Project, :name => "from conditions")

0 notes on commit 2db73e6

Please sign in to comment.
Something went wrong with that request. Please try again.