Replies: 3 comments 1 reply
-
I like the idea; should we call them "Scoping Objects" 🤔
Yeah, one of the main benefits (IMO) of the current implementation is the fact that scopes are executed in the context of a policy instance. The first idea that came into my mind is to simply pass policy as an argument: class ProjectPolicy < ApplicationPolicy
relation_scope AuthorizedProjects
end
module AuthorizedProjects
def self.call(policy, target, **options)
# ...
end
end In Rails, we can hide this by using class AuthorizedScope
attr_reader :policy
delegate_missing_to :policy
def self.call(policy, target, **options)
new(policy).call(target, **options)
end
def initialize(policy)
@policy = policy
end
end
class AuthorizedProjects < AuthorizedScope
def call(target, **options)
admin? ? target.all : target.none
end
end |
Beta Was this translation helpful? Give feedback.
0 replies
-
@palkan, can mark this discussion as resolved? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When I was migrating some existing authorization logic to Action Policy, with some pretty lengthy scoping, I noticed that due to that scoping policy class became very huge: hundreds lines of code in total.
It is hard to navigate, hard to test, etc.
Proposal
It would be awesome if ActioPolicy defined and suggested a way to move scopes definition to some classes (with inheritance, modules and all other goodies that can be used from classes) and allowed just to plug them into policy.
E.g. in some way like this:
And scope objects could be defined like this:
However, there are some questions about getting access to authorization scope defined in policy (because now it is kind of separate, independent class from policy) and etc.
WDYT?
Beta Was this translation helpful? Give feedback.
All reactions