Skip to content

Loading…

Port fix for namespaced params from 2.0 back to 1.6 #554

Closed
wants to merge 1 commit into from

4 participants

@cgunther

This is a port of the fix for #349, but back ported to the 1.6 series. Commit: c94de4a

Probably also solves #541, #528, and #524.

@Sija Sija pushed a commit to Sija/cancan that referenced this pull request
Sijawusz Pur Rahnama Merged PR #554 5621c5e
@jeremyf
Collaborator

[Verified] Tests pass. I like removing references to instance_variables in preference for method calls.

@ryanb
Owner

I like this patch but it looks like it no longer merges cleanly. If someone wants to fix I will get this pulled in.

@derekprior
Collaborator

I've rebased on master and ensured all specs pass. See the referenced pull above. Recommend closing this.

@jeremyf
Collaborator

Closing this pull request as it is superseded by #619.

@jeremyf jeremyf closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 31, 2012
  1. @cgunther
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 4 deletions.
  1. +7 −2 lib/cancan/controller_resource.rb
  2. +14 −2 spec/cancan/controller_resource_spec.rb
View
9 lib/cancan/controller_resource.rb
@@ -82,7 +82,7 @@ def load_collection
end
def build_resource
- resource = resource_base.new(@params[name] || {})
+ resource = resource_base.new(resource_params || {})
resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource
initial_attributes.each do |attr_name, value|
resource.send("#{attr_name}=", value)
@@ -92,7 +92,7 @@ def build_resource
def initial_attributes
current_ability.attributes_for(@params[:action].to_sym, resource_class).delete_if do |key, value|
- @params[name] && @params[name].include?(key)
+ resource_params && resource_params.include?(key)
end
end
@@ -207,6 +207,11 @@ def name
@name || name_from_controller
end
+ def resource_params
+ # since Rails includes the namespace in the params sent by the form (issue #349)
+ @params[namespaced_name.to_s.underscore.gsub("/", "_")]
+ end
+
def namespaced_name
@name || @params[:controller].sub("Controller", "").singularize.camelize.constantize
rescue NameError
View
16 spec/cancan/controller_resource_spec.rb
@@ -47,6 +47,18 @@ class Project < ::Project; end
@controller.instance_variable_get(:@project).should == project
end
+ # Rails includes namespace in params, see issue #349
+ it "should create through the namespaced params" do
+ module MyEngine
+ class Project < ::Project; end
+ end
+
+ @params.merge!(:controller => "MyEngine::ProjectsController", :action => "create", :my_engine_project => {:name => "foobar"})
+ resource = CanCan::ControllerResource.new(@controller)
+ resource.load_resource
+ @controller.instance_variable_get(:@project).name.should == "foobar"
+ end
+
it "should properly load resource for namespaced controller when using '::' for namespace" do
project = Project.create!
@params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)
@@ -339,7 +351,7 @@ class Project < ::Project; end
lambda { resource.load_and_authorize_resource }.should raise_error(CanCan::AccessDenied)
@controller.instance_variable_get(:@custom_project).should == project
end
-
+
it "should load resource using custom ID param" do
project = Project.create!
@params.merge!(:action => "show", :the_project => project.id)
@@ -347,7 +359,7 @@ class Project < ::Project; end
resource.load_resource
@controller.instance_variable_get(:@project).should == project
end
-
+
it "should load resource using custom find_by attribute" do
project = Project.create!(:name => "foo")
@params.merge!(:action => "show", :id => "foo")
Something went wrong with that request. Please try again.