-
Notifications
You must be signed in to change notification settings - Fork 633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to access namespaced policy with const_get #172
Comments
I think this is because of Rails' autoloading. I ran the following in a Rails console: Object.const_get "UserPolicy"
# => UserPolicy
Admin.const_get "UserPolicy"
# => UserPolicy
Admin::UserPolicy
# => Admin::UserPolicy
Admin.const_get 'UserPolicy'
# => Admin::UserPolicy If I'll look into it more when I have some time and see if there's a way to handle it in the |
My initial thought was that Rails autoloading has something to do with it too. Note that if you start with the namespaced call to const_get, it works appropriately...
I was hoping that "qualified_const_get" might work, but that doesn't seem to make a difference. In my fork of pundit, I just ended up brute-forcing it to use the namespaced policy no matter what...
You lose the fallback to the non-namespaced policy functionality with this, but that is not a big deal for my usage. |
I ran into this problem last night. It was definitely an autoload issue. If you require the policies explicitly in an initializer, it should fix the problem. To illustrate: # config/require_policies.rb
Dir[Rails.root.join('app/policies/**/*.rb')].each &method(:require)
I'd guess there are no constants under your namespace because of how autoload works. As a result, Also, how are you declaring your namespaced classes? If you embed the namespace in the class definition, you'll run into a
I really doubt you need to patch Pundit to fix this issue. |
@jnicklas Do we need to address this before releasing |
See my comment in #178. IMO the namespacing thing is a failure and we should yank it entirely before 0.3.0. Thoughts? |
@jnicklas, @thomasklemm It's not a failure it's just how autoload works in rails rails/rails#14844 rails/rails#8726 # article_policy.rb
class ArticlePolicy < ApplicationPolicy
end
require_dependency "admin/article_policy"
# admin/article_policy.rb
class Admin::ArticlePolicy < ArticlePolicy
end You don't need to remove feature, just write in readme that person need to requre dependency at the bottom. However this is not so nice, so I'm thinking maybe pundit can use modules for inheritance? Every policy usually have user and record object, so if there was one class for policy and actual rules would be taken from corresponding modules. With modules as far as I'm thinking there will be no such troubles with autoload. |
See my reply in: #178 (comment) |
There seems to be an issue with the namespace functionality for what I think is a fairly common use case. Suppose an application has a UsersController at the base and then another UsersController namespaced under "admin." Assume that each of these then have a policy; UserPolicy and Admin::UserPolicy.
If the UserPolicy is accessed first, it seems that Admin::UserPolicy can then not be accessed later via const_get.
If I open a Rails console, I see the following...
However, if I exit out of the console, re-enter the console and ask for the namespaced policy first, it works...
The text was updated successfully, but these errors were encountered: