Skip to content
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

WIP: Spike "pseudo self serve" feature #2775

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem "aws-sdk-s3"
gem "bootsnap", require: false
gem "bootstrap-kaminari-views"
gem "dartsass-rails"
gem "diffy"
gem "gds-api-adapters"
gem "gds-sso"
gem "govspeak"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ GEM
date (3.3.4)
debug_inspector (1.2.0)
diff-lcs (1.5.1)
diffy (3.4.2)
docile (1.4.0)
domain_name (0.6.20240107)
drb (2.2.1)
Expand Down Expand Up @@ -799,6 +800,7 @@ DEPENDENCIES
capybara-select-2
dartsass-rails
database_cleaner-mongoid
diffy
factory_bot
gds-api-adapters
gds-sso
Expand Down
1 change: 1 addition & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//= link_directory ../javascripts .js
//= link_tree ../builds
//= link application.css
//= link diff.css
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import "govuk_publishing_components/all_components";

@import "select2";
@import "select2-bootstrap";

Expand Down
83 changes: 83 additions & 0 deletions app/assets/stylesheets/diff.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// stylelint-disable max-nesting-depth

// Diff of two editions

$added-color: #ddffdd;
$strong-added-color: #77f177;
$removed-color: #ffdddd;
$strong-removed-color: #ffaaaa;
$gray-lighter: govuk-colour("light-grey");
$state-danger-text: govuk-colour("red");
$state-success-text: govuk-colour("green");

.diff {
border: 1px solid $gray-lighter;
border-left: 40px solid $gray-lighter;
border-radius: 3px;
padding: 15px;

ul {
padding-left: 0;

li {
min-height: 24px;
margin: 0 -15px;
padding: 0 15px;
word-wrap: break-word;
list-style: none;
position: relative;

del,
ins {
text-decoration: none;
}
}

.del,
.ins {
padding-top: 2px;
}

.del {
background-color: $removed-color;

strong {
font-weight: normal;
background-color: $strong-removed-color;
}
}

.ins {
background-color: $added-color;

strong {
font-weight: normal;
background-color: $strong-added-color;
}
}

.del::before,
.ins::before {
position: absolute;
font-weight: bold;
margin-left: -55px;
width: 40px;
text-align: center;
min-height: 24px;
top: 0;
bottom: 0;
}

.del::before {
color: $state-danger-text;
background-color: $removed-color;
content: "-";
}

.ins::before {
color: $state-success-text;
background-color: $added-color;
content: "+";
}
}
}
74 changes: 74 additions & 0 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class AdminController < ApplicationController
layout "design_system"

before_action :check_authorisation, if: :document_type_slug

def index_of_admin_forms; end

def edit_metadata; end

def edit_facets; end

def save_metadata
@submitted_params = params.permit(
:name,
:description,
:organisations,
:editing_organisations,
:related,
:base_path,
:content_id,
"filter.format".to_sym,
:format_name,
:document_title,
:document_noun,
).to_unsafe_h
%i[organisations editing_organisations related].each do |str_that_should_be_arr|
@submitted_params[str_that_should_be_arr] = @submitted_params[str_that_should_be_arr].split("\r\n")
if @submitted_params[str_that_should_be_arr].empty?
@submitted_params.delete(str_that_should_be_arr)
end
end
@submitted_params["filter"] = { "format": @submitted_params["filter.format".to_sym] }
@submitted_params.delete("filter.format".to_sym)

@original_schema = current_format.finder_schema.schema
@proposed_schema = @original_schema.merge(@submitted_params)
render :temporary_output
end

def save_facets
@submitted_params = params.except(:authenticity_token, :action, :controller, :document_type_slug).to_unsafe_h
# hashes come through as e.g. `facets: { "0": { "name": "Category"... }}`,
# we need to convert to array e.g. `facets: [ { "name", "Category"... } ]`
@submitted_params["facets"] = @submitted_params["facets"].keys.map(&:to_i).sort.map do |i|
@submitted_params["facets"][i.to_s]
end
@submitted_params["facets"].each_with_index do |hash, i|
hash.each do |key, value|
# delete empty values
if value == ""
@submitted_params["facets"][i].delete(key)
# cast booleans
elsif %w[true false].include?(value)
@submitted_params["facets"][i][key] = value == "true"
end
end
end

@original_schema = current_format.finder_schema.schema
@proposed_schema = @original_schema.merge(@submitted_params)
render :temporary_output
end

private

def check_authorisation
if current_format
authorize current_format
else
flash[:danger] = "That format doesn't exist. If you feel you've reached this in error, please contact your main GDS contact."
redirect_to root_path
end
end
end
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module ApplicationHelper
def render_back_link(options)
render("govuk_publishing_components/components/back_link", options)
end

def facet_options(form, facet)
form.object.facet_options(facet)
end
Expand Down
10 changes: 10 additions & 0 deletions app/policies/document_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ def index?
# FIXME: fix this, attachments are using the wrong policy
alias_method :destroy?, :index?

def can_request_edits_to_finder?
# TODO: figure out who should be allowed to do what RE administrating finders
publish?
end
alias_method :index_of_admin_forms?, :can_request_edits_to_finder?
alias_method :edit_metadata?, :can_request_edits_to_finder?
alias_method :save_metadata?, :can_request_edits_to_finder?
alias_method :edit_facets?, :can_request_edits_to_finder?
alias_method :save_facets?, :can_request_edits_to_finder?

def publish?
document_type_editor? || gds_editor? || departmental_editor?
end
Expand Down
41 changes: 41 additions & 0 deletions app/views/admin/edit_facets.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<%= content_for :page_title, "Edit #{current_format.title} finder" %>

<% content_for :back_link, render_back_link(href: "/admin/#{current_format.admin_slug}") %>

<h1>Editing <%= current_format.title %> finder</h1>

<div class="row">
<div class="col-md-8">
<p>The intention is for this to start off as "pseudo self serve". Departments would be able to make edits to this form, generate the JSON output, and then send the JSON to a developer to apply (the onus would be on the dev to 'git diff' the changes to make sure they make sense). In time, we may then swap out aspects of this for 'true' self-serve. It could also form the basis of a form be used for 'new' specialist finders, as opposed to just editing existing ones.</p>

<%= form_tag do %>
<h2>Facets</h2>
<% current_format.finder_schema.schema["facets"].each_with_index do |facet, i| %>
<h4><%= facet["name"] %></h4>
<% %w[key name short_name type preposition display_as_result_metadata filterable].each do |property| %>
<%= render "govuk_publishing_components/components/input", {
label: {
text: property,
},
name: "facets[#{i}][#{property}]",
value: facet[property].to_s,
} # could add ` if facet[property]` to hide the empty inputs, but then there'd be no way of adding them
%>
<% end %>
<%= render "govuk_publishing_components/components/textarea", {
label: {
text: "'allowed_values' (JSON hashes, each on a separate line)",
},
name: "facets[#{i}][allowed_values]",
rows: facet["allowed_values"] ? 10 : 3, # only show a large textarea if there is an existing value
value: facet["allowed_values"]&.join("\n"),
} %>
<hr />
<% end %>

<%= render "govuk_publishing_components/components/button", {
text: "Generate schema"
} %>
<% end %>
</div>
</div>
62 changes: 62 additions & 0 deletions app/views/admin/edit_metadata.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<%= content_for :page_title, "Edit #{current_format.title} finder" %>

<% content_for :back_link, render_back_link(href: "/admin/#{current_format.admin_slug}") %>

<h1>Editing <%= current_format.title %> finder</h1>

<div class="row">
<div class="col-md-8">
<%= form_tag do %>
<h2>Finder metadata</h2>
<p>How the finder displays to users</p>
<% %w[name description].each do |input| %>
<%= render "govuk_publishing_components/components/input", {
label: {
text: input,
},
name: input,
value: current_format.finder_schema.schema[input],
} %>
<% end %>

<% # special case for arrays %>
<% %w[organisations editing_organisations related].each do |input| %>
<%= render "govuk_publishing_components/components/textarea", {
label: {
text: "#{input} (content IDs, each on a separate line)",
},
name: input,
rows: 3,
value: current_format.finder_schema.schema[input]&.join(","),
} %>
<% end %>

<% # special case for 'nested' structure `filter.format` %>
<%= render "govuk_publishing_components/components/input", {
label: {
text: "filter.format",
},
name: "filter.format",
value: current_format.finder_schema.schema["filter"]["format"],
readonly: true,
} %>

<h2>Publisher configuration</h2>
<p>How the finder and its documents are referred to in Specialist Publisher.</p>

<% %w[format_name document_noun document_title].each do |input| %>
<%= render "govuk_publishing_components/components/input", {
label: {
text: input,
},
name: input,
value: current_format.finder_schema.schema[input],
} %>
<% end %>

<%= render "govuk_publishing_components/components/button", {
text: "Generate schema"
} %>
<% end %>
</div>
</div>
14 changes: 14 additions & 0 deletions app/views/admin/index_of_admin_forms.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%= content_for :page_title, "Edit #{current_format.title} finder" %>

<% content_for :back_link, render_back_link(href: documents_path(current_format.admin_slug)) %>

<h1>Editing <%= current_format.title %> finder</h1>

<div class="row">
<div class="col-md-8">
<ul>
<li><%= link_to "Edit metadata", "/admin/metadata/#{current_format.admin_slug}" %></li>
<li><%= link_to "Edit facets", "/admin/facets/#{current_format.admin_slug}" %></li>
</ul>
</div>
</div>
30 changes: 30 additions & 0 deletions app/views/admin/temporary_output.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%= content_for :page_title, "Edit #{current_format.title} finder" %>

<% content_for :back_link, render_back_link(href: "/admin/#{current_format.admin_slug}") %>

<h1>Submit request for changes to <%= current_format.title %> finder</h1>

<div class="row">
<div class="col-md-8">
<p>Please visit <a href="https://support.publishing.service.gov.uk/changes_to_publishing_apps_request/new">Changes to publishing applications or technical advice</a> and copy and paste the "Proposed JSON" code for a developer to action. Ignore the other outputs below it, which are for debugging only.</p>
<p>Longer term, we will eliminate this step and automatically raise a Zendesk ticket on your behalf.</p>

<h2>Proposed JSON</h2>
<pre><%= JSON.pretty_generate(@proposed_schema) %></pre>

<hr />

<h2>Diff</h2>
<%= Diffy::Diff.new(
JSON.pretty_generate(@original_schema).to_s,
JSON.pretty_generate(@proposed_schema).to_s,

Check notice

Code scanning / Brakeman

Unescaped parameter value. Note

Unescaped parameter value.
allow_empty_diff: false,
).to_s(:html).html_safe %>

<h2>Submitted params</h2>
<pre><%= JSON.pretty_generate(@submitted_params) %></pre>

<h2>Original JSON</h2>
<pre><%= JSON.pretty_generate(@original_schema) %></pre>
</div>
</div>
Loading
Loading