Last active
May 18, 2021 18:17
-
-
Save stevepolitodesign/d780529ac6db7de57354354bfb012190 to your computer and use it in GitHub Desktop.
Authorize Teams with Pundit
This file contains 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
# app/controllers/posts_controller.rb | |
def show | |
raise Pundit::NotAuthorizedError unless PostPolicy.new(current_team, @post).show? | |
end |
This file contains 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
# app/policies/post_policy.rb | |
class PostPolicy < ApplicationPolicy | |
# Rename attributes for clarity | |
attr_reader :team, :record | |
def initialize(team, record) | |
@team = team | |
@record = record | |
end | |
def show? | |
team.has_access_to_feature? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment