Skip to content

Commit

Permalink
Make presenter for popular links
Browse files Browse the repository at this point in the history
  • Loading branch information
Ana Botto authored and syed-ali-tw committed Jul 7, 2024
1 parent 1728591 commit 0d3c359
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app/presenters/formats/popular_links_presenter.rb
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
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

0 comments on commit 0d3c359

Please sign in to comment.