Skip to content

Commit

Permalink
Merge pull request #2429 from alphagov/edit-assignee
Browse files Browse the repository at this point in the history
Enable editing the person assigned to an edition
  • Loading branch information
mtaylorgds authored Nov 28, 2024
2 parents 1932ab6 + fdf9e8e commit 791a821
Show file tree
Hide file tree
Showing 7 changed files with 434 additions and 30 deletions.
59 changes: 56 additions & 3 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ class EditionsController < InheritedResources::Base
before_action only: %i[unpublish confirm_unpublish process_unpublish] do
require_govuk_editor(redirect_path: edition_path(resource))
end
before_action only: %i[progress admin update confirm_destroy] do
before_action only: %i[progress admin update confirm_destroy edit_assignee update_assignee] do
require_editor_permissions
end
before_action only: %i[confirm_destroy destroy] do
destroyable_edition?
end

before_action only: %i[edit_assignee update_assignee] do
require_assignee_editable
end

helper_method :locale_to_language

def index
Expand All @@ -35,7 +39,7 @@ def show
alias_method :admin, :show

def update
@resource.assign_attributes(permitted_params)
@resource.assign_attributes(permitted_update_params)

if @resource.save
UpdateWorker.perform_async(resource.id.to_s)
Expand Down Expand Up @@ -106,6 +110,28 @@ def destroy
render "secondary_nav_tabs/confirm_destroy"
end

def edit_assignee
render "secondary_nav_tabs/_edit_assignee"
end

def update_assignee
assignee_id = params.require(:assignee_id)

if update_assignment(@resource, assignee_id)
flash[:success] = "Assigned person updated."
redirect_to edition_path
else
render "secondary_nav_tabs/_edit_assignee"
end
rescue ActionController::ParameterMissing
flash.now[:danger] = "Please select a person to assign, or 'None' to unassign the currently assigned person."
render "secondary_nav_tabs/_edit_assignee"
rescue StandardError => e
Rails.logger.error "Error #{e.class} #{e.message}"
flash.now[:danger] = "Due to a service problem, the assigned person couldn't be saved"
render "secondary_nav_tabs/_edit_assignee"
end

protected

def setup_view_paths
Expand Down Expand Up @@ -166,7 +192,7 @@ def progress_action_param
nil
end

def permitted_params
def permitted_update_params
params.require(:edition).permit(%i[title overview in_beta body major_change change_note])
end

Expand All @@ -176,4 +202,31 @@ def destroyable_edition?
flash[:danger] = "Cannot delete a #{description(@resource).downcase} that has ever been published."
redirect_to edition_path(@resource)
end

def require_assignee_editable
return if can_update_assignee?(@resource)

flash[:danger] = "Cannot edit the assignee of an edition that has been published."
redirect_to edition_path(@resource)
end

def update_assignment(edition, assignee_id)
return true if assignee_id == edition.assigned_to_id

if assignee_id == "none"
current_user.unassign(edition)
return true
end

assignee = User.where(id: assignee_id).first
raise StandardError, "An attempt was made to assign non-existent user '#{assignee_id}' to edition '#{edition.id}'." unless assignee

if assignee.has_editor_permissions?(@resource)
current_user.assign(edition, assignee)
return true
end

flash.now[:danger] = "Chosen assignee does not have correct editor permissions."
false
end
end
28 changes: 28 additions & 0 deletions app/helpers/tabbed_nav_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def current_tab_name
end
end

def assignee_edit_link(edition)
if current_user.has_editor_permissions?(edition) && can_update_assignee?(edition)
{
href: edit_assignee_edition_path,
link_text: "Edit",
}
else
{}
end
end

private

def all_tab_names
Expand Down Expand Up @@ -61,4 +72,21 @@ def edit_nav_item(label, href, current)
},
]
end

def available_assignee_items(resource)
items = []
unless resource.assignee.nil?
items << { value: resource.assigned_to_id, text: resource.assignee, checked: true }
items << { value: "none", text: "None" }
items << :or
end
User.enabled.order_by([%i[name asc]]).each do |user|
items << { value: user.id, text: user.name } unless user.name == resource.assignee
end
items
end

def can_update_assignee?(resource)
%w[published archived scheduled_for_publishing].exclude?(resource.state)
end
end
37 changes: 37 additions & 0 deletions app/views/editions/secondary_nav_tabs/_edit_assignee.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<% content_for :title_context, @resource.title %>
<% content_for :page_title, "Assign person" %>
<% content_for :title, "Assign person" %>

<%= form_for @resource, url: update_assignee_edition_path(@resource) do %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/radio", {
name: "assignee_id",
heading: "Choose a person to assign",
visually_hidden_heading: true,
items: available_assignee_items(@resource),
} %>
</div>

<div class="govuk-grid-column-one-third options-sidebar">
<div class="sidebar-components">
<%= render "govuk_publishing_components/components/heading", {
text: "Options",
heading_level: 3,
font_size: "s",
padding: true,
} %>

<%= render "govuk_publishing_components/components/list", {
items: [
(render "govuk_publishing_components/components/button", {
text: "Save",
margin_bottom: 3,
}),
link_to("Cancel", edition_path(@resource), class: "govuk-link govuk-link--no-visited-state"),
],
} %>
</div>
</div>
</div>
<% end %>
1 change: 1 addition & 0 deletions app/views/editions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
{
field: "Assigned to",
value: @resource.assigned_to,
edit: assignee_edit_link(@resource),
},
{
field: "Content type",
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
get "admin/confirm-destroy", to: "editions#confirm_destroy", as: "confirm_destroy"
delete "admin/delete-edition", to: "editions#destroy", as: "admin_delete"
post "progress"
get "edit_assignee"
patch "update_assignee"
post "skip_fact_check",
to: "editions#progress",
edition: {
Expand Down
Loading

0 comments on commit 791a821

Please sign in to comment.