1
0
Fork 0

Add action Staffs::OrgUnitsController#index, #show

This commit is contained in:
Alex Kotov 2019-10-01 06:59:31 +05:00
parent f6247bbb44
commit c01933060a
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
12 changed files with 270 additions and 0 deletions

View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
class Staffs::OrgUnitsController < ApplicationController
before_action :set_org_unit, only: :show
# GET /staff/org_units
def index
authorize [:staff, OrgUnit]
@org_units = policy_scope(
OrgUnit,
policy_scope_class: Staff::OrgUnitPolicy::Scope,
).page(params[:page])
end
# GET /staff/org_units/:id
def show
authorize [:staff, @org_unit]
end
private
def set_org_unit
@org_unit = OrgUnit.find params[:id]
end
end

View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
class Staff::OrgUnitPolicy < ApplicationPolicy
def index?
return false if restricted?
account&.superuser?
end
def show?
return false if restricted?
account&.superuser?
end
class Scope < Scope
def resolve
return scope.none if restricted?
return scope.all if account&.superuser?
scope.none
end
end
end

View File

@ -41,5 +41,12 @@
staff_org_unit_kinds_path %>
</li>
<% end %>
<% if policy([:staff, OrgUnit]).index? %>
<li>
<%= link_to OrgUnit.model_name.human(count: 0),
staff_org_units_path %>
</li>
<% end %>
</ul>
</div>

View File

@ -0,0 +1,27 @@
<table class="table">
<thead>
<tr>
<th scope="col">
<%= OrgUnit.human_attribute_name :short_name %>
</th>
<th scope="col">
<%= OrgUnit.human_attribute_name :name %>
</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<% org_units.each do |org_unit| %>
<tr>
<td scope="row"><%= org_unit.short_name %></td>
<td><%= org_unit.name %></td>
<td>
<% if policy([:staff, org_unit]).show? %>
<%= open_action [:staff, org_unit] %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>

View File

@ -0,0 +1,9 @@
<div class="container">
<%= nav_breadcrumb(
[translate(:staff_services), staff_root_path],
OrgUnit.model_name.human(count: 0),
) %>
<%= render partial: 'table', locals: { org_units: @org_units } %>
<%= pagination @org_units %>
</div>

View File

@ -0,0 +1,39 @@
<div class="container">
<%= nav_breadcrumb(
[translate(:staff_services), staff_root_path],
[OrgUnit.model_name.human(count: 0), staff_org_units_path],
@org_unit.name,
) %>
<dl>
<dt><%= OrgUnit.human_attribute_name :kind %></dt>
<dd>
<% if @org_unit.kind.nil? %>
<%= none %>
<% elsif policy([:staff, @org_unit.kind]).show? %>
<%= link_to @org_unit.kind.name,
[:staff, @org_unit.kind] %>
<% else %>
<%= @org_unit.kind.name %>
<% end %>
</dd>
<dt><%= OrgUnit.human_attribute_name :short_name %></dt>
<dd><%= @org_unit.short_name %></dd>
<dt><%= OrgUnit.human_attribute_name :name %></dt>
<dd><%= @org_unit.name %></dd>
<dt><%= OrgUnit.human_attribute_name :parent %></dt>
<dd>
<% if @org_unit.parent.nil? %>
<%= none %>
<% elsif policy([:staff, @org_unit.parent]).show? %>
<%= link_to @org_unit.parent.name,
[:staff, @org_unit.parent] %>
<% else %>
<%= @org_unit.parent.name %>
<% end %>
</dd>
</dl>
</div>

View File

@ -14,6 +14,10 @@ en:
federal_subject:
one: State
many: States
org_unit:
one: Organizational unit
few: Organizational unit
many: Organizational units
org_unit_kind:
one: Organizational unit type
few: Organizational unit types
@ -68,6 +72,12 @@ en:
centre: Administrative centre
number: Number
timezone: Time zone
org_unit:
id: ID
kind: Type
short_name: Short name
name: Name
parent: Parent
org_unit_kind:
id: ID
codename: Codename

View File

@ -14,6 +14,10 @@ ru:
federal_subject:
one: Регион
many: Регионы
org_unit:
one: Организационное подразделение
few: Организационные подразделения
many: Организационные подразделения
org_unit_kind:
one: Тип организационных подразделений
few: Типы организационных подразделений
@ -68,6 +72,12 @@ ru:
centre: Административный центр
number: Номер
timezone: Часовой пояс
org_unit:
id: ID
kind: Тип
short_name: Короткое развание
name: Название
parent: Родитель
org_unit_kind:
id: ID
codename: Кодовое имя

View File

@ -65,6 +65,8 @@ Rails.application.routes.draw do
resources :org_unit_kinds, param: :codename, only: %i[index show]
resources :org_units, only: %i[index show]
resources :people, only: %i[index show new create] do
resources :person_comments,
path: 'comments',

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Staff::OrgUnitPolicy do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -0,0 +1,71 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'GET /staff/org_units' do
let(:current_account) { create :superuser_account }
let :org_units_count do
[0, 1, rand(2..4), rand(5..10), rand(20..40)].sample
end
before do
sign_in current_account.user if current_account&.user
create_list :some_root_org_unit, org_units_count
get '/staff/org_units'
end
for_account_types nil, :usual do
specify do
expect(response).to have_http_status :forbidden
end
end
for_account_types :superuser do
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are no organizational units' do
let(:org_units_count) { 0 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there is one organizational unit' do
let(:org_units_count) { 1 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are few organizational units' do
let(:org_units_count) { rand 2..4 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are many organizational units' do
let(:org_units_count) { rand 5..10 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are lot of organizational units' do
let(:org_units_count) { rand 20..40 }
specify do
expect(response).to have_http_status :ok
end
end
end

View File

@ -0,0 +1,38 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'GET /staff/org_units/:id' do
let(:current_account) { create :superuser_account }
let(:some_org_unit) { create :some_root_org_unit }
def make_request
get "/staff/org_units/#{some_org_unit.id}"
end
before do
sign_in current_account.user if current_account&.user
make_request
end
for_account_types nil, :usual do
specify do
expect(response).to have_http_status :forbidden
end
end
for_account_types :superuser do
specify do
expect(response).to have_http_status :ok
end
end
context 'when organizational unit has parent' do
let(:some_org_unit) { create :some_children_org_unit }
specify do
expect(response).to have_http_status :ok
end
end
end