Skip to content

Commit

Permalink
[FS-10] Create general customer app panel
Browse files Browse the repository at this point in the history
  • Loading branch information
beetlegius-jt committed Jan 23, 2025
1 parent 8694cd6 commit 1ec902c
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/controllers/app/base_controller.rb
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
7 changes: 7 additions & 0 deletions app/controllers/app/customers_controller.rb
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
20 changes: 20 additions & 0 deletions app/policies/app/customer_policy.rb
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
4 changes: 4 additions & 0 deletions app/views/app/customers/show.html.erb
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>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

constraints CompanyConstraint.new do
namespace :app do
root to: "customers#show"
end

namespace :admin do
resources :activities
resources :attendances, only: [ :index, :show, :new, :create, :destroy ]
Expand Down
35 changes: 35 additions & 0 deletions spec/policies/app/customer_policy_spec.rb
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

0 comments on commit 1ec902c

Please sign in to comment.