Add code for fixing issue #664 (regression in 1.6.8).

This commit is contained in:
Anuj Dutta 2012-06-29 18:53:16 +01:00
parent 1e89b31bad
commit 60bc9e98a7
2 changed files with 21 additions and 4 deletions

View File

@ -213,10 +213,15 @@ module CanCan
def resource_params def resource_params
if @options[:class] if @options[:class]
@params[@options[:class].to_s.underscore.gsub('/', '_')] params_key = extract_key(@options[:class])
else return @params[params_key] if @params[params_key]
@params[namespaced_name.to_s.underscore.gsub("/", "_")]
end end
resource_params_by_namespaced_name
end
def resource_params_by_namespaced_name
@params[extract_key(namespaced_name)]
end end
def namespace def namespace
@ -244,5 +249,11 @@ module CanCan
def new_actions def new_actions
[:new, :create] + [@options[:new]].flatten [:new, :create] + [@options[:new]].flatten
end end
private
def extract_key(value)
value.to_s.underscore.gsub('/', '_')
end
end end
end end

View File

@ -75,13 +75,19 @@ describe CanCan::ControllerResource do
end end
it "should build a new resource for namespaced model with hash if params[:id] is not specified" do 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"}) @params.merge!(:action => "create", 'sub_project' => {:name => "foobar"})
resource = CanCan::ControllerResource.new(@controller, :class => ::Sub::Project) resource = CanCan::ControllerResource.new(@controller, :class => ::Sub::Project)
resource.load_resource resource.load_resource
@controller.instance_variable_get(:@project).name.should == "foobar" @controller.instance_variable_get(:@project).name.should == "foobar"
end 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 it "should build a new resource with attributes from current ability" do
@params.merge!(:action => "new") @params.merge!(:action => "new")
@ability.can(:create, Project, :name => "from conditions") @ability.can(:create, Project, :name => "from conditions")