Skip to content

Commit

Permalink
add 'publish' funcionality for popular links
Browse files Browse the repository at this point in the history
  • Loading branch information
Ana Botto committed Jun 24, 2024
1 parent 2de3fae commit 3497b7a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/controllers/homepage_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def update
render "homepage/popular_links/edit"
end

def publish
publish_latest_popular_links
render "homepage/popular_links/show"
end

def fetch_latest_popular_link
@latest_popular_links = PopularLinksEdition.last
end
Expand All @@ -36,4 +41,8 @@ def update_link_items
@latest_popular_links.link_items = link_items
@latest_popular_links.save!
end

def publish_latest_popular_links
@latest_popular_links.publish_popular_links
end
end
1 change: 1 addition & 0 deletions app/models/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Action
PUBLISH = "publish".freeze,
ARCHIVE = "archive".freeze,
NEW_VERSION = "new_version".freeze,
PUBLISH_POPULAR_LINKS = "publish_popular_links".freeze,
].freeze

NON_STATUS_ACTIONS = [
Expand Down
4 changes: 4 additions & 0 deletions app/models/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class CannotDeletePublishedPublication < RuntimeError; end
transition all => :archived, :unless => :archived?
end

event :publish_popular_links do
transition %i[draft] => :published
end

state :in_review do
validates :review_requested_at, presence: true
end
Expand Down
1 change: 1 addition & 0 deletions app/views/homepage/popular_links/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<%= primary_theme_navigation_button("Create new edition", :post, create_popular_links_path) %>
<% else %>
<%= primary_theme_navigation_button("Edit popular links", :get, edit_popular_links_path(@latest_popular_links)) %>
<%= secondary_theme_navigation_button("Publish", :post, publish_popular_links_path(@latest_popular_links)) %>
<% end %>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
post "/homepage/popular-links/create" => "homepage#create", as: "create_popular_links"
get "/homepage/popular-links/:id" => "homepage#edit", as: "edit_popular_links"
patch "/homepage/popular-links/:id" => "homepage#update", as: "update_popular_links"
post "/homepage/popular-links/:id/publish" => "homepage#publish", as: "publish_popular_links"

mount GovukAdminTemplate::Engine, at: "/style-guide"
mount Flipflop::Engine => "/flipflop"
Expand Down
18 changes: 17 additions & 1 deletion test/functional/homepage_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,28 @@ class HomepageControllerTest < ActionController::TestCase
should "render edit template on errors" do
patch :update, params: { id: @popular_links.id,
"popular_links" =>
{ "1" => { "title" => "title has changed", "url" => "https://www.url1.com" }, } }
{ "1" => { "title" => "title has changed", "url" => "https://www.url1.com" } } }

assert_template "homepage/popular_links/edit"
end
end

context "#publish" do
setup do
@popular_links = FactoryBot.create(:popular_links, state: "draft")
end

should "publish latest draft popular links and render show template" do
assert_equal "draft", PopularLinksEdition.last.state

post :publish, params: { id: @popular_links.id }

assert_response :ok
assert_template "homepage/popular_links/show"
assert_equal "published", PopularLinksEdition.last.state
end
end

def create_a_published_popular_links
FactoryBot.create(:popular_links, state: "published")
end
Expand Down
20 changes: 19 additions & 1 deletion test/integration/homepage_popular_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class HomepagePopularLinksTest < JavascriptIntegrationTest
assert page.has_text?("URL", count: 6)
end

should "save when clicked on 'Save'" do
should "update record when clicked on 'Save'" do
fill_in "popular_links[1][title]", with: "new title 1"
click_button("Save")

Expand All @@ -114,6 +114,24 @@ class HomepagePopularLinksTest < JavascriptIntegrationTest
end
end

context "#publish" do
setup do
click_button("Create new edition")
end

should "publish the latest draft popular links" do
row = find_all(".govuk-summary-list__row")
assert row[1].has_text?("Status")
assert row[1].has_text?("DRAFT")

click_button("Publish")

row = find_all(".govuk-summary-list__row")
assert row[1].has_text?("Status")
assert row[1].has_text?("PUBLISHED")
end
end

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

0 comments on commit 3497b7a

Please sign in to comment.