Last active
September 25, 2023 15:27
-
-
Save stevegeek/513c315ca63620c80c3a7fbef328ba02 to your computer and use it in GitHub Desktop.
Avo Authorization client for`action_policy`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| class ApplicationPolicy < ActionPolicy::Base | |
| # ActionPolicy Policy `initialize` is: | |
| # def initialize(record = nil, **params) | |
| # | |
| # But sometimes Avo calls to create a new Policy with `.new(record, user: user)` & other times with `.new(user, record)` | |
| # | |
| # So modify initialize to support both: | |
| def initialize(record_or_user = nil, record = nil, **options) | |
| user = record ? record_or_user : options[:user] | |
| super(record || record_or_user, user: user) | |
| end | |
| # ... | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # in avo initializer | |
| config.authorization_client = "Avo::ActionPolicyAuthorizationClient" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can optionally define some method overrides on
ActionPolicyAuthorizationClienttoo, so you can for example namespace your Avo policies