Skip to content
This repository has been archived by the owner on Sep 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request ryanb#670 from andhapp/fix-issue-664
Browse files Browse the repository at this point in the history
Namespaced Controllers not building new resource from params(regression 1.6.8)
  • Loading branch information
ryanb committed Jul 2, 2012
2 parents cad4db2 + 60bc9e9 commit 2db73e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 14 additions & 3 deletions lib/cancan/controller_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,15 @@ def name

def resource_params
if @options[:class]
@params[@options[:class].to_s.underscore.gsub('/', '_')]
else
@params[namespaced_name.to_s.underscore.gsub("/", "_")]
params_key = extract_key(@options[:class])
return @params[params_key] if @params[params_key]
end

resource_params_by_namespaced_name
end

def resource_params_by_namespaced_name
@params[extract_key(namespaced_name)]
end

def namespace
Expand Down Expand Up @@ -244,5 +249,11 @@ def collection_actions
def new_actions
[:new, :create] + [@options[:new]].flatten
end

private

def extract_key(value)
value.to_s.underscore.gsub('/', '_')
end
end
end
8 changes: 7 additions & 1 deletion spec/cancan/controller_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ class Project < ::Project; end
end

it "should build a new resource for namespaced model with hash if params[:id] is not specified" do
project = Sub::Project.create!
@params.merge!(:action => "create", 'sub_project' => {:name => "foobar"})
resource = CanCan::ControllerResource.new(@controller, :class => ::Sub::Project)
resource.load_resource
@controller.instance_variable_get(:@project).name.should == "foobar"
end

it "should build a new resource for namespaced controller and namespaced model with hash if params[:id] is not specified" do
@params.merge!(:controller => "Admin::SubProjectsController", :action => "create", 'sub_project' => {:name => "foobar"})
resource = CanCan::ControllerResource.new(@controller, :class => Project)
resource.load_resource
@controller.instance_variable_get(:@sub_project).name.should == "foobar"
end

it "should build a new resource with attributes from current ability" do
@params.merge!(:action => "new")
@ability.can(:create, Project, :name => "from conditions")
Expand Down

0 comments on commit 2db73e6

Please sign in to comment.