Skip to content

Commit

Permalink
Topic select
Browse files Browse the repository at this point in the history
  • Loading branch information
hannako committed Aug 24, 2023
1 parent f43dd56 commit bce5500
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ class Admin::DocumentCollectionEmailSubscriptionsController < Admin::BaseControl
before_action :authorise_user
layout "design_system"

def edit; end
def edit
@topic_list_presenter = TopicListPresenter.new(@collection)
end

def update
if params[:override_email_subscriptions] == "true"
Expand Down
31 changes: 31 additions & 0 deletions app/presenters/topic_list_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class TopicListPresenter
def initialize(collection)
@collection = collection
end

attr_reader :collection

def options(selected_taxon_content_id = nil)
topic_taxonomy.all_taxons.sort.map do |taxon|
{
text: taxon.name,
value: taxon.content_id,
selected: selected?(taxon.content_id, selected_taxon_content_id),
}
end
end

private

def selected?(content_id, selected_taxon_content_id)
content_id == (existing_taxonomy_topic_override || selected_taxon_content_id)
end

def existing_taxonomy_topic_override
collection.taxonomy_topic_email_override
end

def topic_taxonomy
@topic_taxonomy ||= Taxonomy::TopicTaxonomy.new
end
end

Check failure on line 31 in app/presenters/topic_list_presenter.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/TrailingEmptyLines: Final newline missing. (https://rubystyle.guide#newline-eof)
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
<% selected = @collection.taxonomy_topic_email_override || params[:selected_taxon_content_id] %>
<div>
<h4 class="govuk-heading-s govuk-!-margin-bottom-0">Choose the topic</h4>
<%= render "govuk_publishing_components/components/select", {
id: "selected_taxon_content_id",
label: "Choose a topic or subtopic from the topic taxonomy. You can only choose one.",
options: [
{ text: "", value: "" },
{ text: "Topic One", value: "9b889c60-2191-11ee-be56-0242ac120002", selected: selected_field?(selected,"9b889c60-2191-11ee-be56-0242ac120002")},
{ text: "Topic Two", value: "c60-2191-11ee-be56-0242ac120002", selected: selected_field?(selected,"c60-2191-11ee-be56-0242ac120002")}
]
options: @topic_list_presenter.options(params[:selected_taxon_content_id])
} %>
<hr class="govuk-section-break--m">
<%= render "govuk_publishing_components/components/checkboxes", {
Expand Down
4 changes: 2 additions & 2 deletions features/document-collection-email-override.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Feature: Setting the taxonomy topic email override for a document collection
When I visit the edit document collection page
Then I click on the tab "Email notifications"
And I choose "Emails about this topic"
And I select "Topic One"
And I select "Education"
And I click the checkbox to confirm my selection.
And I click "Save"
Then I see the success message "You have set the email type"
Expand All @@ -17,7 +17,7 @@ Feature: Setting the taxonomy topic email override for a document collection
When I visit the edit document collection page
Then I click on the tab "Email notifications"
And I choose "Emails about this topic"
And I select "Topic One"
And I select "Education"
And I click "Save"
Then I see the error "You need to confirm" prompting me to confirm my selection.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
end

Then(/^I click on the tab "Email notifications/) do
stub_taxonomy_data
expect(page).to have_content("Email notifications")
click_on("Email notifications")
expect(page).to have_content("Choose the type of email updates users will get if they sign up for notifications.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
require "test_helper"

class Admin::DocumentCollectionEmailSubscriptionsControllerTest < ActionController::TestCase
include TaxonomyHelper
setup do
@collection = create(:draft_document_collection, :with_group)
@user_with_permission = create(:writer, permissions: [User::Permissions::EMAIL_OVERRIDE_EDITOR])
@user_without_permission = create(:writer)
@selected_taxon_content_id = "9b889c60-2191-11ee-be56-0242ac120002"
@selected_taxon_content_id = root_taxon_content_id
@put_params = {
document_collection_id: @collection.id,
override_email_subscriptions: "true",
selected_taxon_content_id: @selected_taxon_content_id,
email_override_confirmation: "true",
}
login_as @user_without_permission
stub_taxonomy_with_all_taxons
end

should_be_an_admin_controller
Expand Down
7 changes: 4 additions & 3 deletions test/integration/document_collection_email_override_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@ class DocumentCollectionEmailOverrideTest < ActionDispatch::IntegrationTest

before do
login_as(user_with_permission_to_override)
stub_taxonomy_with_all_taxons
end

it "updates the taxonomy topic email override" do
visit edit_admin_document_collection_path(document_collection)
click_link "Email notifications"

page.choose("Emails about the topic")
select "Topic One", from: "selected_taxon_content_id"
select root_taxon["title"], from: "selected_taxon_content_id"
page.check("Select this box to confirm you're happy with what you've selected.")
click_button("Save")
document_collection.reload
assert_equal document_collection.taxonomy_topic_email_override, "9b889c60-2191-11ee-be56-0242ac120002"
assert_equal document_collection.taxonomy_topic_email_override, root_taxon_content_id
end

it "does not update taxonomy topic email if confirmation button is unchecked" do
visit edit_admin_document_collection_path(document_collection)
click_link "Email notifications"

page.choose("Emails about the topic")
select "Topic One", from: "selected_taxon_content_id"
select root_taxon["title"], from: "selected_taxon_content_id"
click_button("Save")
document_collection.reload
assert_nil document_collection.taxonomy_topic_email_override
Expand Down

0 comments on commit bce5500

Please sign in to comment.