port fix for namespaced params from 2.0 back to 1.6

This commit is contained in:
Chris Gunther 2012-01-31 12:01:48 -05:00 committed by Derek Prior
parent 1cdd7b3c18
commit b347c7b78c
2 changed files with 22 additions and 5 deletions

View File

@ -82,10 +82,7 @@ module CanCan
end end
def build_resource def build_resource
params = @options[:class] \ resource = resource_base.new(resource_params || {})
? @params[@options[:class].to_s.underscore.gsub('/', '_')] \
: @params[name] || {}
resource = resource_base.new(params)
resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource resource.send("#{parent_name}=", parent_resource) if @options[:singleton] && parent_resource
initial_attributes.each do |attr_name, value| initial_attributes.each do |attr_name, value|
resource.send("#{attr_name}=", value) resource.send("#{attr_name}=", value)
@ -95,7 +92,7 @@ module CanCan
def initial_attributes def initial_attributes
current_ability.attributes_for(@params[:action].to_sym, resource_class).delete_if do |key, value| 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
end end
@ -210,6 +207,14 @@ module CanCan
@name || name_from_controller @name || name_from_controller
end 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 def namespaced_name
@name || @params[:controller].sub("Controller", "").singularize.camelize.constantize @name || @params[:controller].sub("Controller", "").singularize.camelize.constantize
rescue NameError rescue NameError

View File

@ -47,6 +47,18 @@ describe CanCan::ControllerResource do
@controller.instance_variable_get(:@project).should == project @controller.instance_variable_get(:@project).should == project
end 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 it "should properly load resource for namespaced controller when using '::' for namespace" do
project = Project.create! project = Project.create!
@params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id) @params.merge!(:controller => "Admin::ProjectsController", :action => "show", :id => project.id)