Skip to content

Commit

Permalink
Merge pull request #495 from alphagov/namespace-presenter-mixins
Browse files Browse the repository at this point in the history
Introduce content_item namespace for presenter mixins
  • Loading branch information
h-lame authored Sep 28, 2017
2 parents 84ac724 + 6a538a8 commit 363df2a
Show file tree
Hide file tree
Showing 65 changed files with 708 additions and 680 deletions.
4 changes: 2 additions & 2 deletions app/presenters/answer_presenter.rb
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
12 changes: 0 additions & 12 deletions app/presenters/body.rb

This file was deleted.

6 changes: 3 additions & 3 deletions app/presenters/case_study_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CaseStudyPresenter < ContentItemPresenter
include Body
include Metadata
include TitleAndContext
include ContentItem::Body
include ContentItem::Metadata
include ContentItem::TitleAndContext

def image
content_item["details"]["image"]
Expand Down
12 changes: 6 additions & 6 deletions app/presenters/consultation_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class ConsultationPresenter < ContentItemPresenter
include Body
include Metadata
include NationalApplicability
include Political
include Shareable
include TitleAndContext
include ContentItem::Body
include ContentItem::Metadata
include ContentItem::NationalApplicability
include ContentItem::Political
include ContentItem::Shareable
include ContentItem::TitleAndContext

def opening_date_time
content_item["details"]["opening_date"]
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/contact_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ContactPresenter < ContentItemPresenter
include TitleAndContext
include ContentItem::TitleAndContext

def title_and_context
super.tap do |t|
Expand Down
14 changes: 14 additions & 0 deletions app/presenters/content_item/body.rb
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
26 changes: 26 additions & 0 deletions app/presenters/content_item/contents_list.rb
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 app/presenters/content_item/corporate_information_groups.rb
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
7 changes: 7 additions & 0 deletions app/presenters/content_item/last_updated.rb
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
65 changes: 65 additions & 0 deletions app/presenters/content_item/linkable.rb
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
42 changes: 42 additions & 0 deletions app/presenters/content_item/metadata.rb
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
43 changes: 43 additions & 0 deletions app/presenters/content_item/national_applicability.rb
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
50 changes: 50 additions & 0 deletions app/presenters/content_item/organisation_branding.rb
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
Loading

0 comments on commit 363df2a

Please sign in to comment.