require 'govuk_navigation_helpers/services' require 'govuk_navigation_helpers/configuration' module GovukNavigationHelpers class RelatedNavigationSidebar def initialize(content_item) @content_item = ContentItem.new content_item end def related_navigation_sidebar { related_items: related_items, collections: related_collections, statistical_data_sets: related_statistical_data_sets, topics: related_topics, topical_events: related_topical_events, policies: related_policies, publishers: related_organisations, world_locations: related_world_locations, worldwide_organisations: related_worldwide_organisations, other: [related_external_links, related_contacts] } end private def build_links_for_sidebar(collection, path_key = "base_path", additional_attr = {}) collection.map do |link| { text: link["title"], path: link[path_key] }.merge(additional_attr) end end def world_location_base_path(title) "/world/#{title.downcase.tr(' ', '-')}/news" end def related_items quick_links = build_links_for_sidebar(@content_item.quick_links, "url") quick_links.any? ? quick_links : build_links_for_sidebar(@content_item.related_ordered_items) end def related_organisations build_links_for_sidebar(@content_item.related_organisations) end def related_world_locations locations = @content_item.related_world_locations locations.map! { |link| link.merge("base_path" => world_location_base_path(link["title"])) } build_links_for_sidebar(locations) end def related_worldwide_organisations build_links_for_sidebar(@content_item.related_worldwide_organisations) end def related_collections build_links_for_sidebar(@content_item.related_collections) end def related_policies build_links_for_sidebar(@content_item.related_policies) end def related_statistical_data_sets build_links_for_sidebar(@content_item.related_statistical_data_sets) end def related_topics build_links_for_sidebar(@content_item.related_topics) end def related_topical_events build_links_for_sidebar(@content_item.related_topical_events) end def related_contacts return [] unless @content_item.related_other_contacts.any? [ title: "Other contacts", links: build_links_for_sidebar(@content_item.related_other_contacts).map ] end def related_external_links return [] unless @content_item.external_links.any? [ title: "Elsewhere on the web", links: build_links_for_sidebar(@content_item.external_links, "url", rel: 'external') ] end end end