Skip to content
This repository

Updated: port fix for namespaced params from 2.0 back to 1.6 #619

Merged
merged 1 commit into from almost 2 years ago

4 participants

Derek Prior Jeremy Friesen Chris Gunther Ryan Bates
Derek Prior
Collaborator

This should apply cleanly. The specs including in @cgunther's original commit pass as do the specs included with the conflicted commit.

This supersedes #554.

Jeremy Friesen
Collaborator
jeremyf commented May 14, 2012

:+1: The tests pass. @ryanb merge away!

Chris Gunther

Thanks for updating my patch @derekprior, I just hadn't gotten to it yet.

Ryan Bates ryanb merged commit 0c21831 into from May 14, 2012
Ryan Bates ryanb closed this May 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 2 authors.

May 14, 2012
Chris Gunther port fix for namespaced params from 2.0 back to 1.6 b347c7b
This page is out of date. Refresh to see the latest.
15  lib/cancan/controller_resource.rb
@@ -82,10 +82,7 @@ def load_collection
82 82
     end
83 83
 
84 84
     def build_resource
85  
-      params = @options[:class] \
86  
-        ? @params[@options[:class].to_s.underscore.gsub('/', '_')] \
87  
-        : @params[name] || {}
88  
-      resource = resource_base.new(params)
  85
+      resource = resource_base.new(resource_params || {})
89 86
       resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource
90 87
       initial_attributes.each do |attr_name, value|
91 88
         resource.send("#{attr_name}=", value)
@@ -95,7 +92,7 @@ def build_resource
95 92
 
96 93
     def initial_attributes
97 94
       current_ability.attributes_for(@params[:action].to_sym, resource_class).delete_if do |key, value|
98  
-        @params[name] && @params[name].include?(key)
  95
+        resource_params && resource_params.include?(key)
99 96
       end
100 97
     end
101 98
 
@@ -210,6 +207,14 @@ def name
210 207
       @name || name_from_controller
211 208
     end
212 209
 
  210
+    def resource_params
  211
+      if @options[:class]
  212
+        @params[@options[:class].to_s.underscore.gsub('/', '_')]
  213
+      else
  214
+        @params[namespaced_name.to_s.underscore.gsub("/", "_")]
  215
+      end
  216
+    end
  217
+
213 218
     def namespaced_name
214 219
       @name || @params[:controller].sub("Controller", "").singularize.camelize.constantize
215 220
     rescue NameError
12  spec/cancan/controller_resource_spec.rb
@@ -47,6 +47,18 @@ class Project < ::Project; end
47 47
     @controller.instance_variable_get(:@project).should == project
48 48
   end
49 49
 
  50
+  # Rails includes namespace in params, see issue #349
  51
+  it "should create through the namespaced params" do
  52
+    module MyEngine
  53
+      class Project < ::Project; end
  54
+    end
  55
+
  56
+    @params.merge!(:controller => "MyEngine::ProjectsController", :action => "create", :my_engine_project => {:name => "foobar"})
  57
+    resource = CanCan::ControllerResource.new(@controller)
  58
+    resource.load_resource
  59
+    @controller.instance_variable_get(:@project).name.should == "foobar"
  60
+  end
  61
+
50 62
   it "should properly load resource for namespaced controller when using '::' for namespace" do
51 63
     project = Project.create!
52 64
     @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
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.