Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Support for namespaced models and engines #424

Merged
merged 2 commits into from Sep 28, 2011

Conversation

Projects
None yet
8 participants
Contributor

whilefalse commented Jul 20, 2011

Cancan was by default assuming that none of your models are namespaced, and so if your controller is called Admin::ProjectsController, it would rightly look for a Project model not a Admin::Project model.

However, this assumption falls down when you are using engines. I had an app with a Project model, which had mounted an engine with it's own (namespaced) Project model (i.e. MyEngine::Project). Cancan was ignoring the namespacing and picking up the main app's Project in my engine.

This fix tries to find a model under the same namespace as the controller before falling back to the non-namespaced behaviour as before.

I think it makes sense to look for the namespaced model first as it is conceivable that you'd have an Admin::Project that you'd want to be used in your AdminController.

Owner

ryanb commented Jul 23, 2011

Nice, thanks for this. I'll look into getting this pulled in to 2.0.

ajrkerr commented Jul 28, 2011

This was exactly the problem I was having and the solution I was looking for. Thanks!

diegorv commented Aug 1, 2011

Nice!! Its fix my problem! :D

But i think this should work with nested namespaces too..

Example:

Admin::Rh::Users

Then, Cancan will search for this resources:

Admin::Rh::Users
Rh::Users
Users

@kiote kiote commented on the diff Aug 19, 2011

lib/cancan/controller_resource.rb
@@ -203,6 +203,12 @@ module CanCan
@name || name_from_controller
end
+ def namespaced_name
+ @params[:controller].sub("Controller", "").singularize.camelize.constantize
@kiote

kiote Aug 19, 2011

I have "mymodule/myname" in @params[:controller],
so this fix does not works for me

rails 3.0.10

I fixed this way with your help:

def namespaced_name
"#{@params[:controller].split('/').first.capitalize}::#{name.capitalize}".constantize
rescue NameError
name
end

I've been using this fork for a couple months now on 3.1 (rc and now final), works great, I have a LOT of namespacing in models and controllers.

@ryanb ryanb added a commit that referenced this pull request Sep 28, 2011

@ryanb ryanb Merge pull request #424 from whilefalse/master
Support for namespaced models and engines
cfb801e

@ryanb ryanb merged commit cfb801e into ryanb:master Sep 28, 2011

This line should read when nil then name.to_s.classify.constantize

and the lines 206 to 210 removed.

I am running a test and I will create a pull request shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment