diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 75cd3c227..0f9104a80 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,6 +7,7 @@ class ApplicationController < ActionController::Base # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception helper :application + rescue_from GdsApi::HTTPNotFound, with: :error_not_found private def set_slimmer_headers @@ -31,4 +32,7 @@ def finder_slug raise NotImplementedError end + def error_not_found + render status: :not_found, text: "404 error not found" + end end diff --git a/app/controllers/email_alert_subscriptions_controller.rb b/app/controllers/email_alert_subscriptions_controller.rb index eedf1df9f..2e2fd14d3 100644 --- a/app/controllers/email_alert_subscriptions_controller.rb +++ b/app/controllers/email_alert_subscriptions_controller.rb @@ -6,11 +6,7 @@ class EmailAlertSubscriptionsController < ApplicationController protect_from_forgery except: :create def new - if content - @signup = signup_presenter - else - error_not_found - end + @signup = signup_presenter end def create @@ -34,7 +30,7 @@ def signup_presenter end def content - @content ||= content_store.content_item(request.path) + @content ||= content_store.content_item!(request.path) end def finder_slug @@ -42,7 +38,7 @@ def finder_slug end def finder - FinderPresenter.new(content_store.content_item("/#{finder_slug}")) + FinderPresenter.new(content_store.content_item!("/#{finder_slug}")) end def finder_format @@ -73,10 +69,4 @@ def email_signup_attributes "filter" => chosen_options, } end - - def error_not_found - render status: :not_found, text: "404 error not found" - end - end - diff --git a/app/controllers/finders_controller.rb b/app/controllers/finders_controller.rb index 181962c85..85299bc99 100644 --- a/app/controllers/finders_controller.rb +++ b/app/controllers/finders_controller.rb @@ -17,7 +17,7 @@ def show private def finder @finder ||= FinderPresenter.new( - content_store.content_item("/#{finder_slug}"), + content_store.content_item!("/#{finder_slug}"), facet_params, keywords, ) diff --git a/docs/finder-content-item.md b/docs/finder-content-item.md new file mode 100644 index 000000000..a0db6f983 --- /dev/null +++ b/docs/finder-content-item.md @@ -0,0 +1,61 @@ +# The Finder Content Item Format + +A Finder Content Item is a specialisation of the [Content Item](https://github.com/alphagov/content-store/blob/master/doc/content_item_fields.md). This guide explains what goes in the details hash of the ContentItem and why. + +The Finder Content Item is used by Finder Frontend to render the Finder page. Most of what it uses goes in the `details` hash. + +# The `details` hash + +## `beta` + +A boolean. Required. + +A flag used to decide if the Beta banner should be rendered. + +## `beta_message` + +A string. Optional. Can be set to `null`. + +Can contain HTML. If `beta` is true, `beta_message` will be passed to the beta banner. + +## `document_noun` + +A string. Required. + +The lowercase singular version of whatever format the Finder is using. For example: [`/cma-cases`](https://www.gov.uk/cma-cases) has a `document_noun` of `case`, [`/aaib-report`](https://www.gov.uk/aaib-reports) has a `document_noun` of `report`. This is used to construct the sentence descriving the current search by the user. + +## `document_type` + +A string. Required. + +[snake_case](http://en.wikipedia.org/wiki/Snake_case) string which tells Finder Frontend what doctype to limit the search to in Rummager. It must match the name of the file describing the doctype [in Rummager](https://github.com/alphagov/rummager/tree/master/config/schema/default/doctypes). + +## `email_signup_enabled` + +A boolean. Required. + +Used to decide if the link to the email alert signup page should be displayed + +## `format_name` + +A string. Optional. + +Not specifically used by the Finder, but used by [Specialist Frontend](https://github.com/alphagov/specialist-frontend) to link back to the Finder. +Usually a singularised version of the title of the Finder - `"Competition and Markets Authority case"` for [`/cma-cases`](https://www.gov.uk/cma-cases) for example. +However there are edge cases where it's not the same such as `"Medical safety alert"` for [Alerts and recalls for drugs and medical devices](https://www.gov.uk/drug-device-alerts). + +## `signup_link` + +A string. Optional. + +If `email_signup_enabled` is set to true, the link being displayed will point to `base_path/email-signup` where `base_path` is from the Finder object. `signup_link` allows you to point it at a different URL, [Drug Safety Update](https://www.gov.uk/drug-safety-update) and [Drug Device Alerts](https://www.gov.uk/drug-device-alerts) are the two which currently use this feature. + +## `show_summaries` + +A boolean. Required. + +Used to decide if the summaries for Documents should be displayed in the results list. It will truncate the summary at the end of the first sentence. + +## facets + +__TO DO__ diff --git a/spec/controllers/email_alert_subscriptions_controller_spec.rb b/spec/controllers/email_alert_subscriptions_controller_spec.rb index 4a174261b..fc4a8e4a4 100644 --- a/spec/controllers/email_alert_subscriptions_controller_spec.rb +++ b/spec/controllers/email_alert_subscriptions_controller_spec.rb @@ -5,6 +5,17 @@ describe EmailAlertSubscriptionsController do + describe 'GET #new' do + describe "finder email signup item doesn't exist" do + it 'returns a 404, rather than 5xx' do + content_store_does_not_have_item('/does-not-exist/email-signup') + + get :new, slug: 'does-not-exist' + expect(response.status).to eq(404) + end + end + end + describe 'POST "#create"' do let(:alert_name) { double(:alert_name) } let(:alert_identifier) { double(:alert_identifier) } diff --git a/spec/controllers/finders_controller_spec.rb b/spec/controllers/finders_controller_spec.rb new file mode 100644 index 000000000..9c2bbdcfa --- /dev/null +++ b/spec/controllers/finders_controller_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' +require 'gds_api/test_helpers/content_store' +include GdsApi::TestHelpers::ContentStore +include FixturesHelper + +describe FindersController do + describe "GET show" do + describe "finder item doesn't exist" do + it 'returns a 404, rather than 5xx' do + content_store_does_not_have_item('/does-not-exist') + + get :show, slug: 'does-not-exist' + expect(response.status).to eq(404) + end + end + end +end