Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move case studies routes #4372

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Transaction start pages:

* https://www.gov.uk/help/browsers

### Case studies

* https://www.gov.uk/government/case-studies/get-britain-building-carlisle-park
* https://www.gov.uk/government/case-studies/doing-business-in-spain.es
* https://www.gov.uk/government/case-studies/terence-age-33-stoke-on-trent

### Hard-coded routes

* https://www.gov.uk/ (homepage)
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/case_studies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CaseStudiesController < ContentItemsController
def show
@case_study = CaseStudiesPresenter.new(@content_item, view_context)
end

private

def content_item_slug
request.path
end
end
20 changes: 10 additions & 10 deletions app/helpers/locale_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ def page_text_direction
I18n.t("i18n.direction", locale: I18n.locale, default: "ltr")
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 native_language_name_for(locale)
I18n.t("language_names.#{locale}", locale:)
end
Expand All @@ -22,14 +32,4 @@ def translations_for_nav(translations)
end
end
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
end
5 changes: 5 additions & 0 deletions app/models/case_study.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CaseStudy < ContentItem
include Metadata
include Linkable
include Updatable
end
46 changes: 46 additions & 0 deletions app/models/concerns/linkable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Linkable
extend ActiveSupport::Concern

def from
organisations_ordered_by_importance + links_group(%w[worldwide_organisations people speaker])
end

private

def organisations_ordered_by_importance
organisations_with_emphasised_first.map do |link|
ActionController::Base.helpers.link_to(link["title"], link["base_path"], class: "govuk-link")
end
end

def links(type)
expanded_links_from_content_item(type)
.select { |link| link["base_path"] || type == "world_locations" }
.map { |link| link_for_type(type, link) }
end

def organisations_with_emphasised_first
expanded_links_from_content_item("organisations").sort_by do |organisation|
is_emphasised = organisation["content_id"].in?(emphasised_organisations)
is_emphasised ? -1 : 1
end
end

def expanded_links_from_content_item(type)
return [] unless content_store_hash.dig("links", type)

content_store_hash.dig("links", type)
end

def emphasised_organisations
content_store_hash.dig("details", "emphasised_organisations") || []
end

def links_group(types)
types.flat_map { |type| links(type) }.uniq
end

def link_for_type(_type, link)
ActionController::Base.helpers.link_to(link["title"], link["base_path"], class: "govuk-link")
end
end
25 changes: 25 additions & 0 deletions app/models/concerns/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Metadata
extend ActiveSupport::Concern

def details_display_date
content_store_hash.dig("details", "display_date")
end

def pending_stats_announcement?
details_display_date.present? && Time.zone.parse(details_display_date).future?
end

def publisher_metadata
metadata = {
from:,
first_published: first_published_at,
last_updated: updated,
}

unless pending_stats_announcement?
metadata[:see_updates_link] = true
end

metadata
end
end
40 changes: 40 additions & 0 deletions app/models/concerns/updatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Updatable
extend ActiveSupport::Concern

def updated
public_updated_at if any_updates?
end

def history
return [] unless any_updates?

reverse_chronological_change_history
end

private

def any_updates?
if public_updated_at && first_public_at
Time.zone.parse(public_updated_at) != Time.zone.parse(first_public_at)
else
false
end
end

# The direction of change history isn't guaranteed
# https://github.com/alphagov/govuk-content-schemas/issues/545
def reverse_chronological_change_history
change_history.sort_by { |item| Time.zone.parse(item[:timestamp]) }.reverse
end

def change_history
changes = content_store_hash.dig("details", "change_history") || []
changes.map do |item|
{
display_time: item["public_timestamp"],
note: item["note"],
timestamp: item["public_timestamp"],
}
end
end
end
45 changes: 45 additions & 0 deletions app/models/concerns/withdrawable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Withdrawable
def page_title
withdrawn? ? "[Withdrawn] #{title}" : title
end

def withdrawn?
withdrawal_notice.present?
end

def withdrawal_notice_component
if withdrawn?
{
title: withdrawal_notice_title,
description_govspeak: withdrawal_notice["explanation"]&.html_safe,
time: withdrawal_notice_time,
lang: I18n.locale.to_s == "en" ? false : "en",
}
end
end

private

def withdrawal_notice
content_store_hash["withdrawn_notice"]
end

def withdrawal_notice_title
"This #{withdrawal_notice_context.downcase} was withdrawn on #{withdrawal_notice_time}".html_safe
end

def withdrawal_notice_context
I18n.t("formats.#{schema_name}", count: 1, locale: :en)
end

def withdrawal_notice_time
ActionController::Base.helpers.tag.time(
english_display_date(withdrawal_notice["withdrawn_at"]),
datetime: withdrawal_notice["withdrawn_at"],
)
end

def english_display_date(timestamp, format = "%-d %B %Y")
I18n.l(Time.zone.parse(timestamp), format:, locale: :en) if timestamp
end
end
6 changes: 5 additions & 1 deletion app/models/content_item.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require "ostruct"

class ContentItem
include Withdrawable

attr_reader :attachments, :content_store_response, :content_store_hash, :body, :image,
:description, :document_type, :schema_name, :title, :base_path, :locale,
:public_updated_at
:public_updated_at, :first_published_at, :first_public_at

# SCAFFOLDING: remove the override_content_store_hash parameter when full landing page
# content items including block details are available from content-store
Expand All @@ -20,6 +22,8 @@ def initialize(content_store_response, override_content_store_hash: nil)
@base_path = content_store_hash["base_path"]
@locale = content_store_hash["locale"]
@public_updated_at = content_store_hash["public_updated_at"]
@first_published_at = content_store_hash["first_published_at"]
@first_public_at = content_store_hash.dig("details", "first_public_at") || first_published_at

@attachments = get_attachments(content_store_hash.dig("details", "attachments"))

Expand Down
18 changes: 18 additions & 0 deletions app/presenters/case_studies_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CaseStudiesPresenter < ContentItemPresenter
attr_reader :content_item, :view_context

def initialize(content_item, view_context)
super(content_item)
@content_item = content_item
@view_context = view_context
end

def title_and_context
{
title: content_item.title,
context: I18n.t("formats.#{content_item.document_type}", count: 1),
context_locale: view_context.t_locale_fallback("formats.#{content_item.document_type}", count: 1),
average_title_length: "long",
}
end
end
52 changes: 52 additions & 0 deletions app/views/case_studies/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<% content_for :title, "#{@content_item.page_title} - #{I18n.t("formats.#{@content_item.document_type}", count: 1)} - GOV.UK" %>

<% content_for :extra_headers do %>
<%= render "govuk_publishing_components/components/machine_readable_metadata", { content_item: @content_item.to_h, schema: :article } %>
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds responsive-top-margin">
<%= render 'govuk_publishing_components/components/title', @case_study.title_and_context %>
</div>
<%= render 'shared/translations' %>
<div class="govuk-grid-column-two-thirds">
<%= render 'govuk_publishing_components/components/lead_paragraph', text: @content_item.description %>
</div>
</div>

<hr class="govuk-section-break govuk-section-break--s govuk-section-break--visible" aria-hidden="true">

<%= render 'shared/publisher_metadata_with_logo' %>
<% if @content_item.withdrawn? %>
<%= render 'govuk_publishing_components/components/notice', @content_item.withdrawal_notice_component %>
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<div class="content-bottom-margin">
<div class="responsive-bottom-margin">
<%= render 'components/figure',
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(@content_item.body) %>
<% end %>
</div>

<%= render 'components/published_dates', {
published: display_date(@content_item.first_published_at),
last_updated: display_date(@content_item.updated),
history: @content_item.history
} %>
</div>
</div>

<%= render 'shared/sidebar_navigation' %>
</div>

<%= render 'shared/footer_navigation' %>
19 changes: 19 additions & 0 deletions app/views/shared/_publisher_metadata_with_logo.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%
metadata_component_options = @content_item.publisher_metadata
metadata_component_options[:first_published] = display_date(metadata_component_options[:first_published])
%>

<div class="govuk-grid-row">
<div class="metadata-logo-wrapper<%= ' responsive-bottom-margin' if @content_item.try(:logo) %>">
<div class="govuk-grid-column-two-thirds metadata-column">
<%= render 'govuk_publishing_components/components/metadata', metadata_component_options %>
</div>
<div class="govuk-grid-column-one-third">
<% if @content_item.try(:logo) %>
<%= image_tag @content_item.logo[:path],
alt: @content_item.logo[:alt_text],
class: "metadata-logo" %>
<% end %>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions config/govuk_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# https://content-data.publishing.service.gov.uk/
---
calendar: /bank-holidays
case-studies: /government/case-studies/doing-business-in-spain
licence_transaction: /find-licences/tv-licence
local_transaction: /contact-electoral-registration-office
help_page: /help/browsers
Expand Down
7 changes: 7 additions & 0 deletions config/locales/ar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,13 @@ ar:
start_again:
your_answers:
start_now:
case_study:
few:
many:
one: دراسة الحالة
other: دراسات الحالة
two:
zero:
take_part:
few:
many:
Expand Down
3 changes: 3 additions & 0 deletions config/locales/az.yml
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ az:
start_again:
your_answers:
start_now:
case_study:
one: Nümunə üzrə araşdırma
other: Nümunə üzrə araşdırmalar
take_part:
one: İştirak edin
other: İştirak edin
Expand Down
5 changes: 5 additions & 0 deletions config/locales/be.yml
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ be:
start_again:
your_answers:
start_now:
case_study:
few:
many:
one: Тэматычнае даследаванне
other: Тэматычныя даследаванні
take_part:
few:
many:
Expand Down
3 changes: 3 additions & 0 deletions config/locales/bg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ bg:
start_again:
your_answers:
start_now:
case_study:
one: Казус
other: Казуси
take_part:
one: Вземете участие
other: Вземете участие
Expand Down
Loading