From 6c497b8dd91e1d1c9e7014f65df6f366dc4c6595 Mon Sep 17 00:00:00 2001 From: Steven Anderson Date: Wed, 20 Jul 2011 09:31:53 +0100 Subject: [PATCH 1/3] Added support for engines and namespaced models. --- lib/cancan/controller_resource.rb | 8 +++++++- spec/cancan/controller_resource_spec.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 6c41efd..8d4e367 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -134,7 +134,7 @@ module CanCan def resource_class case @options[:class] when false then name.to_sym - when nil then name.to_s.camelize.constantize + when nil then namespaced_name.to_s.camelize.constantize when String then @options[:class].constantize else @options[:class] end @@ -203,6 +203,12 @@ module CanCan @name || name_from_controller end + def namespaced_name + @params[:controller].sub("Controller", "").singularize.constantize + rescue NameError + name + end + def name_from_controller @params[:controller].sub("Controller", "").underscore.split('/').last.singularize end diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index 7033c04..0b052b4 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/spec/cancan/controller_resource_spec.rb @@ -35,6 +35,18 @@ describe CanCan::ControllerResource do @controller.instance_variable_get(:@project).should == project end + it "should attempt to load a resource with the same namespace as the controller when using :: for namespace" do + module MyEngine + class Project < ::Project; end + end + + project = MyEngine::Project.create! + @params.merge!(:controller => "MyEngine::ProjectsController", :action => "show", :id => project.id) + resource = CanCan::ControllerResource.new(@controller) + resource.load_resource + @controller.instance_variable_get(:@project).should == project + 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) From d35419fa4c094a9d4a044eb738567fbeb7b62023 Mon Sep 17 00:00:00 2001 From: Steven Anderson Date: Wed, 20 Jul 2011 10:03:50 +0100 Subject: [PATCH 2/3] Added the needed camelize to recent patch for engines and namespaced models --- lib/cancan/controller_resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 8d4e367..06a8d15 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -204,7 +204,7 @@ module CanCan end def namespaced_name - @params[:controller].sub("Controller", "").singularize.constantize + @params[:controller].sub("Controller", "").singularize.camelize.constantize rescue NameError name end From 5ab7dea2f0290237e2462072a1ab3bb156a55b12 Mon Sep 17 00:00:00 2001 From: kirkconnell Date: Tue, 20 Sep 2011 18:43:39 -0600 Subject: [PATCH 3/3] use version 1.x of with_model to avoid errors in class comparisons --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index b1f7f27..78cdd51 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,7 @@ case ENV["MODEL_ADAPTER"] when nil, "active_record" gem "sqlite3" gem "activerecord", '~> 3.0.9', :require => "active_record" - gem "with_model" + gem "with_model", '~> 0.1.5' gem "meta_where" when "data_mapper" gem "dm-core", "~> 1.0.2"