Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
syed-ali-tw committed Nov 21, 2024
1 parent eb43db7 commit 58d2133
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/editions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@
margin-top: govuk-spacing(3)
}
}

.edition__edit_assignee {
p {
padding-left: govuk-spacing(2);
font-size: 20px;
}
}
26 changes: 25 additions & 1 deletion 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 Down Expand Up @@ -106,6 +110,18 @@ def destroy
render "secondary_nav_tabs/confirm_destroy"
end

def edit_assignee
render "secondary_nav_tabs/_edit_assignee"
end

def update_assignee
@resource.assignee == params[:assignee]

Check failure on line 118 in app/controllers/editions_controller.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Lint/Void: Operator `==` used in void context.
@resource.save!
rescue StandardError
flash[: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 @@ -176,4 +192,12 @@ 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 assignee that has ever been published."
redirect_to edition_path(@resource)
end

Check failure on line 202 in app/controllers/editions_controller.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body end. (https://rubystyle.guide#empty-lines-around-bodies)
end
12 changes: 12 additions & 0 deletions app/helpers/tabbed_nav_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ def edit_nav_item(label, href, current)
},
]
end

def all_users(resource)
users = []
User.enabled.order_by([%i[name asc]]).each do |user|
users << { value: user.name, text: user.name } unless user.name == resource.assignee
end
users
end

def can_update_assignee(resource)
%w[published archived scheduled_for_publishing].exclude?(resource.state)
end
end
44 changes: 44 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,44 @@
<% 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 |f| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds edition__edit_assignee">
<%= render "govuk_publishing_components/components/radio", {
name: "assignee",
items: [{ value: @resource.assignee, text: @resource.assignee, checked: true },
{ value: "None", text: "None" }
]
} %>
<% if all_users(@resource).any? %>
<p class="govuk-body"> or </p>
<% end %>
<%= render "govuk_publishing_components/components/radio", {
name: "assignee",
items: all_users(@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", preview_edition_path(@resource), target: "_blank", rel: "noopener", class: "govuk-link"),
],
} %>
</div>
</div>
</div>
<% end %>
10 changes: 9 additions & 1 deletion app/views/editions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds editions__edit__summary">
<div class="govuk-grid-column-two-thirds editions__edit__summary ">
<%= render "govuk_publishing_components/components/summary_list", {
items: [
{
field: "Assigned to",
value: @resource.assigned_to,
edit: if current_user.has_editor_permissions?(@resource) && can_update_assignee(@resource)
{
href: edit_assignee_edition_path,
link_text: "Edit",
}
else
{}
end,
},
{
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
143 changes: 142 additions & 1 deletion test/functional/editions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,148 @@ class EditionsControllerTest < ActionController::TestCase
end
end

private
context "#edit_assignees" do
context "do not have required permissions" do
context "Welsh editors and non Welsh edition" do
setup do
login_as_welsh_editor
end

should "show permission error and redirects to edition path" do
get :edit_assignee, params: { id: @edition.id }

assert_redirected_to edition_path(@edition)
assert_equal "You do not have correct editor permissions for this action.", flash[:danger]
end
end

context "nor Welsh nor Govuk editors" do
setup do
user = FactoryBot.create(:user, name: "Stub User")
login_as(user)
end

should "show permission error and redirects to edition path" do
get :edit_assignee, params: { id: @edition.id }

assert_redirected_to edition_path(@edition)
assert_equal "You do not have correct editor permissions for this action.", flash[:danger]
end
end
end

context "has required permissions" do
context "Welsh editors and welsh edition" do
setup do
login_as_welsh_editor
end

should "be able to navigate successfully to edit assignee path" do
get :edit_assignee, params: { id: @welsh_edition.id }

assert_response :success
end
end

should "be able to navigate to the edit assignee path" do
get :edit_assignee, params: { id: @edition.id }

assert_response :success
end

%i[published scheduled_for_publishing archived].each do |edition_state|
context "edition with state '#{edition_state}' can not edit assignee" do
setup do
@edition = FactoryBot.create(:edition, state: edition_state, publish_at: Time.zone.now + 1.hour)
end

should "redirect to edition path with error message" do
get :edit_assignee, params: { id: @edition.id }

assert_redirected_to edition_path
assert_equal "Cannot edit assignee that has ever been published.", flash[:danger]
end
end
end
end
end

context "#update_assignees" do
context "do not have required permissions" do
context "Welsh editors and non Welsh edition" do
setup do
login_as_welsh_editor
end

should "show permission error and redirects to edition path" do
patch :update_assignee, params: { id: @edition.id }

assert_redirected_to edition_path(@edition)
assert_equal "You do not have correct editor permissions for this action.", flash[:danger]
end
end

context "nor Welsh nor Govuk editors" do
setup do
user = FactoryBot.create(:user, name: "Stub User")
login_as(user)
end

should "show permission error and redirects to edition path" do
patch :update_assignee, params: { id: @edition.id }

assert_redirected_to edition_path(@edition)
assert_equal "You do not have correct editor permissions for this action.", flash[:danger]
end
end
end

context "has required permissions" do
context "Welsh editors and welsh edition" do
setup do
login_as_welsh_editor
end

should "be able to navigate successfully to update assignee path" do
patch :update_assignee, params: { id: @welsh_edition.id }

assert_response :success
end
end

should "be able to navigate to the update assignee path" do
patch :update_assignee, params: { id: @edition.id }

assert_response :success
end

%i[published scheduled_for_publishing archived].each do |edition_state|
context "edition with state '#{edition_state}' can not update assignee" do
setup do
@edition = FactoryBot.create(:edition, state: edition_state, publish_at: Time.zone.now + 1.hour)
end

should "redirect to edition path with error message" do
get :edit_assignee, params: { id: @edition.id }

assert_redirected_to edition_path
assert_equal "Cannot edit assignee that has ever been published.", flash[:danger]
end
end
end

should "render edit assignee page with error if save in database fails" do
Edition.any_instance.stubs(:save!).raises(Mongoid::Errors::MongoidError.new)

patch :update_assignee, params: { id: @edition.id }

assert_template "secondary_nav_tabs/_edit_assignee"
assert_equal "Due to a service problem, the assigned person couldn't be saved", flash[:danger]
end
end
end

private

def description(edition)
edition.format.underscore.humanize.downcase
Expand Down
Loading

0 comments on commit 58d2133

Please sign in to comment.