Skip to content

Commit

Permalink
Merge pull request #2929 from alphagov/create-edit-finder-form-filter…
Browse files Browse the repository at this point in the history
…s-and-options

Create 'edit finder' form: "Filters and options"
  • Loading branch information
ChrisBAshton authored Jan 7, 2025
2 parents 31645c1 + 2901c3b commit 476acc4
Show file tree
Hide file tree
Showing 20 changed files with 827 additions and 2 deletions.
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 @@ -144,6 +144,7 @@ GEM
date (3.4.1)
debug_inspector (1.2.0)
diff-lcs (1.5.1)
diffy (3.4.3)
docile (1.4.0)
domain_name (0.6.20240107)
drb (2.2.1)
Expand Down Expand Up @@ -812,6 +813,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/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//= require govuk_publishing_components/dependencies
//= require govuk_publishing_components/lib
//= require govuk_publishing_components/components/add-another
//= require govuk_publishing_components/components/copy-to-clipboard

//= require components/autocomplete
3 changes: 3 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ $govuk-page-width: 1140px;

@import 'govuk_publishing_components/govuk_frontend_support';
@import 'govuk_publishing_components/component_support';
@import 'govuk_publishing_components/components/add-another';
@import 'govuk_publishing_components/components/breadcrumbs';
@import 'govuk_publishing_components/components/button';
@import 'govuk_publishing_components/components/checkboxes';
Expand All @@ -24,4 +25,6 @@ $govuk-page-width: 1140px;
@import 'govuk_publishing_components/components/textarea';
@import 'govuk_publishing_components/components/title';

@import "./components/add-another";
@import "./components/autocomplete";
@import "./components/diff";
4 changes: 4 additions & 0 deletions app/assets/stylesheets/components/_add-another.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.js-add-another__fieldset {
background-color: govuk-colour("light-grey");
padding: 1em;
}
83 changes: 83 additions & 0 deletions app/assets/stylesheets/components/_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: "+";
}
}
}
34 changes: 34 additions & 0 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@ class AdminController < ApplicationController

def summary; end

def edit_facets; end

def edit_metadata; end

def confirm_facets
@params = facets_params
@params["facets"] = @params["facets"].values.map { |facet_params|
next if facet_params["_destroy"] == "1"

Facet.from_finder_admin_form_params(facet_params)
.to_finder_schema_attributes
}.compact

@proposed_schema = FinderSchema.new(@current_format.finder_schema.attributes)
@proposed_schema.update(@params)

render :confirm_facets
end

def confirm_metadata
@params = params.permit(
:name,
Expand Down Expand Up @@ -75,4 +92,21 @@ def email_alert_params
:signup_link,
)
end

def facets_params
allowed_facet_params = %i[
key
name
short_name
type
preposition
display_as_result_metadata
filterable
allowed_values
_destroy
]
params.permit(
facets: allowed_facet_params,
)
end
end
83 changes: 83 additions & 0 deletions app/models/facet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class Facet
include ActiveModel::Model
include ActiveModel::Attributes

attribute :key
attribute :name
attribute :short_name
attribute :type
attribute :preposition
attribute :display_as_result_metadata, :boolean
attribute :filterable, :boolean
attribute :allowed_values
attribute :specialist_publisher_properties

def to_finder_schema_attributes
{
key:,
name:,
short_name:,
type:,
preposition:,
display_as_result_metadata:,
filterable:,
allowed_values:,
specialist_publisher_properties:,
}.compact
end

class << self
def from_finder_admin_form_params(params)
facet = new
facet.key = facet_key(params["key"], params["name"])
facet.name = params["name"]
facet.short_name = nil_if_blank(params["short_name"])
facet.type = facet_type(params["type"])
facet.preposition = nil_if_blank(params["preposition"])
facet.display_as_result_metadata = params["display_as_result_metadata"]
facet.filterable = params["filterable"]
facet.allowed_values = facet_allowed_values(params["allowed_values"], params["type"])
facet.specialist_publisher_properties = facet_specialist_publisher_properties(params["type"])
facet
end

private

def facet_key(key, name)
key.presence || name&.gsub(" ", "")&.underscore
end

def nil_if_blank(str)
str.presence
end

def facet_type(type)
facet_types_that_allow_enum_values.include?(type) ? "text" : type
end

def facet_allowed_values(values, type)
return nil if values.nil? || facet_types_that_allow_enum_values.exclude?(type)

values.split("\n").map do |str|
label = str.match(/^(.+){/)
label = label.nil? ? str.strip : label[1].strip
value = str.match(/{(.+)}/)
value = value.nil? ? str.strip.downcase.gsub(/[^\w\d\s]/, "").gsub(/\s/u, "-") : value[1].strip
{ label:, value: }
end
end

def facet_specialist_publisher_properties(type)
case type
when "enum_text_multiple"
{ select: "multiple" }
when "enum_text_single"
{ select: "one" }
end
end

def facet_types_that_allow_enum_values
%w[enum_text_multiple enum_text_single]
end
end
end
2 changes: 2 additions & 0 deletions app/policies/document_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def can_request_edits_to_finder?
publish?
end
alias_method :summary?, :can_request_edits_to_finder?
alias_method :edit_facets?, :can_request_edits_to_finder?
alias_method :confirm_facets?, :can_request_edits_to_finder?
alias_method :edit_metadata?, :can_request_edits_to_finder?
alias_method :confirm_metadata?, :can_request_edits_to_finder?
alias_method :zendesk?, :can_request_edits_to_finder?
Expand Down
110 changes: 110 additions & 0 deletions app/views/admin/_facet_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<% facet ||= {} %>
<input type="hidden" name="facets[<%= index %>][key]" value="<%=facet["key"]%>">

<%= render "govuk_publishing_components/components/input", {
label: {
text: "Filter",
heading_size: "s",
},
name: "facets[#{index}][name]",
value: facet["name"],
hint: "Example: Disease control zone type"
} %>

<%= render "govuk_publishing_components/components/input", {
label: {
text: "Filter short name (optional)",
heading_size: "s",
},
name: "facets[#{index}][short_name]",
value: facet["short_name"],
hint: "Example: Control zone type"
} %>

<%= render "govuk_publishing_components/components/select", {
id: "facets-#{index}-type",
name: "facets[#{index}][type]",
label: "Type",
options: [
{
text: "Multiple select",
value: "enum_text_multiple", # Temporary value for the form only. It ends up as "text" in the schema, and "allowed_values" is retained
selected: facet.dig("specialist_publisher_properties", "select") == "multiple",
},
{
text: "One option",
value: "enum_text_single", # Temporary value for the form only. It ends up as "text" in the schema, and "allowed_values" is retained
selected: facet.dig("specialist_publisher_properties", "select") == "one",
},
{
text: "Free text",
value: "text", # Ends up as "text" in the schema, and deletes any submitted "allowed_values" value
selected: facet["type"] == "text" && facet["allowed_values"].nil?,
},
{
text: "Date",
value: "date",
selected: facet["type"] == "date",
}
]
} %>

<%= render "govuk_publishing_components/components/textarea", {
label: {
text: "Filter options ('Multiple select' or 'One option' only)",
heading_size: "s",
},
hint: sanitize("Put each option on a new line. The underlying name for existing (live) options will appear in curly braces: please don't edit these, and don't add them for new options (they'll be created automatically later on in the process). Example:<br><strong>Pre-existing value {pre-existing-value}</strong><br><strong>New value</strong>"),
name: "facets[#{index}][allowed_values]",
rows: 5,
value: facet["allowed_values"]&.map { |opt| "#{opt["label"]} {#{opt["value"]}}" }&.join("\n"),
} %>

<%= render "govuk_publishing_components/components/radio", {
heading: "Can users use this filter when searching for content items?",
heading_size: "s",
name: "facets[#{index}][filterable]",
inline: true,
items: [
{
value: "true",
text: "Yes",
checked: facet["filterable"]
},
{
value: "false",
text: "No",
checked: !facet["filterable"]
}
]
} %>

<%= render "govuk_publishing_components/components/radio", {
heading: "Show as information under content item?",
heading_size: "s",
hint: "Example: Air Accidents Investigation Branch reports displays ‘Aircraft category: Commercial - fixed wing’ under relevant content items",
name: "facets[#{index}][display_as_result_metadata]",
inline: true,
items: [
{
value: "true",
text: "Yes",
checked: facet["display_as_result_metadata"]
},
{
value: "false",
text: "No",
checked: !facet["display_as_result_metadata"]
}
]
} %>

<%= render "govuk_publishing_components/components/input", {
label: {
text: "Preposition (to be displayed when filter option is selected)",
heading_size: "s",
},
name: "facets[#{index}][preposition]",
value: facet["preposition"],
hint: "Example: In Find funding for land or farms, selecting an option in the filter ‘Area of interest’ displays the preposition ‘With’ before the selected option"
} %>
Loading

0 comments on commit 476acc4

Please sign in to comment.