Skip to content

Commit

Permalink
Add site creation actions and views
Browse files Browse the repository at this point in the history
We are aiming to archive Transition Config which is currently used to create
sites in Transition.

This adds the actions and views necessary for a user to add a new transition.
  • Loading branch information
jkempster34 committed Sep 5, 2023
1 parent 509d8dc commit 927105f
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 9 deletions.
44 changes: 41 additions & 3 deletions app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
class SitesController < ApplicationController
before_action :find_site
before_action :find_site, only: %i[edit update show]
before_action :find_organisation, only: %i[new create]
before_action :check_user_is_gds_editor, only: %i[edit update]

def new
@site_form = SiteForm.new(organisation_slug: @organisation.whitehall_slug)
end

def create
@site_form = SiteForm.new(create_params)

if (site = @site_form.save)
redirect_to site_path(site), flash: { success: "Transition site created" }
else
render :new
end
end

def edit; end

def update
if @site.update(site_params)
if @site.update(update_params)
redirect_to site_path(@site), flash: { success: "Transition date updated" }
else
redirect_to edit_site_path(@site), flash: { alert: "We couldn't save your change" }
Expand All @@ -24,7 +39,30 @@ def find_site
@site = Site.find_by!(abbr: params[:id])
end

def site_params
def find_organisation
@organisation = Organisation.find_by(whitehall_slug: params[:organisation_id])
end

def create_params
params.require(:site_form).permit(
:organisation_slug,
:abbr,
:tna_timestamp,
:homepage,
:homepage_title,
:global_type,
:global_new_url,
:global_redirect_append_path,
:homepage_furl,
:hostname,
:query_params,
:special_redirect_strategy,
:aliases,
extra_organisations: [],
)
end

def update_params
params.require(:site).permit(:launch_date)
end

Expand Down
2 changes: 2 additions & 0 deletions app/views/organisations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<%= render 'in_conjunction_with' %>
</header>

<%= link_to "Add a transition site", new_organisation_site_path(@organisation), class: "btn btn-default" %>
<% unless @sites.empty? %>
<h2>Sites</h2>
<table class="sites table table-hover table-striped table-bordered" data-module="filterable-table">
Expand Down
96 changes: 96 additions & 0 deletions app/views/sites/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<% 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" %>
</div>

<div class="form-group">
<%= form.label :tna_timestamp %>
<%= form.text_field :tna_timestamp, class: "form-control" %>
</div>

<div class="form-group">
<%= form.label :homepage %>
<%= form.text_field :homepage, class: "form-control" %>
</div>

<div class="form-group">
<%= form.label :hostname %>
<%= form.text_field :hostname, class: "form-control" %>
</div>

<div class="form-group">
<%= form.label :homepage_title %>
<%= form.text_field :homepage_title, class: "form-control" %>
</div>

<div class="form-group">
<%= form.label :extra_organisations %>
<%= form.collection_select :extra_organisations,
form.object.organisations,
:id,
:title,
{},
{ multiple: true, class: "form-control" }
%>
</div>

<div class="form-group">
<%= form.label :homepage_furl %>
<%= form.text_field :homepage_furl, class: "form-control" %>
</div>

<div class="form-group">
<%= form.label :global_type %>
<%= form.collection_radio_buttons :global_type,
Site::GLOBAL_TYPES,
:to_s,
:humanize
%>
</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" %>
</div>

<div class="form-group">
<%= form.label :global_redirect_append_path %>
<%= form.check_box :global_redirect_append_path %>
</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
%>
</div>

<div class="form-group">
<%= form.label :aliases %>
<%= form.text_area :aliases, class: "form-control" %>
</div>

<%= form.submit 'Save', class: 'add-top-margin btn btn-success' %>
</div>
</div>
<% end %>
5 changes: 5 additions & 0 deletions config/breadcrumbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
parent :root
end

crumb :new_site do |organisation|
link "New transition site"
parent :organisation, organisation
end

crumb :site do |site|
link site.default_host.hostname, site_path(site)
parent :organisation, site.organisation
Expand Down
14 changes: 14 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ en:
new_url: 'The URL to redirect to'
mappings_batch:
paths: Old URLs
activemodel:
attributes:
site_form:
abbr: Abbreviated name
host: Host
tna_timestamp: TNA timestamp
homepage: Homepage
homepage_title: Homepage title
extra_organisations: Extra organisations
homepage_furl: Homepage full URL
global_type: Global type
options: Options
global_redirect_append_path: Global redirect append path
special_redirect_strategy: Special redirect strategy
mappings:
success:
all_created: '%{created}%{tagged_with}'
Expand Down
13 changes: 13 additions & 0 deletions features/site.feature
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ Scenario: Jumping to a non-existent site
And I jump to the site or mapping "http://not-a-site.gov.uk"
Then I should see the header "Unknown site"

Scenario: Creating a site
Given I have logged in as a GDS Editor
And there are these organisations without sites:
| whitehall_slug | title |
| ukti | UK Trade & Industry |
| go-science | Government Office for Science |
When I visit the page for the UK Trade & Industry organisation
And I click the link "Add a transition site"
Then I should be on the new transition site page for the UK Trade & Industry organisation
When I fill in the new transition site fields
And I save my changes
Then I should be redirected to the new site

Scenario: Editing a site's transition date as a GDS Editor
Given I have logged in as a GDS Editor
And the date is 29/11/19
Expand Down
34 changes: 28 additions & 6 deletions features/step_definitions/site_interaction_steps.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
When(/^I visit the page for the (.*) organisation$/) do |organisation_title|
organisation = Organisation.find_by(title: organisation_title)
visit organisation_path(organisation)
end

When(/^I visit this site page$/) do
visit site_path(@site)
end

When(/^I edit this site's transition date$/) do
click_link "Edit date"
select("2014", from: "site_launch_date_1i")
select("September", from: "site_launch_date_2i")
select("20", from: "site_launch_date_3i")
click_button "Save"
When(/^I fill in the new transition site fields/) do
fill_in "Abbreviated name", with: "aaib"
fill_in "TNA timestamp", with: "20141104112824"
fill_in "Homepage", with: "https://www.gov.uk/government/organisations/air-accidents-investigation-branch"
fill_in "Hostname", with: "www.aaib.gov.uk"
fill_in "Homepage title", with: "Air accidents investigation branch"
select "Government Office for Science"
fill_in "Homepage full URL", with: "www.gov.uk/aaib"
choose "Redirect"
fill_in "Global new URL", with: "https://www.gov.uk/government/organisations/air-accidents-investigation-branch/about"
fill_in "Query params", with: "file"
check "Global redirect append path"
choose "Via aka"
fill_in "Aliases", with: "aaib.gov.uk,aaib.com"
end

Then(/^I should be on the new transition site page for the (.*) organisation$/) do |organisation_title|
organisation = Organisation.find_by(title: organisation_title)
i_should_be_on_the_path new_organisation_site_path(organisation)
end

Then(/^I should be redirected to the new site$/) do
i_should_be_on_the_path site_path(Site.last)
end
12 changes: 12 additions & 0 deletions spec/controllers/sites_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
let(:site) { create :site, abbr: "moj" }
let(:gds_bob) { create(:gds_editor, name: "Bob Terwhilliger") }

describe "#new" do
let(:organisation) { create(:organisation) }

before { login_as gds_bob }

it "returns a success response" do
get :new, params: { organisation_id: organisation.id }

expect(response.status).to eql(200)
end
end

describe "#edit" do
context "when the user does have permission" do
before do
Expand Down
48 changes: 48 additions & 0 deletions spec/requests/site_creation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "rails_helper"

describe "Site creation", type: :request do
let!(:gds_bob) { create(:gds_editor, name: "Bob Terwhilliger") }
let(:organisation) { create(:organisation, whitehall_slug: "air-accidents-investigation-branch") }
let(:params) { attributes_for :site_form, :with_optional_fields, :with_aliases, organisation_slug: "air-accidents-investigation-branch" }

it "redirects to the new site path" do
post organisation_sites_path(organisation), params: { create_site: params }

expect(response).to redirect_to(site_path(Site.last))
end

it "creates the new site" do
post organisation_sites_path(organisation), params: { create_site: params }

attributes = {
abbr: "aaib",
global_new_url: "https://www.gov.uk/government/organisations/air-accidents-investigation-branch/about",
global_redirect_append_path: true,
global_type: "redirect",
homepage: "https://www.gov.uk/government/organisations/air-accidents-investigation-branch",
homepage_furl: "www.gov.uk/aaib",
homepage_title: "Air accidents investigation branch",
query_params: "file",
special_redirect_strategy: "via_aka",
tna_timestamp: Time.strptime("20141104112824", "%Y%m%d%H%M%S"),
}

expect(Site.last.attributes.with_indifferent_access).to include attributes
expect(Site.last.organisation).to eq organisation
end

it "creates related hosts and aka hosts" do
post organisation_sites_path(organisation), params: { create_site: params }

alias_site_1 = Host.where(hostname: "www.aaib.gov.uk")
alias_site_2 = Host.where(hostname: "aaib.gov.uk")
alias_site_3 = Host.where(hostname: "aaib.com")

expect(alias_site_1).to exist
expect(alias_site_2).to exist
expect(alias_site_3).to exist
expect(Host.where(hostname: "aka.aaib.gov.uk", canonical_host: alias_site_1)).to exist
expect(Host.where(hostname: "aka-aaib.gov.uk", canonical_host: alias_site_2)).to exist
expect(Host.where(hostname: "aka-aaib.com", canonical_host: alias_site_3)).to exist
end
end

0 comments on commit 927105f

Please sign in to comment.