diff --git a/app/controllers/admin/lieutenants_controller.rb b/app/controllers/admin/lieutenants_controller.rb index 5eb5605b2..bac4af611 100644 --- a/app/controllers/admin/lieutenants_controller.rb +++ b/app/controllers/admin/lieutenants_controller.rb @@ -4,11 +4,20 @@ def index params[:search].permit! authorize :lieutenant, :index? - @search = LieutenantSearch.new(Lieutenant.all). - search(params[:search]) + @search = LieutenantSearch.new(Lieutenant.all) + .search(params[:search]) @resources = @search.results.page(params[:page]) end + def deleted + authorize :lieutenant, :restore? + + @search = LieutenantSearch.new(Lieutenant.deleted) + .search(params[:search]) + @resources = @search.results.page(params[:page]) + render :index + end + def new @resource = Lieutenant.new authorize @resource, :create? @@ -43,6 +52,18 @@ def update notice: "User has been successfully updated." end + def restore + @resource = Lieutenant.deleted.find(params[:id]) + + authorize @resource, :restore? + @resource.restore! + + respond_with :admin, + @resource, + location: admin_lieutenants_path, + notice: "User has been successfully restored." + end + def destroy authorize @resource, :destroy? @resource.soft_delete! diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 9e0d69047..e01598011 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,6 +1,6 @@ class Admin::UsersController < Admin::BaseController respond_to :html - before_action :find_resource, except: [:index, :new, :create] + before_action :find_resource, except: [:index, :new, :create, :deleted, :restore] def index params[:search] ||= UserSearch::DEFAULT_SEARCH diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index e106dd1de..c0eaa47c3 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -34,18 +34,23 @@ def sort_link_param(search, field) end end - def user_search_count(resource) - "Showing " + user_count(resource) unless resource.none? + def user_search_count(resource, deleted = false) + "Showing " + user_count(resource, deleted) unless resource.none? end - def user_default_count(resource) - "Showing all " + user_count(resource) unless resource.none? + def user_default_count(resource, deleted = false) + if deleted + "Showing " + else + "Showing all " + end + user_count(resource, deleted) unless resource.none? end - def user_count(resource) + def user_count(resource, deleted = false) user_type = t("admin.users.role_headers.#{controller_name}") user_type = user_type.downcase unless controller_name == "lieutenants" user_type = resource.count == 1 ? user_type.singularize : user_type - "#{resource.count.to_s} #{user_type}" + deleted_msg = " deleted" if deleted + "#{resource.count.to_s}#{deleted_msg.to_s} #{user_type}" end end diff --git a/app/models/concerns/soft_delete.rb b/app/models/concerns/soft_delete.rb index b601c3560..796ad55e6 100644 --- a/app/models/concerns/soft_delete.rb +++ b/app/models/concerns/soft_delete.rb @@ -3,9 +3,15 @@ module SoftDelete included do default_scope { where(deleted: false) } + + scope :deleted, -> { unscoped.where(deleted: true) } end def soft_delete! update_column(:deleted, true) end + + def restore! + update_column(:deleted, false) + end end diff --git a/app/policies/lieutenant_policy.rb b/app/policies/lieutenant_policy.rb index b4b0bf5a8..020d273a6 100644 --- a/app/policies/lieutenant_policy.rb +++ b/app/policies/lieutenant_policy.rb @@ -11,6 +11,10 @@ def update? admin? || (advanced_lieutenant? && same_county?) end + def restore? + admin? + end + def destroy? admin? || (advanced_lieutenant? && subject != record && record.role.regular? && same_county?) end diff --git a/app/views/admin/lieutenants/_list.html.slim b/app/views/admin/lieutenants/_list.html.slim index 2e1a9317c..0e511c5a3 100644 --- a/app/views/admin/lieutenants/_list.html.slim +++ b/app/views/admin/lieutenants/_list.html.slim @@ -16,23 +16,34 @@ div role="region" aria-labelledby="table-list-lieutenancy-office-caption" tabind = sort_link f, "Confirmed on", @search, :confirmed_at th.sortable.govuk-table__header scope="col" = sort_link f, "Locked on", @search, :locked_at - th.govuk-table__header - | Edit + - if action_name == "deleted" + th.govuk-table__header + | Restore + - else + th.govuk-table__header + | Edit tbody.govuk-table__body - if resources.none? tr.govuk-table__row td.text-center colspan=100 br - p.govuk-body.p-empty No Lord Lieutenancy office users found. + - if action_name == "deleted" + p.govuk-body.p-empty No deleted Lord Lieutenancy office users found. + - else + p.govuk-body.p-empty No Lord Lieutenancy office users found. br - else - LieutenantDecorator.decorate_collection(resources).each do |lieutenant| tr.govuk-table__row - th.govuk-table__header scope="row" - = link_to lieutenant.full_name, - edit_admin_lieutenant_path(lieutenant), - class: "govuk-link", - aria: { label: "edit-#{ lieutenant.first_name.downcase }-#{ lieutenant.last_name.downcase }" } + - if action_name == "deleted" + td.govuk-table__cell scope="row" + = lieutenant.full_name + - else + th.govuk-table__header scope="row" + = link_to lieutenant.full_name, + edit_admin_lieutenant_path(lieutenant), + class: "govuk-link", + aria: { label: "edit-#{ lieutenant.first_name.downcase }-#{ lieutenant.last_name.downcase }" } td.govuk-table__cell = lieutenant.ceremonial_county.name td.govuk-table__cell @@ -59,7 +70,14 @@ div role="region" aria-labelledby="table-list-lieutenancy-office-caption" tabind .text-muted ' Not locked td.govuk-table__cell - = link_to "Edit user", edit_admin_lieutenant_path(lieutenant), - class: "govuk-link", - id: "edit-#{ lieutenant.first_name.downcase }-#{ lieutenant.last_name.downcase }", - aria: { label: "edit-#{ lieutenant.first_name.downcase }-#{ lieutenant.last_name.downcase }" } + - if action_name == "deleted" + = link_to "Restore user", restore_admin_lieutenant_path(lieutenant), + class: "govuk-link", + id: "restore-#{ lieutenant.first_name.downcase }-#{ lieutenant.last_name.downcase }", + method: :post, + aria: { label: "restore-#{ lieutenant.first_name.downcase }-#{ lieutenant.last_name.downcase }" } + - else + = link_to "Edit user", edit_admin_lieutenant_path(lieutenant), + class: "govuk-link", + id: "edit-#{ lieutenant.first_name.downcase }-#{ lieutenant.last_name.downcase }", + aria: { label: "edit-#{ lieutenant.first_name.downcase }-#{ lieutenant.last_name.downcase }" } diff --git a/app/views/admin/users/index.html.slim b/app/views/admin/users/index.html.slim index 9d41cc185..faa0910b0 100644 --- a/app/views/admin/users/index.html.slim +++ b/app/views/admin/users/index.html.slim @@ -18,6 +18,21 @@ .govuk-grid-row = render "shared/users/search_count" .govuk-button-group + - if controller_name == "lieutenants" + - if action_name == "deleted" + = link_to "Show active users", + admin_lieutenants_path, + class: "new-user govuk-button pull-right", + role: "button", + aria: { label: "Show active users" } + + - else + = link_to "Show deleted users", + deleted_admin_lieutenants_path, + class: "new-user govuk-button pull-right", + role: "button", + aria: { label: "Show deleted users" } + = link_to public_send("new_admin_#{controller_name.singularize}_path"), class: 'new-user govuk-button pull-right', role: 'button' do = t("admin.users.new_button.#{controller_name}") .clear diff --git a/app/views/shared/users/_search_count.html.slim b/app/views/shared/users/_search_count.html.slim index d7db68bd7..54270c0d9 100644 --- a/app/views/shared/users/_search_count.html.slim +++ b/app/views/shared/users/_search_count.html.slim @@ -1,6 +1,6 @@ p.govuk-body-l.govuk-grid-column-two-thirds class="govuk-!-font-weight-bold" - if @search.query? - = user_search_count(@resources) + = user_search_count(@resources, action_name == "deleted") - else - = user_default_count(@resources) + = user_default_count(@resources, action_name == "deleted") .clear diff --git a/config/routes.rb b/config/routes.rb index f81a26bc0..ec60f7ded 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -187,7 +187,12 @@ end end resources :assessors - resources :lieutenants + + resources :lieutenants do + get :deleted, on: :collection + post :restore, on: :member + end + resources :group_leaders, except: [:new, :create, :show] resources :citations, only: [:index] diff --git a/spec/features/admin/lieutenants/manage_spec.rb b/spec/features/admin/lieutenants/manage_spec.rb index 702361b4b..d44ccdaf5 100644 --- a/spec/features/admin/lieutenants/manage_spec.rb +++ b/spec/features/admin/lieutenants/manage_spec.rb @@ -52,5 +52,20 @@ expect(page).to have_no_content "Rob Bobbers" end + + it "can restore deleted lieutenant" do + Lieutenant.last.soft_delete! + + visit admin_lieutenants_path + expect(page).not_to have_content "Bob Bobbers" + + click_link "Show deleted users" + click_link "Restore user" + + expect(page).to have_content "User has been successfully restored" + + visit admin_lieutenants_path + expect(page).to have_content "Bob Bobbers" + end end end