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

WIP merge hardcoded bits content into content items #3775

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/assets/images/rory.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions app/controllers/organisations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class OrganisationsController < ApplicationController
set_expiry content_item.max_age, public_cache: content_item.public_cache
end
around_action :switch_locale, only: %i[show court]
after_action :override_theme

def index
@organisations = ContentStoreOrganisations.find!("/government/organisations")
Expand Down Expand Up @@ -52,4 +53,17 @@ def presented_organisations
def locale
".#{params[:locale]}" if params[:locale]
end

def override_theme
theme_config = @content_item.dig("details", "theme_config")
if theme_config.present?
layout = theme_config["layout"]
case layout
when "full_width"
response.headers[Slimmer::Headers::TEMPLATE_HEADER] = "gem_layout_full_width"
else
raise "Unrecognised layout #{layout.inspect}"
end
end
end
end
24 changes: 24 additions & 0 deletions app/models/content_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ def self.find!(base_path)
"max_age" => content_item.cache_control["max-age"],
"public" => !content_item.cache_control.private?,
}
# See if we're extending this content item
content_id = content_item["content_id"]
content_extensions = []
if content_id.present?
Dir.glob(Rails.root.join("data", "content_extensions", content_id, "*.yaml")) do |filename|
content_extensions << YAML.load_file(filename)
end
Dir.glob(Rails.root.join("data", "theme_overrides", content_id, "theme.yaml")) do |filename|
theme_config = YAML.load_file(filename)
details = content_item_hash["details"]
details["theme_config"] = theme_config if details.present?
end
end
if content_extensions.any?
links = content_item_hash.fetch("links", {})
links["content_extensions"] = content_extensions
content_item_hash["links"] = links
end
new(content_item_hash)
end

Expand All @@ -21,6 +39,12 @@ def initialize(content_item_data)
end
end

%i[content_extensions].each do |field|
define_method field do
(@content_item_data["links"] || {})[field.to_s]
end
end

def details
@content_item_data["details"] || {}
end
Expand Down
1 change: 1 addition & 0 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
class Organisation
include ActiveModel::Model

attr_reader :content_item

Check failure on line 6 in app/models/organisation.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Layout/EmptyLinesAroundAttributeAccessor: Add an empty line after attribute accessor. (https://rubystyle.guide#empty-lines-around-attribute-accessor)
delegate :content_extensions, to: :content_item

HMCTS_CONTENT_ID = "6f757605-ab8f-4b62-84e4-99f79cf085c2".freeze

Expand Down
5 changes: 5 additions & 0 deletions app/views/organisations/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<% add_view_stylesheet("organisation") %>

<%= render partial: 'meta', locals: { organisation: @organisation } %>

<% @organisation.content_extensions&.each do |content_extension| %>
<%= render partial: "shared/content_extension_#{content_extension["document_type"]}", locals: { content_extension: } %>
<% end %>

<%= render partial: 'breadcrumb' %>
<%= render partial: 'header' %>
<% if @organisation.custom_banner %>
Expand Down
12 changes: 12 additions & 0 deletions app/views/shared/_content_extension_hero_image.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<style>
.full-width {
width: 100vw;
position: relative;
left: 50%;
right: 50%;
margin-left: -50vw;
margin-right: -50vw;
}
</style>
<h2 class="govuk-heading-m"><%= content_extension.dig("details", "text") %></h2>
<%= image_tag content_extension.dig("details", "image"), class: 'full-width' %>
3 changes: 2 additions & 1 deletion config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
GovukContentSecurityPolicy.configure
# TODO - reenable this once we've removed the placeholder images

Check failure on line 1 in config/initializers/content_security_policy.rb

View workflow job for this annotation

GitHub Actions / Lint Ruby / Run RuboCop

Style/CommentAnnotation: Annotation keywords like `TODO` should be all upper case, followed by a colon, and a space, then a note describing the problem. (https://rubystyle.guide#annotate-keywords)
# GovukContentSecurityPolicy.configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This is just an example of the sort of thing we might put in this directory
# Once the designs have crystallized a little bit, we should be able to hardcode
# some more compelling bits of content here.
document_type: 'hero_image'
schema_name: 'hero_image'
locale: 'en'
title: "Prime Minister's Office, 10 Downing Street"
details:
image: 'rory.jpg'
image_alt: '' # Intentionally left blank, image is decorative
text: "So Government! Much Improvement! Wow!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Content extensions for [/government/organisations/prime-ministers-office-10-downing-street](https://www.gov.uk/government/organisations/prime-ministers-office-10-downing-street)

The YAML files in this directory will be merged into the content item retrieved from content store before the
page is rendered.

The content of each YAML file in alphabetical order will appear in `.links.content_extensions[]` in the content item.

Because this is happening in the frontend application, there's no need to adhere to any particular schema for these
files, but you must make sure that the structure of each file matches the structure the ruby code expects it to have.
27 changes: 27 additions & 0 deletions data/content_extensions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Content Extensions

These are hardcoded extensions to the content items returned from content-store.

Hardcoding these allows us to experiment with the structure of the page and the components on the page, without having
to continuously tweak the publishing system.

Eventually, these extensions should either be provided by content-store itself, or removed.

## Similar past approaches

For previous pages with significant hardcoded content, we've used locale files. For example, see

```
git log -- config/locales/en/coronavirus_landing_page.yml
```

... for a history of the changes we made to the coronavirus landing page.

This approach works fine, but it doesn't align as closely with the way the publishing system works.

## Structure

Each subdirectory name is the content_id of some page rendered by collections. Inside each directory
is a list of numbered YAML files. Each YAML file will be added to the "links.content_extensions" field
of the content item by collections when it loads the item. The files will appear in the same order as
they do in the directory (so they can be reordered by changing their numbers).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layout: "full_width"
Loading