-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2218 from alphagov/edit-pop
Create an 'edit' popular links page
- Loading branch information
Showing
18 changed files
with
449 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.sidebar-components { | ||
.govuk-button { | ||
width: 100%; | ||
} | ||
border-top: 2px solid $govuk-brand-colour; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,54 @@ | ||
class HomepageController < ApplicationController | ||
layout "design_system" | ||
before_action :fetch_latest_popular_link | ||
|
||
def show | ||
@latest_popular_links = PopularLinksEdition.last | ||
render "homepage/popular_links/show" | ||
end | ||
|
||
def create | ||
@latest_popular_links = @latest_popular_links.create_draft_popular_links_from_last_record | ||
render "homepage/popular_links/show" | ||
end | ||
|
||
def edit | ||
render "homepage/popular_links/edit" | ||
end | ||
|
||
def update | ||
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 | ||
render "homepage/popular_links/show" | ||
end | ||
|
||
private | ||
|
||
def fetch_latest_popular_link | ||
@latest_popular_links = PopularLinksEdition.last | ||
end | ||
|
||
def update_link_items | ||
@latest_popular_links.link_items = remove_leading_and_trailing_url_spaces(params[:popular_links].values) | ||
@latest_popular_links.save! | ||
end | ||
|
||
def remove_leading_and_trailing_url_spaces(links) | ||
link_items = [] | ||
links.each do |link| | ||
link[:url] = link[:url].strip | ||
link_items << link | ||
end | ||
link_items | ||
end | ||
|
||
def publish_latest_popular_links | ||
@latest_popular_links.publish_popular_links | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
module ErrorsHelper | ||
def errors_for(errors, attribute) | ||
def errors_for(errors, attribute, use_full_message: true) | ||
return nil if errors.blank? | ||
|
||
errors.filter_map { |error| | ||
if error.attribute == attribute | ||
{ | ||
text: error.full_message, | ||
text: use_full_message ? error.full_message : error.message, | ||
} | ||
end | ||
} | ||
.presence | ||
.presence | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,36 @@ | ||
class PopularLinksEdition < Edition | ||
field :link_items, type: Array | ||
validate :six_link_items_present? | ||
validate :all_urls_and_titles_are_present? | ||
validate :six_link_items_present | ||
validate :all_valid_urls_and_titles_are_present | ||
|
||
def six_link_items_present? | ||
def six_link_items_present | ||
errors.add(:link_items, "6 links are required") if link_items.count != 6 | ||
end | ||
|
||
def all_urls_and_titles_are_present? | ||
def all_valid_urls_and_titles_are_present | ||
link_items.each_with_index do |item, index| | ||
errors.add(:item, "A URL is required for Link #{index + 1}") unless item.key?(:url) | ||
errors.add(:item, "A Title is required for Link #{index + 1}") unless item.key?(:title) | ||
errors.add("url#{index + 1}", "URL is required for Link #{index + 1}") unless url_present?(item) | ||
errors.add("title#{index + 1}", "Title is required for Link #{index + 1}") unless title_present?(item) | ||
errors.add("url#{index + 1}", "URL is invalid for Link #{index + 1}, all URLs should have at least one '.' and no spaces.") if url_present?(item) && url_has_spaces_or_has_no_dot?(item[:url]) | ||
end | ||
end | ||
|
||
def url_has_spaces_or_has_no_dot?(url) | ||
url.include?(" ") || url.exclude?(".") | ||
end | ||
|
||
def title_present?(item) | ||
item.key?(:title) && !item[:title].empty? | ||
end | ||
|
||
def url_present?(item) | ||
item.key?(:url) && !item[:url].empty? | ||
end | ||
|
||
def create_draft_popular_links_from_last_record | ||
last_popular_links = PopularLinksEdition.last | ||
popular_links = PopularLinksEdition.new(title: last_popular_links.title, link_items: last_popular_links.link_items, version_number: last_popular_links.version_number.next) | ||
popular_links.save! | ||
popular_links | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<%= render "govuk_publishing_components/components/fieldset", { | ||
legend_text: "Link #{index+1}", | ||
heading_level: 2, | ||
heading_size: "m", | ||
} do %> | ||
<%= render "govuk_publishing_components/components/input", { | ||
label: { | ||
text: "Title", | ||
}, | ||
name: "popular_links[#{index + 1}][title]", | ||
id: "title#{index + 1}", | ||
value: item[:title], | ||
error_items: errors_for(form.object.errors, "title#{index + 1}".to_sym, use_full_message: false), | ||
} %> | ||
|
||
<%= render "govuk_publishing_components/components/input", { | ||
label: { | ||
text: "URL", | ||
}, | ||
name: "popular_links[#{index + 1}][url]", | ||
id: "url#{index + 1}", | ||
value: item[:url], | ||
error_items: errors_for(form.object.errors, "url#{index + 1}".to_sym, use_full_message: false), | ||
} %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<div class="sidebar-components"> | ||
<%= render "govuk_publishing_components/components/heading", { | ||
text: "Options", | ||
font_size: "s", | ||
padding: true | ||
} %> | ||
<% if @latest_popular_links.state == "published" %> | ||
<%= 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<% content_for :page_title, 'Edit popular links' %> | ||
<% content_for :title, 'Edit popular links' %> | ||
<% content_for :title_context, 'Popular on GOV.UK' %> | ||
<% unless @latest_popular_links.errors.empty? %> | ||
<% content_for :error_summary do %> | ||
<%= render("govuk_publishing_components/components/error_summary", { | ||
id: "error-summary", | ||
title: "There is a problem", | ||
items: @latest_popular_links.errors.map do |error| | ||
{ | ||
text: error.message, | ||
href: "##{error.attribute.to_s}", | ||
} | ||
end | ||
}) %> | ||
<% end %> | ||
<% end %> | ||
|
||
<div class="govuk-grid-column-two-thirds"> | ||
<%= form_for @latest_popular_links, url: update_popular_links_path(@latest_popular_links), method: "patch" do |form| %> | ||
<% @latest_popular_links.link_items.each_with_index do |item, index| %> | ||
<%= render "homepage/popular_links/form", item:, index:, form: %> | ||
<% end %> | ||
<div class="govuk-button-group"> | ||
<%= render("govuk_publishing_components/components/button", { | ||
text: "Save", | ||
type: "submit", | ||
}) %> | ||
<%= link_to("Cancel", show_popular_links_path, class: "govuk-link govuk-link--no-visited-state") %> | ||
</div> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.