Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
FIX - add welsh for take_part content_item
  • Loading branch information
KludgeKML committed Sep 4, 2024
1 parent 5da0473 commit 50b3e7b
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 77 deletions.
12 changes: 7 additions & 5 deletions app/controllers/concerns/content_item_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ module ContentItemLoader
included do
before_action :load_content_item
before_action :set_expiry

helper_method :view_context
end

def load_content_item
content_item = GdsApi.content_store.content_item(request.path)
content_item["links"]["ordered_related_items"] = ordered_related_items(content_item["links"]) if content_item["links"]
response = GdsApi.content_store.content_item(request.path)
response["links"]["ordered_related_items"] = ordered_related_items(response["links"]) if response["links"]

@content_item = content_item
@content_item = model_type.new(response)
end

def ordered_related_items(links)
Expand All @@ -23,8 +25,8 @@ def ordered_related_items(links)

def set_expiry
expires_in(
@content_item.cache_control.max_age,
public: @content_item.cache_control.public?,
@content_item.content_item.cache_control.max_age,
public: @content_item.content_item.cache_control.public?,
)
end
end
4 changes: 4 additions & 0 deletions app/controllers/take_part_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class TakePartController < ApplicationController
include ContentItemLoader

def model_type
TakePart
end
end
14 changes: 14 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@ def wrapper_class(publication = nil)
def current_path_without_query_string
request.original_fullpath.split("?", 2).first
end

def t_locale_fallback(key, options = {})
options[:locale] = I18n.locale
options[:fallback] = nil
translation = I18n.t(key, **options)

if translation.nil? || translation.downcase.include?("translation missing")
I18n.default_locale
end
end

def page_text_direction
I18n.t("i18n.direction", locale: I18n.locale, default: "ltr")
end
end
52 changes: 0 additions & 52 deletions app/helpers/content_item_helper.rb

This file was deleted.

5 changes: 5 additions & 0 deletions app/helpers/govuk_chat_promo_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module GovukChatPromoHelper
def show_govuk_chat_promo?(_base_path)
false
end
end
6 changes: 3 additions & 3 deletions app/helpers/machine_readable_metadata_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module MachineReadableMetadataHelper
def machine_readable_metadata(args)
locals = { content_item: @content_item }.merge(args) # rubocop:disable Rails/HelperInstanceVariable
# render("govuk_publishing_components/components/machine_readable_metadata", locals)
def machine_readable_metadata(content_item, args)
locals = { content_item: content_item.content_item }.merge(args)
render("govuk_publishing_components/components/machine_readable_metadata", locals)
end
end
10 changes: 10 additions & 0 deletions app/helpers/title_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module TitleHelper
def title_and_context(title, document_type, view_context)
{
title:,
context: I18n.t("content_item.schema_name.#{document_type}", count: 1),
context_locale: view_context.t_locale_fallback("content_item.schema_name.#{document_type}", count: 1),
average_title_length: "long",
}
end
end
41 changes: 41 additions & 0 deletions app/models/content_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class ContentItem
attr_reader :content_item, :body, :image, :description, :document_type, :title, :base_path

def initialize(content_store_response)
@content_item = content_store_response
@body = content_item.dig("details", "body")
@image = content_item.dig("details", "image")
@description = content_item["description"]
@document_type = content_item["document_type"]
@title = content_item["title"]
@base_path = content_item["base_path"]
end

def available_translations
translations = content_item["links"]["available_translations"] || []

mapped_locales(sorted_locales(translations))
end

def sorted_locales(translations)
translations.sort_by { |t| t["locale"] == I18n.default_locale.to_s ? "" : t["locale"] }
end

private

def mapped_locales(translations)
translations.map do |translation|
{
locale: translation["locale"],
base_path: translation["base_path"],
text: native_language_name_for(translation["locale"]),
}.tap do |h|
h[:active] = true if h[:locale] == I18n.locale.to_s
end
end
end

def native_language_name_for(locale)
I18n.t("language_names.#{locale}", locale:)
end
end
2 changes: 2 additions & 0 deletions app/models/take_part.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class TakePart < ContentItem
end
2 changes: 1 addition & 1 deletion app/views/shared/_sidebar_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<%= render "govuk_publishing_components/components/chat_entry", { border_top: true } %>
<% end %>
<%= render 'govuk_publishing_components/components/contextual_sidebar', content_item: @content_item %>
<%= render 'govuk_publishing_components/components/contextual_sidebar', content_item: @content_item.content_item %>
</div>
4 changes: 2 additions & 2 deletions app/views/shared/_translations.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if available_translations(@content_item).length > 1 %>
<% if @content_item.available_translations.any? %>
<div class="govuk-grid-column-one-third">
<%= render 'govuk_publishing_components/components/translation_nav',
translations: available_translations(@content_item) %>
translations: @content_item.available_translations %>
</div>
<% end %>
18 changes: 8 additions & 10 deletions app/views/take_part/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<% content_for :extra_head_content do %>
<%= machine_readable_metadata(
schema: :article
) %>
<%= machine_readable_metadata(@content_item, schema: :article) %>
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render 'govuk_publishing_components/components/title', title_and_context(@content_item) %>
<%= render 'govuk_publishing_components/components/lead_paragraph', text: @content_item['description'] %>
<%= render 'govuk_publishing_components/components/title', title_and_context(@content_item.title, @content_item.document_type, view_context) %>
<%= render 'govuk_publishing_components/components/lead_paragraph', text: @content_item.description %>
</div>

<%= render 'shared/translations' %>
Expand All @@ -17,15 +15,15 @@
<div class="govuk-grid-column-two-thirds">
<div class="content-bottom-margin">
<%= render 'components/figure',
src: image(@content_item)["url"],
alt: image(@content_item)["alt_text"],
credit: image(@content_item)["credit"],
caption: image(@content_item)["caption"] if image(@content_item) %>
src: @content_item.image["url"],
alt: @content_item.image["alt_text"],
credit: @content_item.image["credit"],
caption: @content_item.image["caption"] if @content_item.image %>
<%= render "govuk_publishing_components/components/govspeak", {
direction: page_text_direction
} do %>
<%= raw(body(@content_item)) %>
<%= raw(@content_item.body) %>
<% end %>
</div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions config/locales/cy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ cy:
components:
figure:
image_credit: 'Delwedd gan: %{credit}'
content_item:
schema_name:
take_part:
few:
many:
one: Cymryd rhan
other: Cymryd rhan
two:
zero:
continue: Parhau
cookies:
always_on: Mae angen iddynt fod ymlaen bob amser.
Expand Down
9 changes: 5 additions & 4 deletions spec/system/take_part_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
RSpec.describe "TakePart" do
before do
stub_content_store_has_item("/government/get-involved/take-part/tp1", schema: "take_part", links: {})
content_store_has_example_item("/government/get-involved/take-part/tp1", schema: :take_part)
end

context "/government/get-involved/take-part/:slug" do
it "displays the take_part page" do
visit "/government/get-involved/take-part/tp1"

expect(page).to have_title("Take Part")
expect(page).to have_text("take part!")
expect(page).to have_text("There's a thing")
expect(page).to have_css("h1", text: "Become a councillor")
expect(page).to have_text("All councils are led by democratically elected councillors who set the vision and direction, and represent their local community.")

assert page.has_text?("There are roughly 20,000 local councillors in England. Councillors are elected to the local council to represent their own local community, so they must either live or work in the area.")
end

it "does not display a single page notification button" do
Expand Down

0 comments on commit 50b3e7b

Please sign in to comment.