This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
➕ Add gem "pundit" for authorization
- Loading branch information
Showing
13 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class ApplicationPolicy | ||
attr_reader :user, :record | ||
|
||
def initialize(user, record) | ||
@user = user | ||
@record = record | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class PostPolicy < ApplicationPolicy | ||
def index? | ||
true | ||
end | ||
|
||
def show? | ||
true | ||
end | ||
|
||
def create? | ||
user&.is_admin? | ||
end | ||
|
||
def new? | ||
create? | ||
end | ||
|
||
def update? | ||
user&.is_admin? | ||
end | ||
|
||
def edit? | ||
update? | ||
end | ||
|
||
def destroy? | ||
user&.is_admin? | ||
end | ||
|
||
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,10 @@ | |
factory :user do | ||
password "password" | ||
is_admin false | ||
|
||
factory :admin do | ||
is_admin true | ||
end | ||
end | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'rails_helper' | ||
|
||
describe PostPolicy do | ||
let(:post) { build(:post) } | ||
|
||
subject { PostPolicy.new(user, post) } | ||
|
||
context "for a visitor" do | ||
let(:user) { nil } | ||
|
||
it { should permit(:index) } | ||
it { should permit(:show) } | ||
|
||
it { should_not permit(:create) } | ||
it { should_not permit(:new) } | ||
it { should_not permit(:update) } | ||
it { should_not permit(:edit) } | ||
it { should_not permit(:destroy) } | ||
end | ||
|
||
context "for a user" do | ||
let(:user) { build(:user) } | ||
|
||
it { should permit(:index) } | ||
it { should permit(:show) } | ||
|
||
it { should_not permit(:create) } | ||
it { should_not permit(:new) } | ||
it { should_not permit(:update) } | ||
it { should_not permit(:edit) } | ||
it { should_not permit(:destroy) } | ||
end | ||
|
||
context "for a admin" do | ||
let(:user) { build(:admin) } | ||
|
||
it { should permit(:index) } | ||
it { should permit(:show) } | ||
it { should permit(:create) } | ||
it { should permit(:new) } | ||
it { should permit(:update) } | ||
it { should permit(:edit) } | ||
it { should permit(:destroy) } | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
RSpec::Matchers.define :permit do |action| | ||
match do |policy| | ||
policy.public_send("#{action}?") | ||
end | ||
|
||
failure_message do |policy| | ||
"#{policy.class} does not permit #{action} on #{policy.record} for #{policy.user.inspect}." | ||
end | ||
|
||
failure_message_when_negated do |policy| | ||
"#{policy.class} does not forbid #{action} on #{policy.record} for #{policy.user.inspect}." | ||
end | ||
end |