-
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.
[FS-10] Create general customer app panel
- Loading branch information
1 parent
8694cd6
commit 1ec902c
Showing
6 changed files
with
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module App | ||
class BaseController < ApplicationController | ||
include HasCurrentAttributes | ||
include ErrorHandler | ||
|
||
before_action :authenticate_user! | ||
before_action :protect | ||
after_action :verify_authorized | ||
|
||
def policy_scope(scope) | ||
super([ :app, scope ]) | ||
end | ||
|
||
def authorize(record, query = nil) | ||
super([ :app, record ], query) | ||
end | ||
|
||
private | ||
|
||
def protect | ||
redirect_to admin_root_path unless Current.user.customer? | ||
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,7 @@ | ||
module App | ||
class CustomersController < BaseController | ||
def show | ||
@customer = authorize Current.customer | ||
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,20 @@ | ||
module App | ||
class CustomerPolicy < ApplicationPolicy | ||
# NOTE: Up to Pundit v2.3.1, the inheritance was declared as | ||
# `Scope < Scope` rather than `Scope < ApplicationPolicy::Scope`. | ||
# In most cases the behavior will be identical, but if updating existing | ||
# code, beware of possible changes to the ancestors: | ||
# https://gist.github.com/Burgestrand/4b4bc22f31c8a95c425fc0e30d7ef1f5 | ||
|
||
def show? | ||
user&.customer == Current.customer | ||
end | ||
|
||
class Scope < ApplicationPolicy::Scope | ||
# NOTE: Be explicit about which records you allow access to! | ||
# def resolve | ||
# scope.all | ||
# 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,4 @@ | ||
<h2 class="pb-2 border-bottom"> | ||
<i class="bi bi-person-fill"></i> | ||
<%= @customer.name %> | ||
</h2> |
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,35 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe App::CustomerPolicy, type: :policy do | ||
let(:customer) { build(:customer) } | ||
|
||
subject { described_class } | ||
|
||
before { Current.customer = customer } | ||
|
||
permissions :show? do | ||
context "when the user is not logged in" do | ||
let(:user) { nil } | ||
|
||
it { is_expected.not_to permit(user, customer) } | ||
end | ||
|
||
context "when the user is an admin" do | ||
let(:user) { build(:user, :admin) } | ||
|
||
it { is_expected.not_to permit(user, customer) } | ||
end | ||
|
||
context "when the user is a company" do | ||
let(:user) { build(:user, :company) } | ||
|
||
it { is_expected.not_to permit(user, customer) } | ||
end | ||
|
||
context "when the user is a customer" do | ||
let(:user) { build(:user, :customer, owner: customer) } | ||
|
||
it { is_expected.to permit(user, customer) } | ||
end | ||
end | ||
end |