Skip to content
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

authorize! with no arguments not resolving namespace #14

Closed
jacobwalkr opened this issue Jun 15, 2018 · 2 comments
Closed

authorize! with no arguments not resolving namespace #14

jacobwalkr opened this issue Jun 15, 2018 · 2 comments
Labels
bug Something isn't working

Comments

@jacobwalkr
Copy link

Tell us about your environment

Ruby Version:
2.4.3

Framework Version (Rails, whatever):
Rails 5.2.0.rc2

Action Policy Version:
0.1.4

What did you do?

Called authorize! from Groups::AnnouncementsController#show (/app/controllers/groups/announcements_controller.rb) with no arguments.

What did you expect to happen?

For the policy Groups::AnnouncementPolicy (/app/policies/groups/announcement_policy.rb) or Group::AnnouncementPolicy (/app/policies/group/announcement_policy.rb) (tried both) to be found and to authorise #show.

What actually happened?

ActionPolicy::NotFound in Groups::AnnouncementsController#show
Couldn't find policy class for Announcement(...)

Moving the policy out of a directory and calling it ::AnnouncementPolicy works just fine, but I'd rather keep the namespace :)

For reference, the policy as-is:

class AnnouncementPolicy < ApplicationPolicy
  def show?
    true
  end
end
@palkan palkan added the bug Something isn't working label Jun 16, 2018
palkan added a commit that referenced this issue Jun 16, 2018
@palkan
Copy link
Owner

palkan commented Jun 16, 2018

For the policy Groups::AnnouncementPolicy (/app/policies/groups/announcement_policy.rb) or Group::AnnouncementPolicy (/app/policies/group/announcement_policy.rb) (tried both) to be found and to authorise #show.

Should work with groups/announcement_policy.rb, not group/announcement_policy.rb.

I've tried to reproduce it here: https://github.com/palkan/action_policy/tree/repro/14/tmp/issue14-app.

Works as expected. How does it differ from your setup?

Also, while I was trying to reproduce the problem I also faced the same issue; it turned out that I had a type in policy file name.

Could you try to run "Groups::AnnouncementPolicy".safe_constantize" from your rails c? Does it return a class or nil?

@jacobwalkr
Copy link
Author

Hi @palkan, thanks for your reply!

I did what you suggested:

Loading development environment (Rails 5.2.0.rc2)
# AnnouncementPolicy in policies/
irb(main):001:0> "Groups::AnnouncementPolicy".safe_constantize
=> nil
irb(main):002:0> reload!
Reloading...
=> true
# Groups::AnnouncementPolicy in policies/
irb(main):003:0> "Groups::AnnouncementPolicy".safe_constantize
LoadError: Unable to autoload constant AnnouncementPolicy, expected /app/app/policies/announcement_policy.rb to define it
        from (irb):3
irb(main):004:0> reload!
Reloading...
=> true
# Groups::AnnouncementPolicy in policies/groups/
irb(main):005:0> "Groups::AnnouncementPolicy".safe_constantize
=> Groups::AnnouncementPolicy

I've also since restarted my machine and rebuilt the Docker container after electrical maintenance and... it seems to work now. We had been restarting the web server within the Docker image (docker-compose restart app) and there was no change. I'm not sure what's brought this about but it seems to be resolved 😕. Perhaps it was some issue with Rails auto-reloading.

Sorry for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants