-
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-6] Create companies admin panel (#21)
* [FS-6] Add company policy * [FS-6] Add companies helper * [FS-6] Configure navigation properly * [FS-6] Add companies admin * [FS-6] Do assets precompile before running tests in CI
- Loading branch information
1 parent
bb9531f
commit bf01b08
Showing
22 changed files
with
295 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Admin | ||
class CompaniesController < BaseController | ||
before_action :set_company | ||
|
||
def show | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def update | ||
@company.update!(company_params) | ||
|
||
flash.now.notice = notice_message | ||
|
||
respond_to do |format| | ||
format.turbo_stream { render_flash } | ||
format.html { redirect_to edit_admin_company_path, notice: notice_message } | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_company | ||
@company = authorize Current.company | ||
end | ||
|
||
def company_params | ||
params.require(:company).permit(:name, :subdomain, :utc_offset, :logo) | ||
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,16 @@ | ||
module ErrorHandler | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
rescue_from ActiveRecord::RecordInvalid, with: :handle_record_invalid | ||
|
||
def handle_record_invalid(exception) | ||
flash.now.alert = exception.message | ||
|
||
respond_to do |format| | ||
format.turbo_stream { render_flash } | ||
format.html { render :edit } | ||
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,7 @@ | ||
module CompaniesHelper | ||
def options_for_utc_offset | ||
Company::UTC_OFFSETS.map do |utc_offset| | ||
[ ActiveSupport::TimeZone.seconds_to_utc_offset(utc_offset), utc_offset ] | ||
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,22 @@ | ||
class CompanyPolicy < 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&.company == Current.company | ||
end | ||
|
||
def update? | ||
user&.company == Current.company | ||
end | ||
|
||
class Scope < ApplicationPolicy::Scope | ||
# NOTE: Be explicit about which records you allow access to! | ||
# def resolve | ||
# scope.all | ||
# 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 @@ | ||
<%= link_to admin_root_path, class: "navbar-brand" do %> | ||
<% if Current.company.logo.attached? %> | ||
<%= image_tag Current.company.logo.variant(:thumb), class: "img-fluid" %> | ||
<% else %> | ||
<%= Current.company.name %> | ||
<% 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 @@ | ||
<%= nav_item t(".edit_company"), edit_admin_company_path if Current.user.company? %> |
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,33 @@ | ||
<div class="card"> | ||
<div class="card-header"> | ||
<%= t '.title' %> | ||
</div> | ||
|
||
<%= form_with(model: company, url: admin_company_path) do |form| %> | ||
<div class="card-body"> | ||
<div class="mb-3"> | ||
<%= form.label :name, class: 'form-label' %> | ||
<%= form.text_field :name, autofocus: true, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= form.label :subdomain, class: 'form-label' %> | ||
<%= form.text_field :subdomain, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= form.label :utc_offset, class: 'form-label' %> | ||
<%= form.select :utc_offset, options_for_select(options_for_utc_offset, form.object.utc_offset), {}, class: 'form-control' %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<%= form.label :logo, class: 'form-label' %> | ||
<%= form.file_field :logo, class: 'form-control' %> | ||
</div> | ||
</div> | ||
|
||
<div class="card-footer"> | ||
<%= form.submit class: 'btn btn-primary' %> | ||
</div> | ||
<% end %> | ||
</div> |
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 @@ | ||
<%= render "form", company: @company %> |
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 @@ | ||
<h1><%= Current.company.name %> %> |
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,11 @@ | ||
<span class="navbar-brand"> | ||
<% if Current.company.present? %> | ||
<% if Current.company.logo.attached? %> | ||
<%= image_tag Current.company.logo.variant(:thumb), class: "img-fluid" %> | ||
<% else %> | ||
<%= Current.company.name %> | ||
<% end %> | ||
<% else %> | ||
Fit Shift | ||
<% end %> | ||
</span> |
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,18 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe CompaniesHelper, type: :helper do | ||
describe 'options_for_utc_offset' do | ||
let(:utc_offset) { "whatever" } | ||
let(:seconds_to_utc_offset) { "text" } | ||
|
||
before do | ||
stub_const("Company::UTC_OFFSETS", [ utc_offset ]) | ||
|
||
allow(ActiveSupport::TimeZone).to receive(:seconds_to_utc_offset).with(utc_offset).and_return(seconds_to_utc_offset) | ||
end | ||
|
||
subject { helper.options_for_utc_offset } | ||
|
||
it { is_expected.to eq([ [ seconds_to_utc_offset, utc_offset ] ]) } | ||
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,35 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe CompanyPolicy, type: :policy do | ||
let(:company) { build(:company) } | ||
|
||
subject { described_class } | ||
|
||
before { Current.company = company } | ||
|
||
permissions :show?, :update? do | ||
context "when the user is not logged in" do | ||
let(:user) { nil } | ||
|
||
it { is_expected.not_to permit(user, company) } | ||
end | ||
|
||
context "when the user is an admin" do | ||
let(:user) { build(:user, :admin) } | ||
|
||
it { is_expected.not_to permit(user, company) } | ||
end | ||
|
||
context "when the user is a customer" do | ||
let(:user) { build(:user, :customer) } | ||
|
||
it { is_expected.not_to permit(user, company) } | ||
end | ||
|
||
context "when the user is a company" do | ||
let(:user) { build(:user, :company, owner: company) } | ||
|
||
it { is_expected.to permit(user, company) } | ||
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,9 @@ | ||
RSpec.configure do |config| | ||
# config.include Devise::Test::IntegrationHelpers, type: :request | ||
config.include Devise::Test::IntegrationHelpers, type: :system | ||
|
||
# TODO: Remove when Devise fixes https://github.com/heartcombo/devise/issues/5705 | ||
config.before(:each, type: :system) do | ||
Rails.application.reload_routes_unless_loaded | ||
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,52 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Admin::Companies", type: :system do | ||
let(:subdomain) { "something" } | ||
let(:company) { create(:company, subdomain:) } | ||
let(:user) { create(:user, :company, owner: company) } | ||
|
||
before { sign_in(user) } | ||
|
||
it "can update the company configuration" do | ||
visit admin_root_url(subdomain:) | ||
|
||
within("#web-nav") do | ||
find("a[href='#{edit_admin_company_path}']").click | ||
end | ||
|
||
new_name = "whatever name" | ||
new_subdomain = "whatever-subdomain" | ||
|
||
fill_in :company_name, with: new_name | ||
fill_in :company_subdomain, with: new_subdomain | ||
|
||
click_button I18n.t(:update, scope: "helpers.submit") | ||
|
||
expect(page).to have_current_path(edit_admin_company_path) | ||
expect(page).to have_content I18n.t(:update, scope: :notice) | ||
|
||
company.reload | ||
|
||
expect(company.name).to eq(new_name) | ||
expect(company.subdomain).to eq(new_subdomain) | ||
end | ||
|
||
it "shows errors when attempting to update a company with invalid attributes" do | ||
visit admin_root_url(subdomain:) | ||
|
||
within("#web-nav") do | ||
find("a[href='#{edit_admin_company_path}']").click | ||
end | ||
|
||
fill_in :company_name, with: nil | ||
|
||
click_button I18n.t(:update, scope: "helpers.submit") | ||
|
||
message = "Validation failed: Company name can't be blank" | ||
|
||
expect(page).to have_current_path(admin_company_path) | ||
expect(page).to have_content(message) | ||
|
||
expect(company.reload.name).to be_present | ||
end | ||
end |