varvet / pundit

Minimal authorization through OO design and pure Ruby classes
MIT License
8.24k stars 627 forks source link

Use of Struct in Headless Policies #696

Closed drewmoore closed 2 years ago

drewmoore commented 2 years ago

Thanks for this great gem! When trying to implement a headless policy, rubocop advised against this. When experimenting on my own machine, I actually found it possible to avoid the usage of Struct altogether and instead just use the exact same pattern as for our ActiveRecord models' policies. This also works for me in views using if policy(:my_resource).show?. I'm wondering if Struct is needed, and if not, if the documentation could be updated to reflect this. Thanks!

Ruby version: 2.7.3 Pundit version: 2.1.1

dgmstuart commented 2 years ago

Just came here to raise the same thing.

There's also a good blog post from The Pug Automatic going into this in some detail.

dgmstuart commented 2 years ago

@drewmoore could you expand on what you mean by "instead just use the exact same pattern as for our ActiveRecord models' policies."?

dgmstuart commented 2 years ago

@drewmoore We've discussed this and concluded that there isn't a particular reason to use Struct. We'd happily accept a PR which changes the documentation to a different approach if you have something specific in mind.

drewmoore commented 2 years ago

@drewmoore could you expand on what you mean by "instead just use the exact same pattern as for our ActiveRecord models' policies."?

Oh yes, I just meant that we are able to declare our headless policies in exactly the same manner as any other type of policy. I provided a documentation update on #717.

dgmstuart commented 2 years ago

Closed by #171