Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
port fix for namespaced params from 2.0 back to 1.6
  • Loading branch information
cgunther authored and derekprior committed May 14, 2012
1 parent 1cdd7b3 commit b347c7b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/cancan/controller_resource.rb
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/cancan/controller_resource_spec.rb
Expand Up @@ -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)
Expand Down

0 comments on commit b347c7b

Please sign in to comment.