-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7950 from alphagov/email-signup-override-for-doc-…
…collections UI to add a taxonomy topic email override to document collections
- Loading branch information
Showing
20 changed files
with
726 additions
and
4 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
app/controllers/admin/document_collection_email_subscriptions_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
class Admin::DocumentCollectionEmailSubscriptionsController < Admin::BaseController | ||
include Admin::DocumentCollectionEmailOverrideHelper | ||
before_action :load_document_collection | ||
before_action :authorise_user | ||
layout "design_system" | ||
|
||
def edit | ||
@topic_list_select_presenter = TopicListSelectPresenter.new(@collection.taxonomy_topic_email_override) | ||
end | ||
|
||
def update | ||
if user_has_selected_taxonomy_topic_emails? | ||
if all_required_params_present? | ||
@collection.update!(taxonomy_topic_email_override: params["selected_taxon_content_id"]) | ||
else | ||
build_missing_params_flash | ||
return redirect_to form_with_stored_params | ||
end | ||
else | ||
@collection.update!(taxonomy_topic_email_override: nil) | ||
end | ||
build_flash("notice") | ||
redirect_to edit_admin_document_collection_path(@collection) | ||
rescue ActiveRecord::RecordInvalid | ||
redirect_to edit_admin_document_collection_path(@collection) | ||
end | ||
|
||
private | ||
|
||
def all_required_params_present? | ||
required_params.select { |key| params[key].present? } == required_params | ||
end | ||
|
||
def required_params | ||
%w[selected_taxon_content_id email_override_confirmation] | ||
end | ||
|
||
def form_with_stored_params | ||
admin_document_collection_edit_email_subscription_path(@collection, params_to_store_state_of_failed_form_submission) | ||
end | ||
|
||
def build_missing_params_flash | ||
missing_params = required_params.select { |required_param| params[required_param].blank? } | ||
missing_params.each { |key| build_flash(key) } | ||
end | ||
|
||
def params_to_store_state_of_failed_form_submission | ||
{ | ||
"selected_taxon_content_id" => params["selected_taxon_content_id"], | ||
"override_email_subscriptions" => params["override_email_subscriptions"], | ||
} | ||
end | ||
|
||
def load_document_collection | ||
@collection = DocumentCollection.find(params[:document_collection_id]) | ||
end | ||
|
||
def authorise_user | ||
redirect_to edit_admin_document_collection_path(@collection) unless current_user.can_edit_email_overrides? | ||
end | ||
|
||
def build_flash(key) | ||
flash[key] = { | ||
"selected_taxon_content_id" => "You must choose a topic", | ||
"email_override_confirmation" => "You must confirm you’re happy with the email notification settings", | ||
"notice" => "You’ve selected the email notification settings. #{confirmation_message}. You will not be able to change these settings after you publish the collection.", | ||
}[key] | ||
end | ||
|
||
def confirmation_message | ||
if @collection.taxonomy_topic_email_override.present? | ||
"You’ve chosen ‘Emails about the topic’ and the topic #{taxonomy_topic_email_override_title(@collection)}" | ||
else | ||
"You’ve chosen ‘Emails about the page’" | ||
end | ||
end | ||
|
||
def user_has_selected_taxonomy_topic_emails? | ||
params[:override_email_subscriptions] == "true" | ||
end | ||
end |
29 changes: 29 additions & 0 deletions
29
app/helpers/admin/document_collection_email_override_helper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Admin::DocumentCollectionEmailOverrideHelper | ||
def taxonomy_topic_cannot_be_set?(collection) | ||
collection.document.live_edition_id.present? | ||
end | ||
|
||
def has_page_level_notifications?(collection) | ||
collection.taxonomy_topic_email_override.nil? | ||
end | ||
|
||
def taxonomy_topic_email_override_title(collection) | ||
taxonomy_topic_content_item(collection).fetch("title", "") | ||
end | ||
|
||
def taxonomy_topic_email_override_base_path(collection) | ||
taxonomy_topic_content_item(collection).fetch("base_path", "") | ||
end | ||
|
||
def taxonomy_topic_content_item(collection) | ||
@taxonomy_topic_content_item ||= Services.publishing_api | ||
.get_content(collection.taxonomy_topic_email_override) | ||
.to_h | ||
rescue GdsApi::HTTPNotFound | ||
{} | ||
end | ||
|
||
def emails_about_this_topic_checked?(collection, params) | ||
collection.taxonomy_topic_email_override.present? || (params["override_email_subscriptions"] == "true") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
class TopicListSelectPresenter | ||
# This presenter is used to select a taxonomy topic email override for some document collections | ||
# We are only going to show the branches which the dept has told us they will need to tag to. | ||
TAGGABLE_BRANCHES = [ | ||
"/business-and-industry", | ||
"/business-tax", | ||
"/childcare-parenting", | ||
"/defence", | ||
"/defence-and-armed-forces", | ||
"/education", | ||
"/employment", | ||
"/environment", | ||
"/government", | ||
"/housing-and-local-government", | ||
"/money", | ||
"/regional-and-local-government", | ||
"/society-and-culture", | ||
"/transport", | ||
"/welfare", | ||
"/work", | ||
].freeze | ||
|
||
def initialize(taxonomy_topic_email_override = nil) | ||
@taxonomy_topic_email_override = taxonomy_topic_email_override | ||
end | ||
|
||
attr_reader :taxonomy_topic_email_override | ||
|
||
def grouped_options(selected_taxon_content_id = nil) | ||
branches_sorted_by_level_one_taxon_name.map do |level_one_taxon| | ||
[level_one_taxon.name, sorted_transformed_descendants(level_one_taxon, selected_taxon_content_id)] | ||
end | ||
end | ||
|
||
private | ||
|
||
def branches_sorted_by_level_one_taxon_name | ||
topic_taxonomy.visible_branches | ||
.select { |branch| TAGGABLE_BRANCHES.include?(branch.base_path) } | ||
.sort_by { |branch| branch.name.downcase } | ||
end | ||
|
||
def sorted_transformed_descendants(level_one_taxon, selected_taxon_content_id) | ||
transform_descendants(level_one_taxon, selected_taxon_content_id) | ||
.sort_by { |s| s[:text].downcase } | ||
end | ||
|
||
def transform_descendants(level_one_taxon, selected_taxon_content_id) | ||
sort_descendants(level_one_taxon).map do |child| | ||
transform_taxon(child, selected_taxon_content_id) | ||
end | ||
end | ||
|
||
def sort_descendants(level_one_taxon) | ||
sorted_descendants = level_one_taxon.descendants.sort_by { |descendant| descendant.name.downcase } | ||
[level_one_taxon, sorted_descendants].flatten | ||
end | ||
|
||
def taxon_with_ancestors(taxon) | ||
ancestors_names = taxon.ancestors.map(&:name) | ||
ancestor_string = ancestors_names.join(" > ") | ||
|
||
"#{ancestor_string} > #{taxon.name} " if ancestors_names.any? | ||
end | ||
|
||
def transform_taxon(taxon, selected_taxon_content_id = nil) | ||
formatted_taxon_name = | ||
taxon.ancestors.present? ? taxon_with_ancestors(taxon) : taxon.name.to_s | ||
{ | ||
text: formatted_taxon_name, | ||
value: taxon.content_id, | ||
selected: selected?(taxon.content_id, selected_taxon_content_id), | ||
} | ||
end | ||
|
||
def selected?(content_id, selected_taxon_content_id) | ||
previously_selected = selected_taxon_content_id || taxonomy_topic_email_override | ||
previously_selected == content_id | ||
end | ||
|
||
def topic_taxonomy | ||
@topic_taxonomy ||= Taxonomy::TopicTaxonomy.new | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
...views/admin/document_collection_email_subscriptions/_email_override_summary_page.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<p class="govuk-body-m"> | ||
You cannot change the email notifications for this document collection. | ||
If you have any questions about this, contact the managing editor at HMRC. | ||
</p> | ||
<p class="govuk-body-m"> Users who sign up to notifications on this document collection get an email alert when:</p> | ||
<% if has_page_level_notifications?(@collection) %> | ||
<%= render "govuk_publishing_components/components/list", { | ||
visible_counters: true, | ||
items: [ | ||
"there’s a major change to any content that’s listed on the document collection (the content items in the ‘Collection documents’ tab)", | ||
"there’s a major change to the document collection itself", | ||
"the page is unpublished", | ||
], | ||
} %> | ||
<% else %> | ||
<%= render "govuk_publishing_components/components/list", { | ||
visible_counters: true, | ||
items: [ | ||
"there’s a major change to any content tagged to the topic #{link_to taxonomy_topic_email_override_title(@collection), taxonomy_topic_email_override_base_path(@collection)}", | ||
"the page is unpublished", | ||
], | ||
} %> | ||
<% end %> |
36 changes: 36 additions & 0 deletions
36
app/views/admin/document_collection_email_subscriptions/_form.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<%= form_for [:admin, @collection], url: admin_document_collection_update_email_subscription_path(@collection), method: "put" do |form| %> | ||
<%= render "govuk_publishing_components/components/radio", { | ||
name: "override_email_subscriptions", | ||
id: "email_override_email_subscription_choice", | ||
items: [ | ||
{ | ||
value: false, | ||
text: "Emails about this page", | ||
hint_text: "Users will get an email when the document collection or any of the content listed on it is updated.", | ||
checked: has_page_level_notifications?(@collection), | ||
bold: true, | ||
}, | ||
{ | ||
value: true, | ||
text: "Emails about the topic", | ||
bold: true, | ||
hint_text: "Users will get an email when any content related to the topic is updated. You choose the topic from the topic taxonomy.", | ||
checked: emails_about_this_topic_checked?(@collection, params), | ||
conditional: render("taxonomy_choice"), | ||
}, | ||
], | ||
} %> | ||
|
||
<div class="govuk-button-group"> | ||
<%= render "govuk_publishing_components/components/button", { | ||
text: "Save", | ||
data_attributes: { | ||
module: "gem-track-click", | ||
"track-category": "form-button", | ||
"track-action": "update-email-subscriptions-button", | ||
"track-label": "Save", | ||
}, | ||
} %> | ||
<%= link_to("Cancel", admin_document_collection_path(@collection), class: "govuk-link govuk-link--no-visited-state") %> | ||
</div> | ||
<% end %> |
26 changes: 26 additions & 0 deletions
26
app/views/admin/document_collection_email_subscriptions/_taxonomy_choice.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<div> | ||
<h4 class="govuk-heading-s govuk-!-margin-bottom-2">Choose the topic</h4> | ||
<%= render "components/select-with-search", { | ||
label: "Choose a topic from the topic taxonomy. You can only choose one.", | ||
error_message: flash["selected_taxon_content_id"], | ||
error_id: flash["selected_taxon_content_id"], | ||
id: "selected_taxon_content_id", | ||
include_blank: true, | ||
grouped_options: @topic_list_select_presenter.grouped_options(params["selected_taxon_content_id"]), | ||
} %> | ||
<hr class="govuk-section-break--m"> | ||
<%= render "govuk_publishing_components/components/checkboxes", { | ||
id: "email_override_confirmation", | ||
name: "email_override_confirmation", | ||
heading_size: "s", | ||
heading: "You cannot change the email notification settings after the collection is published.", | ||
no_hint_text: true, | ||
error: flash["email_override_confirmation"], | ||
items: [ | ||
{ | ||
label: "Select this box to confirm you're happy with what you've selected.", | ||
value: true, | ||
}, | ||
], | ||
} %> | ||
</div> |
29 changes: 29 additions & 0 deletions
29
app/views/admin/document_collection_email_subscriptions/edit.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<% content_for :page_title, "Email notification settings" %> | ||
<% content_for :title_margin_bottom, 8 %> | ||
<% content_for :page_class, "document-collection-email-groups edit" %> | ||
<% errors = errors_from_flash(flash) %> | ||
|
||
<% if errors.present? %> | ||
<%= render "govuk_publishing_components/components/error_summary", { | ||
id: "error-summary", | ||
title: "There are some problems", | ||
items: errors, | ||
} %> | ||
<% end %> | ||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds"> | ||
<%= render "components/secondary_navigation", { | ||
aria_label: "Document navigation tabs", | ||
items: secondary_navigation_tabs_items(@collection, request.path), | ||
} %> | ||
|
||
<h2 class="govuk-heading-l">Email notifications</h2> | ||
|
||
<% if taxonomy_topic_cannot_be_set?(@collection) %> | ||
<%= render partial: "email_override_summary_page" %> | ||
<% else %> | ||
<p class="govuk-body">Choose the type of email updates users will get if they sign up for notifications.</p> | ||
<%= render partial: "form" %> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.