From d2917b6d98481a81ab8d6935bf8a1181c2723acc Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 4 Jan 2020 23:51:32 +0500 Subject: [PATCH] Add concern PaginalController --- app/controllers/concerns/.keep | 0 .../concerns/paginal_controller.rb | 9 ++++ .../settings/contacts_controller.rb | 4 +- .../settings/sessions_controller.rb | 4 +- app/controllers/staffs/accounts_controller.rb | 4 +- .../staffs/contact_networks_controller.rb | 4 +- .../staffs/org_unit_kinds_controller.rb | 4 +- .../staffs/org_units_controller.rb | 4 +- app/controllers/staffs/people_controller.rb | 4 +- .../staffs/relation_statuses_controller.rb | 4 +- spec/rails_helper.rb | 2 + spec/requests/settings/contacts/index_spec.rb | 42 +----------------- spec/requests/settings/sessions/index_spec.rb | 42 +----------------- .../shared_examples/paginal_controller.rb | 43 +++++++++++++++++++ spec/requests/staff/accounts/index_spec.rb | 42 +----------------- .../staff/contact_networks/index_spec.rb | 42 +----------------- .../staff/org_unit_kinds/index_spec.rb | 42 +----------------- spec/requests/staff/org_units/index_spec.rb | 42 +----------------- spec/requests/staff/people/index_spec.rb | 42 +----------------- .../staff/relation_statuses/index_spec.rb | 42 +----------------- 20 files changed, 94 insertions(+), 328 deletions(-) delete mode 100644 app/controllers/concerns/.keep create mode 100644 app/controllers/concerns/paginal_controller.rb create mode 100644 spec/requests/shared_examples/paginal_controller.rb diff --git a/app/controllers/concerns/.keep b/app/controllers/concerns/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/app/controllers/concerns/paginal_controller.rb b/app/controllers/concerns/paginal_controller.rb new file mode 100644 index 0000000..e7734e3 --- /dev/null +++ b/app/controllers/concerns/paginal_controller.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module PaginalController + extend ActiveSupport::Concern + + def active_page + params[:page] + end +end diff --git a/app/controllers/settings/contacts_controller.rb b/app/controllers/settings/contacts_controller.rb index b3d91a7..d701ee7 100644 --- a/app/controllers/settings/contacts_controller.rb +++ b/app/controllers/settings/contacts_controller.rb @@ -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 diff --git a/app/controllers/settings/sessions_controller.rb b/app/controllers/settings/sessions_controller.rb index e5ded84..803c405 100644 --- a/app/controllers/settings/sessions_controller.rb +++ b/app/controllers/settings/sessions_controller.rb @@ -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 diff --git a/app/controllers/staffs/accounts_controller.rb b/app/controllers/staffs/accounts_controller.rb index ae20f11..f718ae0 100644 --- a/app/controllers/staffs/accounts_controller.rb +++ b/app/controllers/staffs/accounts_controller.rb @@ -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 diff --git a/app/controllers/staffs/contact_networks_controller.rb b/app/controllers/staffs/contact_networks_controller.rb index a47c330..0e28895 100644 --- a/app/controllers/staffs/contact_networks_controller.rb +++ b/app/controllers/staffs/contact_networks_controller.rb @@ -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 diff --git a/app/controllers/staffs/org_unit_kinds_controller.rb b/app/controllers/staffs/org_unit_kinds_controller.rb index 8ea02d1..9d1956c 100644 --- a/app/controllers/staffs/org_unit_kinds_controller.rb +++ b/app/controllers/staffs/org_unit_kinds_controller.rb @@ -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 diff --git a/app/controllers/staffs/org_units_controller.rb b/app/controllers/staffs/org_units_controller.rb index 12adb57..f21fe5d 100644 --- a/app/controllers/staffs/org_units_controller.rb +++ b/app/controllers/staffs/org_units_controller.rb @@ -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 diff --git a/app/controllers/staffs/people_controller.rb b/app/controllers/staffs/people_controller.rb index e0d4ac9..71147e3 100644 --- a/app/controllers/staffs/people_controller.rb +++ b/app/controllers/staffs/people_controller.rb @@ -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 diff --git a/app/controllers/staffs/relation_statuses_controller.rb b/app/controllers/staffs/relation_statuses_controller.rb index 428452a..4f28396 100644 --- a/app/controllers/staffs/relation_statuses_controller.rb +++ b/app/controllers/staffs/relation_statuses_controller.rb @@ -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 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index f503b82..9539ffc 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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 diff --git a/spec/requests/settings/contacts/index_spec.rb b/spec/requests/settings/contacts/index_spec.rb index 4137bec..91732cb 100644 --- a/spec/requests/settings/contacts/index_spec.rb +++ b/spec/requests/settings/contacts/index_spec.rb @@ -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 diff --git a/spec/requests/settings/sessions/index_spec.rb b/spec/requests/settings/sessions/index_spec.rb index b7e4833..f565dc3 100644 --- a/spec/requests/settings/sessions/index_spec.rb +++ b/spec/requests/settings/sessions/index_spec.rb @@ -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 diff --git a/spec/requests/shared_examples/paginal_controller.rb b/spec/requests/shared_examples/paginal_controller.rb new file mode 100644 index 0000000..d253e7b --- /dev/null +++ b/spec/requests/shared_examples/paginal_controller.rb @@ -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 diff --git a/spec/requests/staff/accounts/index_spec.rb b/spec/requests/staff/accounts/index_spec.rb index 87b7374..1654a17 100644 --- a/spec/requests/staff/accounts/index_spec.rb +++ b/spec/requests/staff/accounts/index_spec.rb @@ -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 diff --git a/spec/requests/staff/contact_networks/index_spec.rb b/spec/requests/staff/contact_networks/index_spec.rb index 453e02e..7c6b9fd 100644 --- a/spec/requests/staff/contact_networks/index_spec.rb +++ b/spec/requests/staff/contact_networks/index_spec.rb @@ -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 diff --git a/spec/requests/staff/org_unit_kinds/index_spec.rb b/spec/requests/staff/org_unit_kinds/index_spec.rb index d022da4..8cb355b 100644 --- a/spec/requests/staff/org_unit_kinds/index_spec.rb +++ b/spec/requests/staff/org_unit_kinds/index_spec.rb @@ -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 diff --git a/spec/requests/staff/org_units/index_spec.rb b/spec/requests/staff/org_units/index_spec.rb index f01d22d..3771269 100644 --- a/spec/requests/staff/org_units/index_spec.rb +++ b/spec/requests/staff/org_units/index_spec.rb @@ -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 diff --git a/spec/requests/staff/people/index_spec.rb b/spec/requests/staff/people/index_spec.rb index 9cc87c9..e7cf7e8 100644 --- a/spec/requests/staff/people/index_spec.rb +++ b/spec/requests/staff/people/index_spec.rb @@ -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 diff --git a/spec/requests/staff/relation_statuses/index_spec.rb b/spec/requests/staff/relation_statuses/index_spec.rb index 0b86d1c..35c2f46 100644 --- a/spec/requests/staff/relation_statuses/index_spec.rb +++ b/spec/requests/staff/relation_statuses/index_spec.rb @@ -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