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