-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new transition form #1384
Merged
Merged
Add new transition form #1384
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2376ba3
Add constants for global and special redirect types
jkempster34 98a59f2
Add routes for site creation
jkempster34 0250f54
Only validate presence of canonical host or aka if hostname is present
jkempster34 da2db05
Add site form object
jkempster34 6e38101
Add site creation actions and views
jkempster34 e249f33
Validate that host hostname is lowercase
jkempster34 cb051c7
Only allow `Site managers` to create new sites
jkempster34 7e1e35e
Validate that global new URL is absent if the global type is "archive"
jkempster34 ab5026c
Improve appearance of radio buttons
jkempster34 e68936d
Nilify blanks on the Site model
jkempster34 1f8f4ef
Add hints to new site form inputs
jkempster34 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,105 @@ | ||
class SiteForm | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :organisation_slug | ||
attribute :abbr | ||
attribute :tna_timestamp, :datetime | ||
attribute :homepage | ||
attribute :homepage_title | ||
attribute :extra_organisations | ||
attribute :global_type | ||
attribute :global_new_url | ||
attribute :homepage_furl | ||
attribute :query_params | ||
attribute :global_redirect_append_path, :boolean, default: false | ||
attribute :special_redirect_strategy | ||
|
||
attribute :hostname | ||
attribute :aliases | ||
|
||
validate :validate_children | ||
validate :validate_aliases | ||
validate :aliases_are_unique | ||
|
||
def save | ||
return false if invalid? | ||
|
||
ActiveRecord::Base.transaction do | ||
site.save! | ||
hosts.each(&:save!) | ||
aka_hosts.each(&:save!) | ||
end | ||
|
||
site | ||
end | ||
|
||
def organisations | ||
Organisation.where.not(whitehall_slug: organisation_slug) | ||
end | ||
|
||
private | ||
|
||
def site | ||
@site ||= Site.new( | ||
abbr:, | ||
tna_timestamp:, | ||
homepage:, | ||
organisation: Organisation.find_by(whitehall_slug: organisation_slug), | ||
extra_organisations: Organisation.where(title: extra_organisations), | ||
homepage_title:, | ||
homepage_furl:, | ||
global_type:, | ||
global_new_url:, | ||
global_redirect_append_path:, | ||
query_params:, | ||
special_redirect_strategy:, | ||
) | ||
end | ||
|
||
def hosts | ||
[default_host].concat(alias_hosts) | ||
end | ||
|
||
def aka_hosts | ||
hosts.map { |host| build_aka_host(host) } | ||
end | ||
|
||
def default_host | ||
@default_host ||= Host.new(hostname:, site:) | ||
end | ||
|
||
def alias_hosts | ||
return [] if aliases.nil? | ||
|
||
@alias_hosts ||= aliases.split(",").map do |host| | ||
Host.new(hostname: host, site:) | ||
end | ||
end | ||
|
||
def build_aka_host(host) | ||
Host.new(hostname: host.aka_hostname, canonical_host: host, site:) | ||
end | ||
|
||
def validate_children | ||
[site, default_host].each do |child| | ||
errors.merge!(child.errors) if child.invalid? | ||
end | ||
end | ||
|
||
def validate_aliases | ||
alias_hosts.each do |host| | ||
next if host.valid? | ||
|
||
host.errors.each do |error| | ||
errors.add(:aliases, "\"#{host.hostname}\" #{error.message}") | ||
end | ||
end | ||
end | ||
|
||
def aliases_are_unique | ||
if alias_hosts.length != alias_hosts.map(&:hostname).uniq.length | ||
errors.add(:aliases, "must be unique") | ||
end | ||
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
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,120 @@ | ||
<% breadcrumb(:new_site, @organisation) %> | ||
|
||
<div class="page-title-with-border"> | ||
<h1>New transition site</h1> | ||
</div> | ||
|
||
<%= form_for @site_form, html: { role: "form" }, url: organisation_sites_path do |form| %> | ||
<%= render "shared/error_messages", error_messages: form.object.errors.full_messages %> | ||
|
||
<div class="form-group row"> | ||
<div class="col-md-8"> | ||
<%= form.hidden_field :organisation_slug, value: @organisation.whitehall_slug %> | ||
|
||
<div class="form-group"> | ||
<%= form.label :abbr %> | ||
<%= form.text_field :abbr, class: "form-control", aria: { describedby: "abbr-help" } %> | ||
<small id="abbr-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.abbr") %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :tna_timestamp %> | ||
<%= form.text_field :tna_timestamp, class: "form-control", aria: { describedby: "tna-timestamp-help" } %> | ||
<small id="tna-timestamp-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.tna_timestamp").html_safe %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :homepage %> | ||
<%= form.text_field :homepage, class: "form-control", aria: { describedby: "homepage-help" } %> | ||
<small id="homepage-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.homepage") %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :hostname %> | ||
<%= form.text_field :hostname, class: "form-control", aria: { describedby: "hostname-help" } %> | ||
<small id="hostname-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.hostname") %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :homepage_title %> | ||
<%= form.text_field :homepage_title, class: "form-control", aria: { describedby: "homepage-title-help" } %> | ||
<small id="homepage-title-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.homepage_title") %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :extra_organisations %> | ||
<%= form.collection_select :extra_organisations, | ||
form.object.organisations, | ||
:id, | ||
:title, | ||
{}, | ||
{ multiple: true, class: "form-control", aria: { describedby: "extra-organisations-help" } } | ||
%> | ||
<small id="extra-organisations-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.extra_organisations") %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :homepage_furl %> | ||
<%= form.text_field :homepage_furl, class: "form-control", aria: { describedby: "homepage-furl-help" } %> | ||
<small id="homepage-furl-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.homepage_furl") %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :global_type %> | ||
<%= form.collection_radio_buttons :global_type, | ||
Site::GLOBAL_TYPES, | ||
:to_s, | ||
:humanize, | ||
aria: { describedby: "global-type-help" } do |button| %> | ||
<div class="radio"> | ||
<%= button.label do %> | ||
<%= button.radio_button + button.text %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
<small id="global-type-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.global_type") %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :global_new_url %> | ||
<%= form.text_field :global_new_url, class: "form-control" %> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :query_params %> | ||
<%= form.text_field :query_params, class: "form-control", aria: { describedby: "query-params-help" } %> | ||
<small id="query-params-help" class="form-text text-muted row"><%= I18n.t("activemodel.hint.site_form.query_params") %></small> | ||
</div> | ||
|
||
<div class="form-check"> | ||
<%= form.label :global_redirect_append_path %> | ||
<%= form.check_box :global_redirect_append_path, aria: { describedby: "global-redirect-append-path-help" } %> | ||
<small id="global-redirect-append-path-help" class="form-text text-muted row"><%= I18n.t("activemodel.hint.site_form.global_redirect_append_path") %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :special_redirect_strategy %> | ||
<%= form.collection_radio_buttons :special_redirect_strategy, | ||
Site::SPECIAL_REDIRECT_STRATEGY_TYPES, | ||
:to_s, | ||
:humanize, | ||
aria: { describedby: "special-redirect-strategy-help" } do |button| %> | ||
<div class="radio"> | ||
<%= button.label do %> | ||
<%= button.radio_button + button.text %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
<small id="special-redirect-strategy-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.special_redirect_strategy") %></small> | ||
</div> | ||
|
||
<div class="form-group"> | ||
<%= form.label :aliases %> | ||
<%= form.text_area :aliases, class: "form-control", aria: { describedby: "aliases-help" } %> | ||
<small id="aliases-help" class="form-text text-muted"><%= I18n.t("activemodel.hint.site_form.aliases") %></small> | ||
</div> | ||
|
||
<%= form.submit 'Save', class: 'add-top-margin btn btn-success' %> | ||
</div> | ||
</div> | ||
<% 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just not downcase the host before we send it on?