Skip to content

Commit

Permalink
Merge pull request #8697 from alphagov/ed-ww-org-translations
Browse files Browse the repository at this point in the history
Enable translations for editionable worldwide organisations
  • Loading branch information
brucebolt authored Jan 4, 2024
2 parents 6e2b7d7 + ed77721 commit 2a727db
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ def destroy
end
end

def edit; end
def edit
I18n.with_locale(params[:locale] || I18n.default_locale) do
render :edit
end
end

def index; end
def index
@editionable_social_media_accounts_index_presenter = EditionableSocialMediaAccountsIndexPresenter.new(@edition)
end

def new; end

Expand All @@ -50,6 +56,7 @@ def find_social_media_account

def social_media_account_params
params.fetch(:social_media_account, {}).permit(
:locale,
:social_media_service_id,
:title,
:url,
Expand Down
4 changes: 4 additions & 0 deletions app/models/editionable_worldwide_organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ def secondary_role
def skip_world_location_validation?
false
end

def translatable?
true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class EditionableSocialMediaAccountsIndexPresenter
include Rails.application.routes.url_helpers

attr_accessor :edition

def initialize(edition)
@edition = edition
end

def social_media_accounts
edition.social_media_accounts.map do |social_media_account|
{
title: social_media_account.service_name,
rows: social_media_account_rows(social_media_account),
summary_card_actions: summary_card_actions(social_media_account),
}
end
end

private

def social_media_account_rows(social_media_account)
edition.translations.pluck(:locale).map do |locale|
{
key: "#{Locale.new(locale).english_language_name} Account",
value: I18n.with_locale(locale) { social_media_account.title },
actions: [
{
label: "Edit",
href: edit_admin_edition_social_media_account_path(@edition, social_media_account, locale:),
},
],
}
end
end

def summary_card_actions(social_media_account)
[
{
label: "Delete",
href: confirm_destroy_admin_edition_social_media_account_path(@edition, social_media_account),
destructive: true,
},
]
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
error_items: errors_for(social_media_account.errors, :title),
} %>

<%= form.hidden_field :locale, value: I18n.locale %>

<div class="govuk-button-group govuk-!-margin-top-8">
<%= render "govuk_publishing_components/components/button", {
text: "Save",
Expand Down
25 changes: 5 additions & 20 deletions app/views/admin/editionable_social_media_accounts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% content_for :page_title, "Social media accounts for #{@edition.title}" %>
<% content_for :title, "Social media accounts for for #{@edition.title}" %>
<% content_for :title, "Social media accounts for #{@edition.title}" %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
Expand All @@ -25,26 +25,11 @@
} %>

<% if @edition.social_media_accounts.any? %>
<% @edition.social_media_accounts.each do |social_media_account| %>
<% @editionable_social_media_accounts_index_presenter.social_media_accounts.each do |social_media_account| %>
<%= render "components/summary_card", {
title: social_media_account.service_name,
rows: [
{
key: "Account",
value: social_media_account.title,
actions: [
{
label: "Edit",
href: edit_admin_edition_social_media_account_path(@edition, social_media_account),
},
{
label: "Delete",
href: confirm_destroy_admin_edition_social_media_account_path(@edition, social_media_account),
destructive: true,
},
],
},
],
title: social_media_account[:title],
rows: social_media_account[:rows],
summary_card_actions: social_media_account[:summary_card_actions],
} %>
<% end %>
<% else %>
Expand Down
5 changes: 5 additions & 0 deletions features/editionable-worldwide-organisations.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Feature: Editionable worldwide organisations
And I should see it has been assigned to the "United Kingdom" world location
And I should see the editionable worldwide organisation "Test Worldwide Organisation" in the list of draft documents

Scenario Outline: Adding a translation to an existing worldwide organisation
When I draft a new worldwide organisation "Test Worldwide Organisation" assigned to world location "United Kingdom"
And I add a Welsh translation of the worldwide organisation "Test Worldwide Organisation" named "Translated Name"
Then I should see the Welsh translated title "Translated Name" for the "Test Worldwide Organisation" worldwide organisation

Scenario Outline: Assigning a role to a worldwide organisation
Given a role "Prime Minister" exists
When I draft a new worldwide organisation "Test Worldwide Organisation" assigned to world location "United Kingdom"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@
expect(@worldwide_organisation.world_locations.first.name).to eq(title)
end

And(/^I add a Welsh translation of the worldwide organisation "([^"]*)" named "([^"]*)"$/) do |title, translated_title|
visit_edition_admin(title)
click_link "Add translation"
select "Cymraeg (Welsh)", from: "Choose language"
click_button "Next"
fill_in "Translated title (required)", with: translated_title
click_button "Save"
end

Then(/^I should see the Welsh translated title "([^"]*)" for the "([^"]*)" worldwide organisation$/) do |translated_title, title|
@worldwide_organisation = EditionableWorldwideOrganisation.find_by(title:)

I18n.with_locale(:cy) do
expect(@worldwide_organisation.title).to eq(translated_title)
end
end

Given(/^a role "([^"]*)" exists$/) do |name|
create(:role, name:)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ class Admin::EditionableSocialMediaAccountsControllerTest < ActionController::Te

setup do
login_as :gds_editor
@edition = create(:editionable_worldwide_organisation, :with_social_media_account)
@edition = create(:editionable_worldwide_organisation, :with_social_media_account, translated_into: [:cy])

I18n.with_locale(:cy) do
@edition.social_media_accounts.first.update(
title: "Translated title",
url: "https://www.newurl.gov.cymru",
)
end
end

view_test "GET :index lists the existing social media accounts" do
get :index, params: { edition_id: @edition }

assert_response :success
assert_select "h2.govuk-summary-card__title", text: @edition.social_media_accounts.first.social_media_service.name
assert_select "dd.govuk-summary-list__value", text: @edition.social_media_accounts.first.title

assert_select "div.govuk-summary-list__row:nth-of-type(1) .govuk-summary-list__key", text: "English Account"
assert_select "div.govuk-summary-list__row:nth-of-type(1) .govuk-summary-list__value", text: @edition.social_media_accounts.first.title

assert_select "div.govuk-summary-list__row:nth-of-type(2) .govuk-summary-list__key", text: "Welsh Account"
assert_select "div.govuk-summary-list__row:nth-of-type(2) .govuk-summary-list__value", text: "Translated title"
end

view_test "GET :edit displays an existing social media account" do
Expand All @@ -28,6 +40,19 @@ class Admin::EditionableSocialMediaAccountsControllerTest < ActionController::Te
assert_select "input.govuk-input", value: @edition.social_media_accounts.first.title
end

view_test "GET :edit displays an existing social media account translation" do
get :edit, params: {
edition_id: @edition,
id: @edition.social_media_accounts.first,
locale: "cy",
}

assert_response :success
assert_select "select.govuk-select", value: @edition.social_media_accounts.first.social_media_service.name
assert_select "input.govuk-input", value: "https://www.newurl.gov.cymru"
assert_select "input.govuk-input", value: "Translated title"
end

test "PATCH :update updates an existing social media account" do
patch :update, params: {
edition_id: @edition,
Expand All @@ -43,6 +68,24 @@ class Admin::EditionableSocialMediaAccountsControllerTest < ActionController::Te
assert_equal "https://www.newurl.gov.uk", @edition.social_media_accounts.first.url
end

test "PATCH :update updates an existing social media account translation" do
patch :update, params: {
edition_id: @edition,
id: @edition.social_media_accounts.first,
social_media_account: {
title: "New translated title",
url: "https://www.newurl.gov.cymru",
locale: "cy",
},
}

assert_response :redirect
I18n.with_locale(:cy) do
assert_equal "New translated title", @edition.social_media_accounts.first.title
assert_equal "https://www.newurl.gov.cymru", @edition.social_media_accounts.first.url
end
end

test "POST :create creates a social media account" do
post :create, params: {
edition_id: @edition,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require "test_helper"

class EditionableSocialMediaAccountsIndexPresenterTest < ActiveSupport::TestCase
include Rails.application.routes.url_helpers

test "EditionableSocialMediaAccountsIndexPresenter.social_media_accounts should return an array of accounts for the edition" do
editionable_worldwide_organisation = create(:editionable_worldwide_organisation, translated_into: "cy")
social_media_account_1 = create(:social_media_account)
social_media_account_2 = create(:social_media_account)
editionable_worldwide_organisation.social_media_accounts << [social_media_account_1, social_media_account_2]

expected = [
{
title: social_media_account_1.service_name,
rows: [
{
key: "English Account",
value: social_media_account_1.title,
actions: [
{
label: "Edit",
href: edit_admin_edition_social_media_account_path(editionable_worldwide_organisation, social_media_account_1, locale: :en),
},
],
},
{
key: "Welsh Account",
value: social_media_account_1.title,
actions: [
{
label: "Edit",
href: edit_admin_edition_social_media_account_path(editionable_worldwide_organisation, social_media_account_1, locale: :cy),
},
],
},
],
summary_card_actions: [
{
label: "Delete",
href: confirm_destroy_admin_edition_social_media_account_path(editionable_worldwide_organisation, social_media_account_1),
destructive: true,
},
],
},
{
title: social_media_account_2.service_name,
rows: [
{
key: "English Account",
value: social_media_account_2.title,
actions: [
{
label: "Edit",
href: edit_admin_edition_social_media_account_path(editionable_worldwide_organisation, social_media_account_2, locale: :en),
},
],
},
{
key: "Welsh Account",
value: social_media_account_2.title,
actions: [
{
label: "Edit",
href: edit_admin_edition_social_media_account_path(editionable_worldwide_organisation, social_media_account_2, locale: :cy),
},
],
},
],
summary_card_actions: [
{
label: "Delete",
href: confirm_destroy_admin_edition_social_media_account_path(editionable_worldwide_organisation, social_media_account_2),
destructive: true,
},
],
},
]

assert_equal expected, EditionableSocialMediaAccountsIndexPresenter.new(editionable_worldwide_organisation).social_media_accounts
end
end

0 comments on commit 2a727db

Please sign in to comment.