diff --git a/app/presenters/formats/popular_links_presenter.rb b/app/presenters/formats/popular_links_presenter.rb new file mode 100644 index 000000000..c2d7e039a --- /dev/null +++ b/app/presenters/formats/popular_links_presenter.rb @@ -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 diff --git a/test/unit/presenters/formats/popular_links_format_presenter_test.rb b/test/unit/presenters/formats/popular_links_format_presenter_test.rb new file mode 100644 index 000000000..be0dc3081 --- /dev/null +++ b/test/unit/presenters/formats/popular_links_format_presenter_test.rb @@ -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