-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3653ad
commit 7abfc0d
Showing
8 changed files
with
142 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class BlogPolicy < ApplicationPolicy | ||
class Scope | ||
attr_reader :user, :scope | ||
|
||
def initialize(user, scope) | ||
@user = user | ||
@scope = scope | ||
end | ||
|
||
def resolve | ||
scope | ||
end | ||
end | ||
|
||
def index? | ||
true | ||
end | ||
|
||
def show? | ||
true | ||
end | ||
|
||
def new? | ||
create? | ||
end | ||
|
||
def create? | ||
is_admin? | ||
end | ||
|
||
def edit? | ||
update? | ||
end | ||
|
||
def update? | ||
is_admin? || is_owner? | ||
end | ||
|
||
def destroy? | ||
update? && record.posts.count == 0 | ||
end | ||
|
||
private | ||
|
||
def is_admin? | ||
user&.administrator | ||
end | ||
|
||
def is_owner? | ||
user&.id == record.user.id | ||
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,9 @@ | ||
FactoryBot.define do | ||
factory :blog do | ||
factory :valid_blog do | ||
sequence(:name) { |n| "Blog #{n}" } | ||
user { FactoryBot.create(:registered_user) } | ||
posts { [] } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FactoryBot.define do | ||
factory :post do | ||
factory :valid_post do | ||
sequence(:name) { |n| "Post #{n}" } | ||
user { FactoryBot.create(:registered_user) } | ||
blog { FactoryBot.create(:valid_blog) } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FactoryBot.define do | ||
factory :user do | ||
factory :registered_user do | ||
sequence(:email) { |n| "testuser#{n}@example.com" } | ||
password { '1234567A' } | ||
password_confirmation { '1234567A' } | ||
|
||
factory :administrator do | ||
administrator { true } | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'rails_helper' | ||
|
||
describe BlogPolicy do | ||
subject { described_class.new(user, blog) } | ||
|
||
let(:resolved_scope) { described_class::Scope.new(user, Blog.all).resolve } | ||
let(:blog) { FactoryBot.create(:valid_blog) } | ||
|
||
context 'with visitors' do | ||
let(:user) { nil } | ||
|
||
it { expect(resolved_scope).to include(blog) } | ||
it { is_expected.to permit_only_actions(%i[index show]) } | ||
it { is_expected.to forbid_actions(%i[new create edit update destroy]) } | ||
end | ||
|
||
context 'with registered users' do | ||
let(:user) { FactoryBot.create(:registered_user) } | ||
|
||
it { expect(resolved_scope).to include(blog) } | ||
it { is_expected.to permit_only_actions(%i[index show]) } | ||
it { is_expected.to forbid_actions(%i[new create edit update destroy]) } | ||
|
||
context 'when registered user is the blog owner' do | ||
before { blog.user = user } | ||
|
||
it { is_expected.to permit_only_actions(%i[index show edit update destroy]) } | ||
it { is_expected.to forbid_new_and_create_actions } | ||
|
||
context 'blog has a post' do | ||
before { blog.posts << FactoryBot.create(:valid_post) } | ||
|
||
it { is_expected.to forbid_action(:destroy) } | ||
end | ||
end | ||
end | ||
|
||
context 'with administrators' do | ||
let(:user) { FactoryBot.create(:administrator) } | ||
let(:blog) { FactoryBot.create(:valid_blog) } | ||
|
||
it { expect(resolved_scope).to include(blog) } | ||
it { is_expected.to permit_all_actions } | ||
|
||
context 'blog has a post' do | ||
before { blog.posts << FactoryBot.create(:valid_post) } | ||
|
||
it { is_expected.to forbid_action(:destroy) } | ||
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