-
Notifications
You must be signed in to change notification settings - Fork 17
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 #1384 from alphagov/add-new-transition-form
Add new transition form
- Loading branch information
Showing
19 changed files
with
764 additions
and
6 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,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.