Browse Source

Refactor

pull/624/head
Kat 3 years ago
parent
commit
922fa0a7b1
  1. 8
      app/controllers/users_controller.rb
  2. 4
      app/models/user.rb
  3. 23
      app/views/users/deactivate.html.erb
  4. 20
      app/views/users/reactivate.html.erb
  5. 2
      app/views/users/show.html.erb
  6. 26
      app/views/users/toggle-active.html.erb

8
app/controllers/users_controller.rb

@ -81,13 +81,17 @@ class UsersController < ApplicationController
end
def deactivate
unless current_user != @user && (current_user.support? || current_user.data_coordinator?)
if current_user.can_toggle_active?(@user)
render "toggle-active", locals: { action: "deactivate" }
else
redirect_to user_path(@user)
end
end
def reactivate
unless current_user != @user && (current_user.support? || current_user.data_coordinator?)
if current_user.can_toggle_active?(@user)
render "toggle-active", locals: { action: "reactivate" }
else
redirect_to user_path(@user)
end
end

4
app/models/user.rb

@ -135,4 +135,8 @@ class User < ApplicationRecord
end
end
end
def can_toggle_active?(user)
self != user && (support? || data_coordinator?)
end
end

23
app/views/users/deactivate.html.erb

@ -1,23 +0,0 @@
<% content_for :title, current_user == @user ? "Your account" : "#{@user.name.presence || @user.email}’s account" %>
<div class="govuk-grid-row">
<%= form_for(@user, as: :user, html: { method: :patch }) do |f| %>
<div class="govuk-grid-column-two-thirds-from-desktop">
<h1 class="govuk-heading-l">
<span class="govuk-caption-l"><%= @user.name %></span>
Are you sure you want to deactivate this user?
</h1>
<p>Deactivating this user will mean they can no longer access this service to submit CORE data.</p>
<p>Any logs this user has already submitted will not be affected.</p>
<%= f.govuk_text_field :active,
value: false,
hidden: true %>
<%= f.govuk_submit "I’m sure - deactivate this user" %>
<p class="govuk-body">
<%= govuk_link_to("No - I’ve changed my mind", user_path(@user)) %>
</p>
</div>
</div>
<% end %>
</div>

20
app/views/users/reactivate.html.erb

@ -1,20 +0,0 @@
<% content_for :title, current_user == @user ? "Your account" : "#{@user.name.presence || @user.email}’s account" %>
<div class="govuk-grid-row">
<%= form_for(@user, as: :user, html: { method: :patch }) do |f| %>
<div class="govuk-grid-column-two-thirds-from-desktop">
<h1 class="govuk-heading-l">
<span class="govuk-caption-l"><%= @user.name %></span>
Are you sure you want to reactivate this user?
</h1>
<%= f.govuk_text_field :active,
value: true,
hidden: true %>
<%= f.govuk_submit "I’m sure - reactivate this user" %>
<p class="govuk-body">
<%= govuk_link_to("No - I’ve changed my mind", user_path(@user)) %>
</p>
</div>
</div>
<% end %>
</div>

2
app/views/users/show.html.erb

@ -5,7 +5,7 @@
<h1 class="govuk-heading-l">
<%= content_for(:title) %>
</h1>
<% if can_toggle_active?(@user, current_user) %>
<% if current_user.can_toggle_active?(@user) %>
<% if @user.active? %>
<%= govuk_link_to "Deactivate user", "/users/#{@user.id}/deactivate" %>
<% else %>

26
app/views/users/toggle-active.html.erb

@ -0,0 +1,26 @@
<% content_for :title, "#{action.capitalize} #{@user.name.presence || @user.email}’s account" %>
<div class="govuk-grid-row">
<%= form_for(@user, as: :user, html: { method: :patch }) do |f| %>
<div class="govuk-grid-column-two-thirds-from-desktop">
<h1 class="govuk-heading-l">
<span class="govuk-caption-l"><%= @user.name %></span>
Are you sure you want to <%= action %> this user?
</h1>
<% if action == "deactivate" %>
<p>Deactivating this user will mean they can no longer access this service to submit CORE data.</p>
<p>Any logs this user has already submitted will not be affected.</p>
<% end %>
<% active_value = action == "deactivate" ? false : true %>
<div hidden>
<%= f.govuk_text_field :active,
value: active_value %>
</div>
<%= f.govuk_submit "I’m sure - #{action} this user" %>
<p class="govuk-body">
<%= govuk_link_to("No - I’ve changed my mind", user_path(@user)) %>
</p>
</div>
</div>
<% end %>
</div>
Loading…
Cancel
Save