-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #495 from alphagov/namespace-presenter-mixins
Introduce content_item namespace for presenter mixins
- Loading branch information
Showing
65 changed files
with
708 additions
and
680 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class AnswerPresenter < ContentItemPresenter | ||
include Body | ||
include LastUpdated | ||
include ContentItem::Body | ||
include ContentItem::LastUpdated | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module ContentItem | ||
module Body | ||
def body | ||
content_item["details"]["body"] | ||
end | ||
|
||
def govspeak_body | ||
{ | ||
content: body, | ||
direction: text_direction | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module ContentItem | ||
module ContentsList | ||
include ActionView::Helpers::UrlHelper | ||
include TypographyHelper | ||
|
||
def contents | ||
@contents ||= contents_items.each do |item| | ||
item[:href] = "##{item[:id]}" | ||
end | ||
end | ||
|
||
def contents_items | ||
extract_headings_with_ids(body) | ||
end | ||
|
||
private | ||
|
||
def extract_headings_with_ids(html) | ||
headings = Nokogiri::HTML(html).css('h2').map do |heading| | ||
id = heading.attribute('id') | ||
{ text: strip_trailing_colons(heading.text), id: id.value } if id | ||
end | ||
headings.compact | ||
end | ||
end | ||
end |
80 changes: 80 additions & 0 deletions
80
app/presenters/content_item/corporate_information_groups.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module ContentItem | ||
module CorporateInformationGroups | ||
include Linkable | ||
|
||
def corporate_information? | ||
corporate_information_groups.any? | ||
end | ||
|
||
def corporate_information | ||
corporate_information_groups.map do |group| | ||
{ | ||
heading: content_tag(:h3, group["name"], id: group_title_id(group["name"])), | ||
links: normalised_group_links(group) | ||
} | ||
end | ||
end | ||
|
||
def corporate_information_heading_tag | ||
content_tag(:h2, corporate_information_heading[:text], id: corporate_information_heading[:id]) | ||
end | ||
|
||
def further_information | ||
[ | ||
further_information_about("publication_scheme"), | ||
further_information_about("welsh_language_scheme"), | ||
further_information_about("personal_information_charter"), | ||
further_information_about("social_media_use"), | ||
further_information_about("about_our_services") | ||
].join(' ').html_safe | ||
end | ||
|
||
private | ||
|
||
def further_information_link(type) | ||
link = corporate_information_page_links.find { |l| l["document_type"] == type } | ||
link_to(link["title"], link["base_path"]) if link | ||
end | ||
|
||
def further_information_about(type) | ||
link = further_information_link(type) | ||
I18n.t("corporate_information_page.#{type}_html", link: link) if link | ||
end | ||
|
||
def corporate_information_heading | ||
heading_text = I18n.t('corporate_information_page.corporate_information') | ||
|
||
{ | ||
text: heading_text, | ||
id: group_title_id(heading_text) | ||
} | ||
end | ||
|
||
def group_title_id(title) | ||
title.tr(' ', '-').downcase | ||
end | ||
|
||
def normalised_group_links(group) | ||
group["contents"].map do |group_item| | ||
normalised_group_item_link(group_item) | ||
end | ||
end | ||
|
||
def normalised_group_item_link(group_item) | ||
if group_item.is_a?(String) | ||
group_item_link = corporate_information_page_links.find { |l| l["content_id"] == group_item } | ||
link_to(group_item_link["title"], group_item_link["base_path"]) | ||
else | ||
link_to(group_item["title"], group_item["path"] || group_item["url"]) | ||
end | ||
end | ||
|
||
def corporate_information_page_links | ||
expanded_links_from_content_item('corporate_information_pages') | ||
end | ||
|
||
def corporate_information_groups | ||
content_item["details"]["corporate_information_groups"] || [] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module ContentItem | ||
module LastUpdated | ||
def last_updated | ||
display_date(content_item["public_updated_at"]) if content_item["public_updated_at"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module ContentItem | ||
module Linkable | ||
include ActionView::Helpers::UrlHelper | ||
|
||
def from | ||
organisations_ordered_by_importance + links_group(%w{worldwide_organisations ministers speaker}) | ||
end | ||
|
||
def part_of | ||
links_group(%w{ | ||
document_collections | ||
related_policies | ||
policies | ||
world_locations | ||
topics | ||
topical_events | ||
related_statistical_data_sets | ||
}) | ||
end | ||
|
||
private | ||
|
||
def links(type) | ||
expanded_links_from_content_item(type).map do |link| | ||
link_for_type(type, link) | ||
end | ||
end | ||
|
||
def expanded_links_from_content_item(type) | ||
return [] unless content_item["links"][type] | ||
content_item["links"][type] | ||
end | ||
|
||
def links_group(types) | ||
types.flat_map { |type| links(type) }.uniq | ||
end | ||
|
||
def organisations_ordered_by_importance | ||
organisations_with_emphasised_first.map do |link| | ||
link_to(link["title"], link["base_path"]) | ||
end | ||
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 emphasised_organisations | ||
content_item["details"]["emphasised_organisations"] || [] | ||
end | ||
|
||
def link_for_type(type, link) | ||
return link_for_world_location(link) if type == "world_locations" | ||
link_to(link["title"], link["base_path"]) | ||
end | ||
|
||
def link_for_world_location(link) | ||
base_path = WorldLocationBasePath.for(link) | ||
link_to(link["title"], base_path) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module ContentItem | ||
module Metadata | ||
include Linkable | ||
include Updatable | ||
|
||
def metadata | ||
{ | ||
from: from, | ||
first_published: published, | ||
last_updated: updated, | ||
see_updates_link: true, | ||
part_of: part_of, | ||
direction: text_direction, | ||
other: {} | ||
} | ||
end | ||
|
||
def metadata_for_new_navigation | ||
data = metadata | ||
data.delete(:part_of) | ||
data | ||
end | ||
|
||
def document_footer | ||
{ | ||
from: from, | ||
published: published, | ||
updated: updated, | ||
history: history, | ||
part_of: part_of, | ||
direction: text_direction, | ||
other: {} | ||
} | ||
end | ||
|
||
def document_footer_for_new_navigation | ||
data = document_footer | ||
data.delete(:part_of) | ||
data | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module ContentItem | ||
module NationalApplicability | ||
include Metadata | ||
|
||
def applies_to | ||
return nil if !national_applicability | ||
all_nations = national_applicability.values | ||
applicable_nations = all_nations.select { |n| n["applicable"] } | ||
inapplicable_nations = all_nations - applicable_nations | ||
|
||
applies_to = applicable_nations.map { |n| n["label"] }.to_sentence | ||
|
||
if inapplicable_nations.any? | ||
nations_with_alt_urls = inapplicable_nations.select { |n| n["alternative_url"].present? } | ||
if nations_with_alt_urls.any? | ||
alternate_links = nations_with_alt_urls | ||
.map { |n| link_to(n['label'], n['alternative_url'], rel: :external) } | ||
.to_sentence | ||
|
||
applies_to += " (see #{translated_schema_name(nations_with_alt_urls.count)} for #{alternate_links})" | ||
end | ||
end | ||
|
||
applies_to | ||
end | ||
|
||
def metadata | ||
super.tap do |m| | ||
m[:other]['Applies to'] = applies_to | ||
end | ||
end | ||
|
||
private | ||
|
||
def translated_schema_name(count) | ||
I18n.t("content_item.schema_name.#{schema_name}", count: count).downcase | ||
end | ||
|
||
def national_applicability | ||
content_item["details"]["national_applicability"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module ContentItem | ||
module OrganisationBranding | ||
def organisation_logo(organisation = default_organisation) | ||
return nil unless organisation && organisation.dig("details", "logo") | ||
|
||
logo = organisation["details"]["logo"] | ||
logo_component_params = { | ||
organisation: { | ||
name: logo["formatted_title"], | ||
url: organisation["base_path"], | ||
brand: organisation_brand(organisation), | ||
crest: logo["crest"], | ||
} | ||
} | ||
|
||
if logo["image"] | ||
logo_component_params[:organisation][:image] = { | ||
url: logo["image"]["url"], | ||
alt_text: logo["image"]["alt_text"], | ||
} | ||
end | ||
|
||
logo_component_params | ||
end | ||
|
||
def organisation_brand_class(organisation = default_organisation) | ||
"#{organisation_brand(organisation)}-brand-colour" | ||
end | ||
|
||
private | ||
|
||
def default_organisation | ||
orgs = content_item["links"]["organisations"] || [] | ||
orgs.first | ||
end | ||
|
||
# HACK: Replaces the organisation_brand for executive office organisations. | ||
# Remove this in the future after migrating organisations to the content store API, | ||
# and updating them with the correct brand in the actual store. | ||
def organisation_brand(organisation) | ||
brand = organisation["details"]["brand"] | ||
brand = "executive-office" if executive_order_crest?(organisation) | ||
brand | ||
end | ||
|
||
def executive_order_crest?(organisation) | ||
organisation["details"]["logo"]["crest"] == "eo" | ||
end | ||
end | ||
end |
Oops, something went wrong.