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
Fix pundit namespace detection #7144
Fix pundit namespace detection #7144
Conversation
Makes sense to me, can you add a spec? |
@deivid-rodriguez Thanks for your comment. Sure, I'll add specs for this change, but if possible, I'd like to hear your thoughts regarding this PR first. The thing is, although this PR fixes policies lookup to be working as expected, it probably also will break existing policies for some users. What do you think? How can we make this transition smooth? |
I see, nice catch! So, I guess we should keep both conditions, the old one and the new one. If the old one is met, but the new one is not met, then we should keep current behavior and print a warning, so that users can accomodate their policy to the proper convention. Something like def namespace(object)
if default_policy_namespace && !object.class.to_s.include?(default_policy_namespace.to_s.camelize)
if object.class.to_s.match?(/^#{default_policy_namespace.to_s.camelize}::/)
[default_policy_namespace.to_sym, object]
else
warn "<explain the issue and how to fix it>"
object
end
else
object
end
end Makes sense? |
Ping @vlad-psh, still interested? |
d91a8d9
to
ba1b166
Compare
@deivid-rodriguez Thanks for your suggestion! I found out that actual logic would be a little bit more complicated We need first search namespaced policy (eg: We'll show a warning only if policy were found at second try. Otherwise, we fallback to Please, take a look, what do you think? If all good, I'll write the specs for it UPD: specs added |
07336a2
to
f9d97a2
Compare
Rebased onto current |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks really good, I only left some minor comments.
Also I noticed that our test app has this issue since we have an AdminUser
policy at the top level. Can you move the policy to fix the warnings being introduced?
f9d97a2
to
b36e0fe
Compare
@deivid-rodriguez Thank you for review 👍
As I can see, we don't have configured - warn "WARN: You have `pundit_policy_namespace` configured, " \
+ Deprecation.warn "You have `pundit_policy_namespace` configured, " \ I tried this, but UPD: Refactored new |
b36e0fe
to
1181ddf
Compare
Yes, Pundit is not enabled by default (perhaps it should be?) but can be enabled by setting That configuration should live at the
Interesting... I tried the above and didn't raise in the development application. I think this depends on the warning configuration of each specific application, you probably have |
@deivid-rodriguez Thanks for directing me, I found the reason. It's in https://github.com/activeadmin/activeadmin/blob/master/spec/rails_helper.rb#L29 Should I change rspec example to catch an error instead of capturing stderr?
I think this is fine as it is now. We don't have to configure |
Regarding deprecations, the motivation for having Regarding "fixing" deprecations in the test app, you're totally right, it's ok as it is, nevermind. |
1181ddf
to
de710fe
Compare
@deivid-rodriguez I decided to just add |
Sounds good! I'll have a final look soon and get this shipped with the next release. Thanks so much! |
de710fe
to
f1ba855
Compare
I just added a very minor style tweak (keep the variable to capture the exception named as Thanks so much for the outstanding work! 💪 |
Given a following config:
ActiveAdmin will search policies with
Admin::
namespace.But if model already contains the configured namespace name (anywhere in the model name), AA won't tell Pundit to search policy with configured namespace.
For example, if we have model called
ShopAdmin
, we'll look up forShopAdminPolicy
instead ofAdmin::ShopAdminPolicy
Replacing condition in https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/pundit_adapter.rb#L61 to something like this, fixes this bug: