Skip to content

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
46 changes: 46 additions & 0 deletions app/models/concerns/linkable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Linkable
extend ActiveSupport::Concern

def from
organisations_ordered_by_importance + links_group(%w[worldwide_organisations people speaker])
end

private

def organisations_ordered_by_importance
organisations_with_emphasised_first.map do |link|
ActionController::Base.helpers.link_to(link["title"], link["base_path"], class: "govuk-link")
end
end

def links(type)
expanded_links_from_content_item(type)
.select { |link| link["base_path"] || type == "world_locations" }
.map { |link| link_for_type(type, link) }
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 expanded_links_from_content_item(type)
return [] unless content_store_hash.dig("links", type)

content_store_hash.dig("links", type)
end

def emphasised_organisations
content_store_hash.dig("details", "emphasised_organisations") || []
end

def links_group(types)
types.flat_map { |type| links(type) }.uniq
end

def link_for_type(_type, link)
ActionController::Base.helpers.link_to(link["title"], link["base_path"], class: "govuk-link")
end
end
51 changes: 51 additions & 0 deletions spec/models/concerns/linkable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class DummyLinkableContentItem
include Linkable
attr_accessor :content_item

def content_store_hash
@content_item
end

def initialize
@content_item = {
"base_path" => "/a/base/path",
"links" => {},
}
end
end

RSpec.describe Linkable, type: :model do
let(:item) { DummyLinkableContentItem.new }

describe "#from" do
context "when people links have base_paths" do
it "returns the correct links" do
item.content_item["links"]["people"] = [
{
"title" => "Winston Churchill",
"base_path" => "/government/people/winston-churchill",
},
]

expected_from_links = [
%(<a class="govuk-link" href="/government/people/winston-churchill">Winston Churchill</a>),
]
expect(item.from).to eq(expected_from_links)
end
end

context "when people links do not have base_paths" do
it "skips the links" do
item.content_item["links"]["people"] = [
{
"title" => "Winston Churchill",
"base_path" => nil,
},
]

expected_from_links = []
expect(item.from).to eq(expected_from_links)
end
end
end
end

0 comments on commit 9229225

Please sign in to comment.