From 78cbcf1db97a5574258c8327a891d1c444941d80 Mon Sep 17 00:00:00 2001 From: Oliver Morgan Date: Wed, 30 May 2012 12:39:10 +0100 Subject: [PATCH 1/3] Named resources were not being loaded correctly. Fixes #633 --- lib/cancan/controller_resource.rb | 2 +- spec/cancan/controller_resource_spec.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 1d86175..ca5b446 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -145,7 +145,7 @@ module CanCan def resource_class case @options[:class] when false then name.to_sym - when nil then namespaced_name.to_s.camelize.constantize + when nil then (@name || namespaced_name).to_s.classify.constantize when String then @options[:class].constantize else @options[:class] end diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index bfc2bcc..07294b2 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/spec/cancan/controller_resource_spec.rb @@ -222,6 +222,19 @@ describe CanCan::ControllerResource do } @controller.instance_variable_get(:@project).should be_nil end + + it "named resources should not be loaded independently of the controller" do + category = Category.create! + @params.merge!(:action => "new", :category_id => category.id) + + CanCan::ControllerResource.new(@controller, :category, :load => true).process + CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process + + @controller.instance_variable_get(:@category).should eq(category) + + project = @controller.instance_variable_get(:@project) + project.category.should eq(category) + end it "authorizes nested resource through parent association on index action" do pending From 245b83f6b443a25d8563f94f2d4a563ac6583576 Mon Sep 17 00:00:00 2001 From: Oliver Morgan Date: Thu, 31 May 2012 10:45:55 +0100 Subject: [PATCH 2/3] Classify causes plural model names to be incorrectly renamed Some model names will be renamed incorrectly e.g. 'business'. It should be the responsibility of the user to make sure they use a name that directly corresponds to the model name. The only filtering performed should be camelize. --- lib/cancan/controller_resource.rb | 2 +- spec/cancan/controller_resource_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index ca5b446..1d6a3e0 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -145,7 +145,7 @@ module CanCan def resource_class case @options[:class] when false then name.to_sym - when nil then (@name || namespaced_name).to_s.classify.constantize + when nil then (@name || namespaced_name).to_s.camelize.constantize when String then @options[:class].constantize else @options[:class] end diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index 07294b2..5a2981e 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/spec/cancan/controller_resource_spec.rb @@ -222,8 +222,8 @@ describe CanCan::ControllerResource do } @controller.instance_variable_get(:@project).should be_nil end - - it "named resources should not be loaded independently of the controller" do + + it "named resources should be loaded independently of the controller name" do category = Category.create! @params.merge!(:action => "new", :category_id => category.id) From 354e34b8ab05ad10735c6a352618fb905d9010f0 Mon Sep 17 00:00:00 2001 From: Oliver Morgan Date: Mon, 4 Jun 2012 17:44:33 +0100 Subject: [PATCH 3/3] Fixed bug where parent resources were being regarded as children --- lib/cancan/controller_resource.rb | 4 ++-- spec/cancan/controller_resource_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 1d6a3e0..355ebb6 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -145,7 +145,7 @@ module CanCan def resource_class case @options[:class] when false then name.to_sym - when nil then (@name || namespaced_name).to_s.camelize.constantize + when nil then namespaced_name.to_s.camelize.constantize when String then @options[:class].constantize else @options[:class] end @@ -230,7 +230,7 @@ module CanCan end def namespaced_name - @params[:controller].sub("Controller", "").singularize.camelize.constantize + (@name || @params[:controller].sub("Controller", "")).singularize.camelize.constantize rescue NameError name end diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index 5a2981e..e37e9ec 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/spec/cancan/controller_resource_spec.rb @@ -235,6 +235,18 @@ describe CanCan::ControllerResource do project = @controller.instance_variable_get(:@project) project.category.should eq(category) end + + it "parent resources shouldn't be altered" do + category = Category.create! + @params.merge!(:action => "create", :category_id => category.id, :project => { :name => 'foo' }) + + CanCan::ControllerResource.new(@controller, :category, :load => true).process + CanCan::ControllerResource.new(@controller, :project, :load => true, :through => :category).process + + project = @controller.instance_variable_get(:@project) + project.new_record?.should eq(true) + project.name.should eq('foo') + end it "authorizes nested resource through parent association on index action" do pending