Skip to content

Loading…

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

Merged
merged 1 commit into from

4 participants

@derekprior
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.

@jeremyf
Collaborator

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

@cgunther

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

@ryanb ryanb merged commit 0c21831 into ryanb:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 14, 2012
  1. @cgunther @derekprior
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 5 deletions.
  1. +10 −5 lib/cancan/controller_resource.rb
  2. +12 −0 spec/cancan/controller_resource_spec.rb
View
15 lib/cancan/controller_resource.rb
@@ -82,10 +82,7 @@ def load_collection
end
def build_resource
- params = @options[:class] \
- ? @params[@options[:class].to_s.underscore.gsub('/', '_')] \
- : @params[name] || {}
- resource = resource_base.new(params)
+ 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)
@@ -95,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
@@ -210,6 +207,14 @@ def name
@name || name_from_controller
end
+ def resource_params
+ if @options[:class]
+ @params[@options[:class].to_s.underscore.gsub('/', '_')]
+ else
+ @params[namespaced_name.to_s.underscore.gsub("/", "_")]
+ end
+ end
+
def namespaced_name
@name || @params[:controller].sub("Controller", "").singularize.camelize.constantize
rescue NameError
View
12 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)
Something went wrong with that request. Please try again.