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

Validate suggested URL #21

Merged
merged 1 commit into from
Nov 26, 2023
Merged
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 app/assets/stylesheets/govuk_publishing_component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@import 'govuk_publishing_components/components/checkboxes';
@import 'govuk_publishing_components/components/details';
@import 'govuk_publishing_components/components/document-list';
@import 'govuk_publishing_components/components/error-summary';
@import 'govuk_publishing_components/components/heading';
@import 'govuk_publishing_components/components/input';
@import 'govuk_publishing_components/components/inset-text';
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@ class ApplicationController < ActionController::Base
password: ENV.fetch("BASIC_AUTH_PASSWORD"),
)
end

private

def show_more_results?
ActiveModel::Type::Boolean.new.cast(cookies[:show_more_results])
end
helper_method :show_more_results?

def show_metadata?
ActiveModel::Type::Boolean.new.cast(cookies[:show_metadata])
end
helper_method :show_metadata?
end
7 changes: 4 additions & 3 deletions app/controllers/feedbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ class FeedbacksController < ApplicationController
def create
@feedback = Feedback.new(feedback_params)

# TODO: Implement unhappy path
if @feedback.save
flash[:success] = "Thank you for your feedback - it will help improve the new GOV.UK search."
redirect_to search_path
else
@search = Search.new(query: @feedback.search_query)
render "searches/show"
end

redirect_to search_path
end

private
Expand Down
10 changes: 0 additions & 10 deletions app/controllers/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,4 @@ def show
def query_params
params.permit(:query).merge(count: show_more_results? ? 10 : 5)
end

def show_more_results?
ActiveModel::Type::Boolean.new.cast(cookies[:show_more_results])
end
helper_method :show_more_results?

def show_metadata?
ActiveModel::Type::Boolean.new.cast(cookies[:show_metadata])
end
helper_method :show_metadata?
end
5 changes: 5 additions & 0 deletions app/models/feedback.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class Feedback
include ActiveModel::Model
include ActiveModel::Validations

validates :suggested_url, format: { with: URI::DEFAULT_PARSER.make_regexp(%w[http https]), allow_blank: true, message: "must be a valid URL" }

attr_accessor :search_query, :result_ratings, :suggested_url, :comments

def save
return false unless valid?

# TODO: Implement me!
Rails.logger.info "Feedback submitted: #{inspect}"
true
Expand Down
24 changes: 22 additions & 2 deletions app/views/searches/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
<% end %>
</div>

<% if @feedback.errors.any? %>
<%= render "govuk_publishing_components/components/error_summary", {
id: "error-summary",
title: "There was a problem with your feedback",
description: "You need to correct these errors before you can submit your feedback.",
items: @feedback.errors.details.map { |field, error|
{
text: @feedback.errors.full_messages_for(field).first,
href: "#feedback_#{field}",
}
}
} %>
<% end %>

<% if @search.show_results? %>
<div class="results-container">
<div class="govuk-grid-row">
Expand All @@ -31,7 +45,7 @@
<div class="govuk-grid-column-full">
<ul class="search-results__list">
<% @search.results.each do |result| %>
<%= render partial: "result", locals: { result:, form: } %>
<%= render partial: "searches/result", locals: { result:, form: } %>
<% end %>
</ul>
</div>
Expand All @@ -40,10 +54,16 @@
<div class="govuk-grid-column-two-thirds search-feedback__fields">
<%= render "govuk_publishing_components/components/details", {
title: "Is the best possible result not included?",
open: @feedback.errors.include?(:suggested_url),
} do %>
<div class="govuk-form-group">
<div class="govuk-form-group <%= 'govuk-form-group--error' if @feedback.errors.include?(:suggested_url) %>">
<%= form.label :suggested_url, "URL for the best possible result", class: "govuk-label" %>
<div class="govuk-hint">Enter the full URL of a page on GOV.UK (or an external link) that you think is a better result for your query than any of the results shown.</div>
<% if @feedback.errors.include?(:suggested_url) %>
<p id="national-insurance-number-error" class="govuk-error-message">
<span class="govuk-visually-hidden">Error:</span> <%= @feedback.errors.full_messages_for(:suggested_url).join(", ") %>
</p>
<% end %>
<%= form.text_field :suggested_url, class: "govuk-input" %>
</div>
<% end %>
Expand Down
5 changes: 4 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
# enabled: "ON"

en:
hello: "Hello world"
activemodel:
attributes:
feedback:
suggested_url: URL for the best possible result