-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1728591
commit 0d3c359
Showing
2 changed files
with
80 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,47 @@ | ||
module Formats | ||
class PopularLinksPresenter | ||
# Unlike presenter for other models which inherit from editions model, | ||
# PopularLinksPresenter do not inherit from EditionFormatPresenter | ||
# This is because Edition presenter has dependency on Artifact and | ||
# Popular links are not coupled with Artefact unlike any other models. | ||
# Hence the difference | ||
def initialize(popular_links_edition) | ||
@popular_links_edition = popular_links_edition | ||
end | ||
|
||
def render_for_publishing_api(republish: false) | ||
required_fields.merge optional_fields | ||
end | ||
|
||
private | ||
|
||
attr_reader :popular_links_edition | ||
|
||
def required_fields | ||
{ | ||
title: popular_links_edition.title, | ||
schema_name: "link_collection", | ||
document_type: "link_collection", | ||
public_updated_at: public_updated_at.rfc3339(3), | ||
publishing_app: "publisher", | ||
rendering_app: "frontend", | ||
details:, | ||
} | ||
end | ||
|
||
def details | ||
{ | ||
link_items: popular_links_edition.link_items, | ||
} | ||
end | ||
|
||
def optional_fields | ||
access_limited = { auth_bypass_ids: [popular_links_edition.auth_bypass_id] } | ||
{ access_limited:, public_updated_at: public_updated_at.rfc3339(3) } | ||
end | ||
|
||
def public_updated_at | ||
popular_links_edition.public_updated_at || popular_links_edition.updated_at | ||
end | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
test/unit/presenters/formats/popular_links_format_presenter_test.rb
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,33 @@ | ||
require "test_helper" | ||
|
||
class PopularLinksFormatPresenterTest < ActiveSupport::TestCase | ||
def subject | ||
Formats::PopularLinksPresenter.new(FactoryBot.create(:popular_links, major_change: true)) | ||
end | ||
|
||
context "#render_for_publishing_api" do | ||
setup do | ||
@result = subject.render_for_publishing_api | ||
end | ||
|
||
should "have expected required fields" do | ||
assert_equal "Homepage Popular Links", @result[:title] | ||
assert_equal "link_collection", @result[:schema_name] | ||
assert_equal "link_collection", @result[:document_type] | ||
assert_equal "publisher", @result[:publishing_app] | ||
assert_equal "frontend", @result[:rendering_app] | ||
assert_equal ({ link_items: [{ url: "https://www.url1.com", title: "title1" }, | ||
{ url: "https://www.url2.com", title: "title2" }, | ||
{ url: "https://www.url3.com", title: "title3" }, | ||
{ url: "https://www.url4.com", title: "title4" }, | ||
{ url: "https://www.url5.com", title: "title5" }, | ||
{ url: "https://www.url6.com", title: "title6" }] }), | ||
@result[:details] | ||
end | ||
|
||
should "have expected optional fields" do | ||
assert_not_empty @result[:access_limited] | ||
assert_not_empty @result[:public_updated_at] | ||
end | ||
end | ||
end |