From 9ceb27612859d58388cfc3f178895943bf65d764 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 13 May 2022 12:46:30 +0100 Subject: [PATCH] Add pagination to user views --- app/controllers/organisations_controller.rb | 7 +++++-- app/controllers/users_controller.rb | 3 ++- app/views/users/index.html.erb | 6 ++++++ spec/requests/organisations_controller_spec.rb | 4 ++++ spec/requests/users_controller_spec.rb | 4 ++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 1ffddcf08..9401fae3a 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -1,10 +1,13 @@ class OrganisationsController < ApplicationController + include Pagy::Backend before_action :authenticate_user!, except: [:index] before_action :find_resource, except: [:index] before_action :authenticate_scope! def index - @organisations = current_user.support? ? Organisation.all : @user.organisation + redirect_to organisation_path(current_user.organisation) unless current_user.support? + + @pagy, @organisations = pagy(Organisation.all) end def show @@ -12,7 +15,7 @@ class OrganisationsController < ApplicationController end def users - @users = @organisation.users.where(active: true) + @pagy, @users = pagy(@organisation.users.where(active: true)) render "users/index" end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bdde9113b..cd4c3254e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,5 @@ class UsersController < ApplicationController + include Pagy::Backend include Devise::Controllers::SignInOut include Helpers::Email before_action :authenticate_user! @@ -8,7 +9,7 @@ class UsersController < ApplicationController def index redirect_to users_organisation_path(current_user.organisation) unless current_user.support? - @users = User.all.where(active: true) + @pagy, @users = pagy(User.all.where(active: true)) end def show; end diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 086ff12e9..cff822745 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -8,6 +8,11 @@ <%= govuk_button_link_to "Invite user", new_user_path, html: { method: :get } %> <% end %> <%= govuk_table do |table| %> + <%= table.caption(size: "s", classes: %w[govuk-!-text-align-left govuk-!-margin-top-4 govuk-!-margin-bottom-4]) do |caption| %> + + <%= @pagy.count %> total users + + <% end %> <%= table.head do |head| %> <%= head.row do |row| %> <% row.cell(header: true, text: "Name and email adress") %> @@ -25,3 +30,4 @@ <% end %> <% end %> <% end %> +<%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "users" } %> diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 444d1e065..f9e164b38 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -136,6 +136,10 @@ RSpec.describe OrganisationsController, type: :request do expect(page).not_to have_content(inactive_user.name) expect(page).not_to have_content(other_org_user.name) end + + it "shows the pagination count" do + expect(page).to have_content("2 total users") + end end context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index 26f9c24c5..b75cc5b4f 100644 --- a/spec/requests/users_controller_spec.rb +++ b/spec/requests/users_controller_spec.rb @@ -724,6 +724,10 @@ RSpec.describe UsersController, type: :request do expect(page).not_to have_content(inactive_user.name) expect(page).to have_content(other_org_user.name) end + + it "shows the pagination count" do + expect(page).to have_content("3 total users") + end end describe "#show" do