Skip to content

Commit

Permalink
Publish 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 10, 2024
1 parent 6b5dd15 commit 0b48e84
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
21 changes: 4 additions & 17 deletions app/controllers/homepage_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ def edit
end

def update
update_link_items
UpdateWorker.perform_async(@latest_popular_links.id.to_s, update_action_is_publish?)

@latest_popular_links.update_draft(update_link_items)
flash[:success] = "Popular links draft saved.".html_safe
redirect_to show_popular_links_path
rescue StandardError
render "homepage/popular_links/edit"
end

def publish
publish_latest_popular_links
@latest_popular_links.publish
flash[:success] = "Popular links successfully published.".html_safe
render "homepage/popular_links/show"
end

Expand All @@ -38,7 +37,7 @@ def fetch_latest_popular_link

def update_link_items
@latest_popular_links.link_items = remove_leading_and_trailing_url_spaces(params[:popular_links].values)
@latest_popular_links.save!
@latest_popular_links
end

def remove_leading_and_trailing_url_spaces(links)
Expand All @@ -49,16 +48,4 @@ def remove_leading_and_trailing_url_spaces(links)
end
link_items
end

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
11 changes: 11 additions & 0 deletions app/models/popular_links_edition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def create_draft_popular_links_from_last_record
popular_links
end

def publish
Services.publishing_api.publish(content_id, "major", locale: "en")
# This publish_popular_links is a new workflow that was introduced for popular links.
publish_popular_links
end

def update_draft(updated_popular_links)
UpdateService.call(updated_popular_links)
updated_popular_links.save!
end

def content_id
"ad7968d0-0339-40b2-80bc-3ea1db8ef1b7".freeze
end
Expand Down
32 changes: 18 additions & 14 deletions test/functional/homepage_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class HomepageControllerTest < ActionController::TestCase
end

should "update latest PopularLinksEdition with changed title and url" do
UpdateWorker.stubs(:perform_async)
UpdateService.stubs(:call)

assert_equal "title1", @popular_links.link_items[0][:title]
assert_equal "https://www.url1.com", @popular_links.link_items[0][:url]
Expand All @@ -79,22 +79,20 @@ class HomepageControllerTest < ActionController::TestCase
end

should "update publishing API" do
Sidekiq::Testing.inline! do
Services.publishing_api.expects(:put_content)

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
Services.publishing_api.expects(:put_content)

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
UpdateWorker.stubs(:perform_async)
UpdateService.stubs(:call)
new_title = "title has changed"

patch :update, params: { id: @popular_links.id,
Expand Down Expand Up @@ -132,5 +130,11 @@ class HomepageControllerTest < ActionController::TestCase
assert_template "homepage/popular_links/show"
assert_equal "published", PopularLinksEdition.last.state
end

should "publish to publishing API" do
Services.publishing_api.expects(:publish)

post :publish, params: { id: @popular_links.id }
end
end
end
15 changes: 15 additions & 0 deletions test/integration/homepage_popular_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class HomepagePopularLinksTest < JavascriptIntegrationTest

context "#edit" do
setup do
UpdateService.stubs(:call)
click_button("Create new edition")
click_link("Edit popular links")
end
Expand Down Expand Up @@ -135,6 +136,20 @@ class HomepagePopularLinksTest < JavascriptIntegrationTest
end
end

context "#publish" do
setup do
Services.publishing_api.stubs(:publish)
click_button("Create new edition")
end

should "publish latest edition when 'Publish' is clicked" do
click_button("Publish")

assert page.has_text?("PUBLISHED")
assert page.has_text?("Popular links successfully published.")
end
end

def visit_popular_links
visit "/homepage/popular-links"
end
Expand Down

0 comments on commit 0b48e84

Please sign in to comment.