Browse Source

Add pagination to user views

pull/576/head
baarkerlounger 3 years ago
parent
commit
9ceb276128
  1. 7
      app/controllers/organisations_controller.rb
  2. 3
      app/controllers/users_controller.rb
  3. 6
      app/views/users/index.html.erb
  4. 4
      spec/requests/organisations_controller_spec.rb
  5. 4
      spec/requests/users_controller_spec.rb

7
app/controllers/organisations_controller.rb

@ -1,10 +1,13 @@
class OrganisationsController < ApplicationController class OrganisationsController < ApplicationController
include Pagy::Backend
before_action :authenticate_user!, except: [:index] before_action :authenticate_user!, except: [:index]
before_action :find_resource, except: [:index] before_action :find_resource, except: [:index]
before_action :authenticate_scope! before_action :authenticate_scope!
def index 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 end
def show def show
@ -12,7 +15,7 @@ class OrganisationsController < ApplicationController
end end
def users def users
@users = @organisation.users.where(active: true) @pagy, @users = pagy(@organisation.users.where(active: true))
render "users/index" render "users/index"
end end

3
app/controllers/users_controller.rb

@ -1,4 +1,5 @@
class UsersController < ApplicationController class UsersController < ApplicationController
include Pagy::Backend
include Devise::Controllers::SignInOut include Devise::Controllers::SignInOut
include Helpers::Email include Helpers::Email
before_action :authenticate_user! before_action :authenticate_user!
@ -8,7 +9,7 @@ class UsersController < ApplicationController
def index def index
redirect_to users_organisation_path(current_user.organisation) unless current_user.support? 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 end
def show; end def show; end

6
app/views/users/index.html.erb

@ -8,6 +8,11 @@
<%= govuk_button_link_to "Invite user", new_user_path, html: { method: :get } %> <%= govuk_button_link_to "Invite user", new_user_path, html: { method: :get } %>
<% end %> <% end %>
<%= govuk_table do |table| %> <%= govuk_table do |table| %>
<%= table.caption(size: "s", classes: %w[govuk-!-text-align-left govuk-!-margin-top-4 govuk-!-margin-bottom-4]) do |caption| %>
<span class="govuk-!-margin-right-4">
<strong><%= @pagy.count %></strong><span style="font-weight: normal"> total users</span>
</span>
<% end %>
<%= table.head do |head| %> <%= table.head do |head| %>
<%= head.row do |row| %> <%= head.row do |row| %>
<% row.cell(header: true, text: "Name and email adress") %> <% row.cell(header: true, text: "Name and email adress") %>
@ -25,3 +30,4 @@
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>
<%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "users" } %>

4
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(inactive_user.name)
expect(page).not_to have_content(other_org_user.name) expect(page).not_to have_content(other_org_user.name)
end end
it "shows the pagination count" do
expect(page).to have_content("2 total users")
end
end end
context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do context "with an organisation that are not in scope for the user, i.e. that they do not belong to" do

4
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).not_to have_content(inactive_user.name)
expect(page).to have_content(other_org_user.name) expect(page).to have_content(other_org_user.name)
end end
it "shows the pagination count" do
expect(page).to have_content("3 total users")
end
end end
describe "#show" do describe "#show" do

Loading…
Cancel
Save