From 907a7e7cab4b8cfb7e96e30c76e2a7208dd1b90c Mon Sep 17 00:00:00 2001 From: syed-ali-tw Date: Sat, 6 Jul 2024 16:50:31 +0100 Subject: [PATCH] Update publishing api --- app/controllers/homepage_controller.rb | 10 ++++++++++ app/models/edition.rb | 2 +- app/models/popular_links_edition.rb | 4 ++++ app/services/edition_presenter_factory.rb | 2 ++ test/functional/homepage_controller_test.rb | 13 +++++++++++++ .../unit/services/edition_presenter_factory_test.rb | 5 +++++ 6 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/controllers/homepage_controller.rb b/app/controllers/homepage_controller.rb index 743701685..f9c701fc4 100644 --- a/app/controllers/homepage_controller.rb +++ b/app/controllers/homepage_controller.rb @@ -17,6 +17,8 @@ def edit def update update_link_items + UpdateWorker.perform_async(@latest_popular_links.id.to_s, update_action_is_publish?) + flash[:success] = "Popular links draft saved.".html_safe redirect_to show_popular_links_path rescue StandardError @@ -51,4 +53,12 @@ def remove_leading_and_trailing_url_spaces(links) def publish_latest_popular_links @latest_popular_links.publish_popular_links end + + def update_action_is_publish? + attempted_activity == :publish + end + + def attempted_activity + Edition::ACTIONS.invert[params[:commit]] + end end diff --git a/app/models/edition.rb b/app/models/edition.rb index f58dfd2c6..e5248fb9b 100644 --- a/app/models/edition.rb +++ b/app/models/edition.rb @@ -123,7 +123,7 @@ class ResurrectionError < RuntimeError validates_with SafeHtml, unless: :popular_links_edition? validates_with LinkValidator, on: :update, unless: :archived_or_popular_links? validates_with ReviewerValidator - validates :change_note, presence: { if: :major_change } + validates :change_note, presence: { if: :major_change }, unless: :popular_links_edition? before_save do check_for_archived_artefact diff --git a/app/models/popular_links_edition.rb b/app/models/popular_links_edition.rb index bc0dd2a6a..693943c85 100644 --- a/app/models/popular_links_edition.rb +++ b/app/models/popular_links_edition.rb @@ -33,4 +33,8 @@ def create_draft_popular_links_from_last_record popular_links.save! popular_links end + + def content_id + "ad7968d0-0339-40b2-80bc-3ea1db8ef1b7".freeze + end end diff --git a/app/services/edition_presenter_factory.rb b/app/services/edition_presenter_factory.rb index 8ac040e54..0a6b1bf85 100644 --- a/app/services/edition_presenter_factory.rb +++ b/app/services/edition_presenter_factory.rb @@ -24,6 +24,8 @@ def presenter_class(edition_class) "Formats::SimpleSmartAnswerPresenter" when "TransactionEdition" "Formats::TransactionPresenter" + when "PopularLinksEdition" + "Formats::PopularLinksPresenter" else "Formats::GenericEditionPresenter" end diff --git a/test/functional/homepage_controller_test.rb b/test/functional/homepage_controller_test.rb index 75058352d..0ccf00324 100644 --- a/test/functional/homepage_controller_test.rb +++ b/test/functional/homepage_controller_test.rb @@ -76,6 +76,19 @@ class HomepageControllerTest < ActionController::TestCase assert_equal new_url, PopularLinksEdition.last.link_items[0][:url] end + should "update publishing API" do + UpdateWorker.expects(:perform_async).with(@popular_links.id.to_s, false) + + patch :update, params: { id: @popular_links.id, + "popular_links" => + { "1" => { "title" => "title", "url" => "url.com" }, + "2" => { "title" => "title2", "url" => "https://www.url2.com" }, + "3" => { "title" => "title3", "url" => "https://www.url3.com" }, + "4" => { "title" => "title4", "url" => "https://www.url4.com" }, + "5" => { "title" => "title5", "url" => "https://www.url5.com" }, + "6" => { "title" => "title6", "url" => "https://www.url6.com" } } } + end + should "redirect to show path on success" do new_title = "title has changed" patch :update, params: { id: @popular_links.id, diff --git a/test/unit/services/edition_presenter_factory_test.rb b/test/unit/services/edition_presenter_factory_test.rb index 6dc0f1352..320750d7d 100644 --- a/test/unit/services/edition_presenter_factory_test.rb +++ b/test/unit/services/edition_presenter_factory_test.rb @@ -51,6 +51,11 @@ class EditionPresenterFactoryTest < ActiveSupport::TestCase assert result == "Formats::TransactionPresenter" end + should "return a presenter for PopularLinks" do + result = EditionPresenterFactory.presenter_class("PopularLinksEdition") + assert result == "Formats::PopularLinksPresenter" + end + should "return default presenter for other pages" do result = EditionPresenterFactory.presenter_class("any_other_format") assert result == "Formats::GenericEditionPresenter"