1
0
Fork 0

Add concern PaginalController

This commit is contained in:
Alex Kotov 2020-01-04 23:51:32 +05:00
parent 8821feee1f
commit d2917b6d98
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
20 changed files with 94 additions and 328 deletions

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
module PaginalController
extend ActiveSupport::Concern
def active_page
params[:page]
end
end

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class Settings::ContactsController < ApplicationController
include PaginalController
before_action :skip_policy_scope, only: :index
before_action :set_contact_list
@ -13,7 +15,7 @@ class Settings::ContactsController < ApplicationController
def index
authorize [:settings, Contact]
@contacts = @contact_list.contacts.page(params[:page])
@contacts = @contact_list.contacts.page(active_page)
end
# POST /settings/contacts

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class Settings::SessionsController < ApplicationController
include PaginalController
before_action :skip_policy_scope, only: :index
# GET /settings/sessions
@ -8,6 +10,6 @@ class Settings::SessionsController < ApplicationController
authorize [:settings, Session]
@sessions = current_account.sessions.order(logged_at: :desc)
.page(params[:page])
.page(active_page)
end
end

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class Staffs::AccountsController < ApplicationController
include PaginalController
before_action :set_account, except: :index
# GET /staff/accounts
@ -9,7 +11,7 @@ class Staffs::AccountsController < ApplicationController
@accounts = policy_scope(
Account,
policy_scope_class: Staff::AccountPolicy::Scope,
).page(params[:page])
).page(active_page)
end
# GET /staff/accounts/:nickname

View file

@ -1,12 +1,14 @@
# frozen_string_literal: true
class Staffs::ContactNetworksController < ApplicationController
include PaginalController
# GET /staff/contact_networks
def index
authorize [:staff, ContactNetwork]
@contact_networks = policy_scope(
ContactNetwork.order(codename: :asc),
policy_scope_class: Staff::ContactNetworkPolicy::Scope,
).page(params[:page])
).page(active_page)
end
end

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class Staffs::OrgUnitKindsController < ApplicationController
include PaginalController
before_action :set_org_unit_kind, only: :show
# GET /staff/org_unit_kinds
@ -9,7 +11,7 @@ class Staffs::OrgUnitKindsController < ApplicationController
@org_unit_kinds = policy_scope(
OrgUnitKind.order(codename: :asc),
policy_scope_class: Staff::OrgUnitKindPolicy::Scope,
).page(params[:page])
).page(active_page)
end
# GET /staff/org_unit_kinds/:codename

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class Staffs::OrgUnitsController < ApplicationController
include PaginalController
before_action :set_org_unit, only: :show
# GET /staff/org_units
@ -9,7 +11,7 @@ class Staffs::OrgUnitsController < ApplicationController
@org_units = policy_scope(
OrgUnit,
policy_scope_class: Staff::OrgUnitPolicy::Scope,
).page(params[:page])
).page(active_page)
end
# GET /staff/org_units/:id

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class Staffs::PeopleController < ApplicationController
include PaginalController
before_action :set_person, except: %i[index new create]
# GET /staff/people
@ -9,7 +11,7 @@ class Staffs::PeopleController < ApplicationController
@people = policy_scope(
Person,
policy_scope_class: Staff::PersonPolicy::Scope,
).page(params[:page])
).page(active_page)
end
# GET /staff/people/:id

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class Staffs::RelationStatusesController < ApplicationController
include PaginalController
before_action :set_relation_status, only: :show
# GET /staff/relation_statuses
@ -9,7 +11,7 @@ class Staffs::RelationStatusesController < ApplicationController
@relation_statuses = policy_scope(
RelationStatus.order(codename: :asc),
policy_scope_class: Staff::RelationStatusPolicy::Scope,
).page(params[:page])
).page(active_page)
end
# GET /staff/relation_statuses/:codename

View file

@ -40,6 +40,8 @@ require_relative 'support/pundit'
require_relative 'models/shared_examples/nameable'
require_relative 'models/shared_examples/required_nameable'
require_relative 'requests/shared_examples/paginal_controller'
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin

View file

@ -20,6 +20,8 @@ RSpec.describe 'GET /settings/contacts' do
get '/settings/contacts'
end
it_behaves_like 'paginal controller', :contacts_count
for_account_types nil do
specify do
expect(response).to have_http_status :forbidden
@ -31,44 +33,4 @@ RSpec.describe 'GET /settings/contacts' do
expect(response).to have_http_status :ok
end
end
context 'when there are no contacts' do
let(:contacts_count) { 0 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there is one contact' do
let(:contacts_count) { 1 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are few contacts' do
let(:contacts_count) { rand 2..4 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are many contacts' do
let(:contacts_count) { rand 5..10 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are lot of contacts' do
let(:contacts_count) { rand 20..100 }
specify do
expect(response).to have_http_status :ok
end
end
end

View file

@ -19,6 +19,8 @@ RSpec.describe 'GET /settings/sessions' do
get '/settings/sessions'
end
it_behaves_like 'paginal controller', :sessions_count
for_account_types nil do
specify do
expect(response).to have_http_status :forbidden
@ -30,44 +32,4 @@ RSpec.describe 'GET /settings/sessions' do
expect(response).to have_http_status :ok
end
end
context 'when there are no sessions' do
let(:sessions_count) { 0 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there is one session' do
let(:sessions_count) { 1 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are few sessions' do
let(:sessions_count) { rand 2..4 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are many sessions' do
let(:sessions_count) { rand 5..10 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are lot of sessions' do
let(:sessions_count) { rand 20..100 }
specify do
expect(response).to have_http_status :ok
end
end
end

View file

@ -0,0 +1,43 @@
# frozen_string_literal: true
RSpec.shared_examples 'paginal controller' do |entities_count_var_name|
context 'when there are no entities' do
let(entities_count_var_name) { 0 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there is one entity' do
let(entities_count_var_name) { 1 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are few entities' do
let(entities_count_var_name) { rand 2..4 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are many entities' do
let(entities_count_var_name) { rand 5..10 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are lot of entities' do
let(entities_count_var_name) { rand 20..40 }
specify do
expect(response).to have_http_status :ok
end
end
end

View file

@ -17,6 +17,8 @@ RSpec.describe 'GET /staff/accounts' do
get '/staff/accounts'
end
it_behaves_like 'paginal controller', :accounts_count
for_account_types nil, :usual do
specify do
expect(response).to have_http_status :forbidden
@ -28,44 +30,4 @@ RSpec.describe 'GET /staff/accounts' do
expect(response).to have_http_status :ok
end
end
context 'when there are no accounts' do
let(:accounts_count) { 0 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there is one account' do
let(:accounts_count) { 1 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are few accounts' do
let(:accounts_count) { rand 2..4 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are many accounts' do
let(:accounts_count) { rand 5..10 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are lot of accounts' do
let(:accounts_count) { rand 20..40 }
specify do
expect(response).to have_http_status :ok
end
end
end

View file

@ -17,6 +17,8 @@ RSpec.describe 'GET /staff/contact_networks' do
get '/staff/contact_networks'
end
it_behaves_like 'paginal controller', :contact_networks_count
for_account_types nil, :usual do
specify do
expect(response).to have_http_status :forbidden
@ -28,44 +30,4 @@ RSpec.describe 'GET /staff/contact_networks' do
expect(response).to have_http_status :ok
end
end
context 'when there are no contact networks' do
let(:contact_networks_count) { 0 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there is one person' do
let(:contact_networks_count) { 1 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are few contact networks' do
let(:contact_networks_count) { rand 2..4 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are many contact networks' do
let(:contact_networks_count) { rand 5..10 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are lot of contact networks' do
let(:contact_networks_count) { rand 20..40 }
specify do
expect(response).to have_http_status :ok
end
end
end

View file

@ -17,6 +17,8 @@ RSpec.describe 'GET /staff/org_unit_kinds' do
get '/staff/org_unit_kinds'
end
it_behaves_like 'paginal controller', :org_unit_kinds_count
for_account_types nil, :usual do
specify do
expect(response).to have_http_status :forbidden
@ -28,44 +30,4 @@ RSpec.describe 'GET /staff/org_unit_kinds' do
expect(response).to have_http_status :ok
end
end
context 'when there are no organizational units' do
let(:org_unit_kinds_count) { 0 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there is one organizational unit' do
let(:org_unit_kinds_count) { 1 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are few organizational units' do
let(:org_unit_kinds_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_unit_kinds_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_unit_kinds_count) { rand 20..40 }
specify do
expect(response).to have_http_status :ok
end
end
end

View file

@ -17,6 +17,8 @@ RSpec.describe 'GET /staff/org_units' do
get '/staff/org_units'
end
it_behaves_like 'paginal controller', :org_units_count
for_account_types nil, :usual do
specify do
expect(response).to have_http_status :forbidden
@ -28,44 +30,4 @@ RSpec.describe 'GET /staff/org_units' 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

@ -17,6 +17,8 @@ RSpec.describe 'GET /staff/people' do
get '/staff/people'
end
it_behaves_like 'paginal controller', :people_count
for_account_types nil, :usual do
specify do
expect(response).to have_http_status :forbidden
@ -28,44 +30,4 @@ RSpec.describe 'GET /staff/people' do
expect(response).to have_http_status :ok
end
end
context 'when there are no people' do
let(:people_count) { 0 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there is one person' do
let(:people_count) { 1 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are few people' do
let(:people_count) { rand 2..4 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are many people' do
let(:people_count) { rand 5..10 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are lot of people' do
let(:people_count) { rand 20..40 }
specify do
expect(response).to have_http_status :ok
end
end
end

View file

@ -17,6 +17,8 @@ RSpec.describe 'GET /staff/relation_statuses' do
get '/staff/relation_statuses'
end
it_behaves_like 'paginal controller', :relation_statuses_count
for_account_types nil, :usual do
specify do
expect(response).to have_http_status :forbidden
@ -28,44 +30,4 @@ RSpec.describe 'GET /staff/relation_statuses' do
expect(response).to have_http_status :ok
end
end
context 'when there are no relation statuses' do
let(:relation_statuses_count) { 0 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there is one relation status' do
let(:relation_statuses_count) { 1 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are few relation statuses' do
let(:relation_statuses_count) { rand 2..4 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are many relation statuses' do
let(:relation_statuses_count) { rand 5..10 }
specify do
expect(response).to have_http_status :ok
end
end
context 'when there are lot of relation statuses' do
let(:relation_statuses_count) { rand 20..40 }
specify do
expect(response).to have_http_status :ok
end
end
end