Skip to content

Commit

Permalink
Merge pull request #1032 from alphagov/add-content-item-links-rules-s…
Browse files Browse the repository at this point in the history
…ubgroups

Add content item links rules subgroups
  • Loading branch information
andrewgarner authored Aug 14, 2018
2 parents 3e33ec2 + b549356 commit 44f6c50
Show file tree
Hide file tree
Showing 26 changed files with 728 additions and 175 deletions.
22 changes: 7 additions & 15 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,15 @@ def load_content_item

def load_taxonomy_navigation
if @content_item.taxons.present?
taxons = @content_item.taxons.select { |taxon| taxon["phase"] == "live" }
current_base_path = @content_item.base_path

taxons = @content_item.taxons.select { |taxon| taxon["phase"] == "live" }
taxon_ids = taxons.map { |taxon| taxon["content_id"] }
services = Supergroups::Services.new(current_base_path, taxon_ids)
guidance_and_regulation = Supergroups::GuidanceAndRegulation.new(current_base_path, taxon_ids)
news = Supergroups::NewsAndCommunications.new(current_base_path, taxon_ids)
policy_and_engagement = Supergroups::PolicyAndEngagement.new(current_base_path, taxon_ids)
transparency = Supergroups::Transparency.new(current_base_path, taxon_ids)

@taxonomy_navigation = {
services: (services.all_services if services.any_services?),
guidance_and_regulation: guidance_and_regulation.tagged_content,
news_and_communication: (news.all_news if news.any_news?),
policy_and_engagement: policy_and_engagement.tagged_content,
transparency: transparency.tagged_content,
}

@taxonomy_navigation = {}
@content_item.links_out_supergroups.each do |supergroup|
supergroup_taxon_links = "Supergroups::#{supergroup.camelcase}".constantize.new(current_base_path, taxon_ids, filter_content_purpose_subgroup: @content_item.links_out_subgroups)
@taxonomy_navigation[supergroup.to_sym] = supergroup_taxon_links.tagged_content
end

@tagged_taxons = taxons.map do |taxon|
{
Expand Down
109 changes: 109 additions & 0 deletions app/presenters/content_item/links_out.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
module ContentItem
module LinksOut
def links_out_supergroups
@links_out_supergroups ||= fetch_links_out_supergroups
end

def links_out_subgroups
@links_out_subgroups ||= fetch_links_out_subgroups
end

private

def fetch_links_out_supergroups
links_out.map { |link| link["supergroup"] }.uniq
end

def fetch_links_out_subgroups
subgroups = []
links_out.each do |link|
if link["type"] == "content_purpose_subgroup"
subgroups << link["title"]
end
end
subgroups.uniq
end

def links_out
@links_out ||= fetch_links_out
end

def fetch_links_out
document_type_link_rule = DocumentTypeLinkRule.new(content_item)
return document_type_link_rule.links if document_type_link_rule.any?
subgroup_link_rule = SubgroupLinkRule.new(content_item)
return subgroup_link_rule.links if subgroup_link_rule.any?
SupergroupLinkRule.new(content_item).links
end

class LinkRule
def initialize(content_item)
@content_item = content_item
@rules = load_rules
@links = fetch_links
end

def any?
!@links.nil?
end

def links
@links || []
end

private

def fetch_links
if @content_item.dig(rule_level) &&
@rules[rule_level] &&
@rules[rule_level].dig(@content_item[rule_level])
@rules[rule_level][@content_item[rule_level]]
end
end

def load_rules
Rails.configuration.taxonomy_navigation_links_out
end

def rule_level
NotImplementedError
end
end

class DocumentTypeLinkRule < LinkRule
def initialize(content_item)
super
end

private

def rule_level
'document_type'
end
end

class SubgroupLinkRule < LinkRule
def initialize(content_item)
super
end

private

def rule_level
'content_purpose_subgroup'
end
end

class SupergroupLinkRule < LinkRule
def initialize(content_item)
super
end

private

def rule_level
'content_purpose_supergroup'
end
end
end
end
1 change: 1 addition & 0 deletions app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ContentItemPresenter
include ContentItem::Withdrawable
include ContentItem::LinksOut

attr_reader :content_item,
:requested_content_item_path,
Expand Down
13 changes: 2 additions & 11 deletions app/presenters/supergroups/guidance_and_regulation.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
module Supergroups
class GuidanceAndRegulation < Supergroup
def initialize(current_path, taxon_ids)
@current_path = current_path
@taxon_ids = taxon_ids
@content = fetch_content
def initialize(current_path, taxon_ids, filters)
super(current_path, taxon_ids, filters, MostPopularContent)
end

def tagged_content
format_document_data(@content, include_timestamp: false)
end

private

def fetch_content
return [] unless @taxon_ids.any?
MostPopularContent.fetch(content_ids: @taxon_ids, current_path: @current_path, filter_content_purpose_supergroup: "guidance_and_regulation")
end
end
end
37 changes: 11 additions & 26 deletions app/presenters/supergroups/news_and_communications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,41 @@ module Supergroups
class NewsAndCommunications < Supergroup
attr_reader :content

def initialize(current_path, taxon_ids)
@taxon_ids = taxon_ids
@current_path = current_path
@content = fetch
def initialize(current_path, taxon_ids, filters)
super(current_path, taxon_ids, filters, MostRecentContent)
end

def all_news
def tagged_content
return unless @content.any?
{
documents: tagged_content,
documents: documents,
promoted_content: promoted_content,
}
end

def any_news?
@content.any?
end
private

def tagged_content
def documents
items = @content.drop(promoted_content_count)
format_document_data(items)
end

def promoted_content
items = @content.shift(promoted_content_count)
items = @content.take(promoted_content_count)
content = format_document_data(items, data_category: "ImageCardClicked")

content.each do |document|
document_image = news_item_photo(document[:link][:path])
document[:image] = {
url: document_image["url"],
alt: document_image["alt_text"],
context: document_image["context"]
url: document_image["url"],
alt: document_image["alt_text"],
context: document_image["context"]
}
end

content
end

private

def fetch
return [] if @taxon_ids.empty?

MostRecentContent.fetch(
content_ids: @taxon_ids,
current_path: @current_path,
filter_content_purpose_supergroup: "news_and_communications"
)
end

def promoted_content_count
3
end
Expand Down
17 changes: 2 additions & 15 deletions app/presenters/supergroups/policy_and_engagement.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
module Supergroups
class PolicyAndEngagement < Supergroup
def initialize(current_path, taxon_ids)
@current_path = current_path
@taxon_ids = taxon_ids
@content = fetch_content
end

def tagged_content
format_document_data(@content)
end

private

def fetch_content
return [] unless @taxon_ids.any?
MostRecentContent.fetch(content_ids: @taxon_ids, current_path: @current_path, filter_content_purpose_supergroup: "policy_and_engagement")
def initialize(current_path, taxon_ids, filters)
super(current_path, taxon_ids, filters, MostRecentContent)
end
end
end
33 changes: 9 additions & 24 deletions app/presenters/supergroups/services.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
module Supergroups
class Services
class Services < Supergroup
attr_reader :content

def initialize(current_path, taxon_ids)
@taxon_ids = taxon_ids
@current_path = current_path
@content = fetch
def initialize(current_path, taxon_ids, filters)
super(current_path, taxon_ids, filters, MostPopularContent)
end

def all_services
def tagged_content
return unless @content.any?
{
documents: tagged_content,
documents: documents,
promoted_content: promoted_content,
}
end

def any_services?
@content.any?
end
private

def tagged_content
def documents
items = @content.drop(promoted_content_count)
format_document_data(items)
end

def promoted_content
items = @content.shift(promoted_content_count)
items = @content.take(promoted_content_count)
format_document_data(items, "HighlightBoxClicked")
end

private

def fetch
return [] if @taxon_ids.empty?

MostPopularContent.fetch(
content_ids: @taxon_ids,
current_path: @current_path,
filter_content_purpose_supergroup: "services"
)
end

def promoted_content_count
3
end
Expand Down
20 changes: 20 additions & 0 deletions app/presenters/supergroups/supergroup.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
module Supergroups
class Supergroup
def initialize(current_path, taxon_ids, filters, content_order_class)
@current_path = current_path
@taxon_ids = taxon_ids
@filters = default_filters.merge(filters)
@content = fetch_content(content_order_class)
end

def tagged_content
format_document_data(@content)
end

private

def format_document_data(documents, data_category: nil, include_timestamp: true)
Expand Down Expand Up @@ -41,5 +52,14 @@ def data_attributes(base_path, link_text, index, data_category)
def data_module_label
self.class.name.demodulize.camelize(:lower)
end

def default_filters
{ filter_content_purpose_supergroup: self.class.name.demodulize.underscore }
end

def fetch_content(content_order_class)
return [] unless @taxon_ids.any?
content_order_class.fetch(content_ids: @taxon_ids, current_path: @current_path, filters: @filters)
end
end
end
17 changes: 2 additions & 15 deletions app/presenters/supergroups/transparency.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
module Supergroups
class Transparency < Supergroup
def initialize(current_path, taxon_ids)
@current_path = current_path
@taxon_ids = taxon_ids
@content = fetch_content
end

def tagged_content
format_document_data(@content)
end

private

def fetch_content
return [] unless @taxon_ids.any?
MostRecentContent.fetch(content_ids: @taxon_ids, current_path: @current_path, filter_content_purpose_supergroup: "transparency")
def initialize(current_path, taxon_ids, filters)
super(current_path, taxon_ids, filters, MostRecentContent)
end
end
end
Loading

0 comments on commit 44f6c50

Please sign in to comment.