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 27, 2024
1 parent 4eac869 commit 30833e2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 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

private

def fetch_latest_popular_link
Expand All @@ -42,4 +47,8 @@ def remove_leading_and_trailing_url_spaces(links)
end
link_items
end

def publish_latest_popular_links
@latest_popular_links.publish_popular_links
end
end
10 changes: 10 additions & 0 deletions app/helpers/popular_links_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def primary_button_for(model, url, text)
end
end

def secondary_button_for(model, url, text)
form_for model, url:, method: :post do
render "govuk_publishing_components/components/button", {
text:,
margin_bottom: 3,
secondary_solid: true,
}
end
end

def primary_link_button_for(url, text)
render "govuk_publishing_components/components/button", {
text:,
Expand Down
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 @@ -8,5 +8,6 @@
<%= primary_button_for(@latest_popular_links, create_popular_links_path, "Create new edition") %>
<% else %>
<%= primary_link_button_for(edit_popular_links_path(@latest_popular_links), "Edit popular links") %>
<%= secondary_button_for(@latest_popular_links, publish_popular_links_path(@latest_popular_links), "Publish") %>
<% 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
16 changes: 16 additions & 0 deletions test/functional/homepage_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,20 @@ class HomepageControllerTest < ActionController::TestCase
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
end

0 comments on commit 30833e2

Please sign in to comment.