Skip to content

Commit

Permalink
Merge pull request #2271 from alphagov/email-flash-message
Browse files Browse the repository at this point in the history
Add email subscribe/unsubscribe flash messages
  • Loading branch information
barrucadu authored Nov 12, 2021
2 parents f012f67 + 0c88d6f commit 9b50162
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "dalli"
gem "gds-api-adapters"
gem "govuk_ab_testing"
gem "govuk_app_config"
gem "govuk_personalisation"
gem "govuk_publishing_components"
gem "htmlentities"
gem "plek"
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ DEPENDENCIES
gds-api-adapters
govuk_ab_testing
govuk_app_config
govuk_personalisation
govuk_publishing_components
govuk_schemas
govuk_test
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ContentItemsController < ApplicationController
include GovukPersonalisation::ControllerConcern

rescue_from GdsApi::HTTPForbidden, with: :error_403
rescue_from GdsApi::HTTPNotFound, with: :error_notfound
rescue_from GdsApi::HTTPGone, with: :error_410
Expand Down Expand Up @@ -199,4 +201,10 @@ def error_redirect(exception)
)
redirect_to destination, status: status_code
end

def set_account_vary_header
# Override the default from GovukPersonalisation::ControllerConcern so pages are cached on each flash message
# variation, rather than caching pages per user
response.headers["Vary"] = [response.headers["Vary"], "GOVUK-Account-Session-Flash"].compact.join(", ")
end
end
2 changes: 2 additions & 0 deletions app/views/content_items/detailed_guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
) %>
<% end %>

<%= render 'shared/email_subscribe_unsubscribe_flash' %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<% brexit_child_taxon = @content_item.brexit_child_taxon %>
Expand Down
2 changes: 2 additions & 0 deletions app/views/content_items/publication.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
) %>
<% end %>

<%= render 'shared/email_subscribe_unsubscribe_flash' %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render 'context_and_title' %>
Expand Down
20 changes: 20 additions & 0 deletions app/views/shared/_email_subscribe_unsubscribe_flash.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<% if @account_flash.include?("email-subscription-success") %>
<div class="govuk-grid-row govuk-!-margin-top-7">
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/success_alert", {
message: sanitize(t("email.subscribe_title")),
description: sanitize(t("email.description_html"))
} %>
</div>
</div>

<% elsif @account_flash.include?("email-unsubscribe-success") %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds govuk-!-margin-top-7">
<%= render "govuk_publishing_components/components/success_alert", {
message: sanitize(t("email.unsubscribe_title")),
description: sanitize(t("email.description_html"))
} %>
</div>
</div>
<% end %>
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ en:
publication_scheme_html: Read about the types of information we routinely publish in our %{link}.
social_media_use_html: Read our policy on %{link}.
welsh_language_scheme_html: Find out about our commitment to %{link}.
email:
subscribe_title: You’ve subscribed to emails about this page
unsubscribe_title: You’ve unsubscribed from emails about this page
description_html: <p class="govuk-body">Go to your GOV.UK account to <a class="govuk-link govuk-notification-banner__link" href="/email/manage">see and manage all your GOV.UK email subscriptions</a>.</p>
fatality_notice:
alt_text: Ministry of Defence crest
field_of_operation: Field of operation
Expand Down
23 changes: 23 additions & 0 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,29 @@ class ContentItemsControllerTest < ActionController::TestCase
assert_equal response.headers["Access-Control-Allow-Origin"], "*"
end

test "sets GOVUK-Account-Session-Flash in the Vary header" do
content_item = content_store_has_schema_example("case_study", "case_study")
get :show, params: { path: path_for(content_item) }

assert response.headers["Vary"].include?("GOVUK-Account-Session-Flash")
end

test "displays the subscription success banner when the 'email-subscription-success' flash is present" do
content_item = content_store_has_schema_example("publication", "publication")

request.headers["GOVUK-Account-Session"] = GovukPersonalisation::Flash.encode_session("session-id", %w[email-subscription-success])
get :show, params: { path: path_for(content_item) }
assert response.body.include?("subscribed to emails about this page")
end

test "displays the unsubscribe success banner when the 'email-unsubscribe-success' flash is present" do
content_item = content_store_has_schema_example("publication", "publication")

request.headers["GOVUK-Account-Session"] = GovukPersonalisation::Flash.encode_session("session-id", %w[email-unsubscribe-success])
get :show, params: { path: path_for(content_item) }
assert response.body.include?("unsubscribed from emails about this page")
end

def path_for(content_item, locale = nil)
base_path = content_item["base_path"].sub(/^\//, "")
base_path.gsub!(/\.#{locale}$/, "") if locale
Expand Down

0 comments on commit 9b50162

Please sign in to comment.