Lesson 2 — BoardPolicy
Why a plain Ruby object
Pundit is the standard Rails authorisation gem and it’s excellent. But it adds indirection — a separate class hierarchy, DSL methods, and conventions to learn. A plain Ruby object is simpler to understand, easier to test, and fully under our control.
BoardPolicy answers one question per method: can this user do this
thing to this board?
|
|
The policy is deliberately simple — binary member/admin, no complex
permission matrix. Note that card deletion is member? not admin?
— any member can delete any card. As noted in the module intro, this
is a known simplification.
Restart the server so it picks up the new policies folder.
Testing the policy
|
|
Wiring policy into ApplicationController
|
|
authorize! redirects with an alert rather than rendering a 403 page
— simpler and friendlier. A dedicated error page can be added in the
finishing touches module.
Enforcing policy in BoardsController
Update the boards controller to use membership-based lookup and enforce the policy:
|
|
Board.find without scoping gives a 404 for non-existent boards and
a 403-style redirect for boards where the user has no membership. This
is the correct separation — record lookup vs access control.
Enforcing policy in ColumnsController
Columns are board structure — only admins can manage them:
|
|
Enforcing policy in CardsController
Cards are board content — any member can create, edit, move, or delete:
|
|
create_card? returns true for any member — it’s the least
restrictive card policy method, so it covers create, edit, move, and
delete correctly.