-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit audit trail: - https://github.com/alphagov/government-frontend/blob/a938e4bee78b794e4e5303a1bde1dfd05e94b287/app/presenters/content_item/linkable.rb - https://github.com/alphagov/government-frontend/blob/a938e4bee78b794e4e5303a1bde1dfd05e94b287/test/presenters/content_item/linkable_test.rb
- Loading branch information
1 parent
2ee14ea
commit 9229225
Showing
2 changed files
with
97 additions
and
0 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 |
---|---|---|
@@ -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 |
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,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 |