From b5517ce3c5d07b492ac6ef9e7dbd8e8adb2669fc Mon Sep 17 00:00:00 2001 From: George Schena Date: Wed, 6 Nov 2024 08:42:55 +0000 Subject: [PATCH] Add case studies model, controller, presenter, route and tests Commit audit trail: - https://github.com/alphagov/government-frontend/blob/0aee348d51cebacd5344115f2ca02bdb17095249/app/presenters/case_study_presenter.rb - https://github.com/alphagov/government-frontend/blob/0aee348d51cebacd5344115f2ca02bdb17095249/app/views/content_items/case_study.html.erb - https://github.com/alphagov/government-frontend/blob/0aee348d51cebacd5344115f2ca02bdb17095249/test/integration/case_study_test.rb - https://github.com/alphagov/government-frontend/blob/0aee348d51cebacd5344115f2ca02bdb17095249/test/presenters/case_study_presenter_test.rb - https://github.com/alphagov/government-frontend/blob/0aee348d51cebacd5344115f2ca02bdb17095249/config/routes.rb --- app/controllers/case_studies_controller.rb | 11 +++++ app/models/case_study.rb | 5 +++ app/presenters/case_studies_presenter.rb | 18 ++++++++ app/views/case_studies/show.html.erb | 52 ++++++++++++++++++++++ config/routes.rb | 2 + spec/requests/case_study_spec.rb | 13 ++++++ spec/system/case_studies_spec.rb | 41 +++++++++++++++++ 7 files changed, 142 insertions(+) create mode 100644 app/controllers/case_studies_controller.rb create mode 100644 app/models/case_study.rb create mode 100644 app/presenters/case_studies_presenter.rb create mode 100644 app/views/case_studies/show.html.erb create mode 100644 spec/requests/case_study_spec.rb create mode 100644 spec/system/case_studies_spec.rb diff --git a/app/controllers/case_studies_controller.rb b/app/controllers/case_studies_controller.rb new file mode 100644 index 0000000000..55e3717767 --- /dev/null +++ b/app/controllers/case_studies_controller.rb @@ -0,0 +1,11 @@ +class CaseStudiesController < ContentItemsController + def show + @case_study = CaseStudiesPresenter.new(@content_item, view_context) + end + +private + + def content_item_slug + request.path + end +end diff --git a/app/models/case_study.rb b/app/models/case_study.rb new file mode 100644 index 0000000000..be216dafd6 --- /dev/null +++ b/app/models/case_study.rb @@ -0,0 +1,5 @@ +class CaseStudy < ContentItem + include Metadata + include Linkable + include Updatable +end diff --git a/app/presenters/case_studies_presenter.rb b/app/presenters/case_studies_presenter.rb new file mode 100644 index 0000000000..55d22febbd --- /dev/null +++ b/app/presenters/case_studies_presenter.rb @@ -0,0 +1,18 @@ +class CaseStudiesPresenter < ContentItemPresenter + attr_reader :content_item, :view_context + + def initialize(content_item, view_context) + super(content_item) + @content_item = content_item + @view_context = view_context + end + + def title_and_context + { + title: content_item.title, + context: I18n.t("formats.#{content_item.document_type}", count: 1), + context_locale: view_context.t_locale_fallback("formats.#{content_item.document_type}", count: 1), + average_title_length: "long", + } + end +end diff --git a/app/views/case_studies/show.html.erb b/app/views/case_studies/show.html.erb new file mode 100644 index 0000000000..09835dccb5 --- /dev/null +++ b/app/views/case_studies/show.html.erb @@ -0,0 +1,52 @@ +<% content_for :title, "#{@content_item.page_title} - #{I18n.t("formats.#{@content_item.document_type}", count: 1)} - GOV.UK" %> + +<% content_for :extra_headers do %> + <%= render "govuk_publishing_components/components/machine_readable_metadata", { content_item: @content_item.to_h, schema: :article } %> +<% end %> + +
+
+ <%= render 'govuk_publishing_components/components/title', @case_study.title_and_context %> +
+ <%= render 'shared/translations' %> +
+ <%= render 'govuk_publishing_components/components/lead_paragraph', text: @content_item.description %> +
+
+ + + +<%= render 'shared/publisher_metadata_with_logo' %> +<% if @content_item.withdrawn? %> + <%= render 'govuk_publishing_components/components/notice', @content_item.withdrawal_notice_component %> +<% end %> + +
+
+
+
+ <%= render 'components/figure', + src: @content_item.image["url"], + alt: @content_item.image["alt_text"], + credit: @content_item.image["credit"], + caption: @content_item.image["caption"] if @content_item.image %> + + <%= render 'govuk_publishing_components/components/govspeak', { + direction: page_text_direction, + } do %> + <%= raw(@content_item.body) %> + <% end %> +
+ + <%= render 'components/published_dates', { + published: display_date(@content_item.first_published_at), + last_updated: display_date(@content_item.updated), + history: @content_item.history + } %> +
+
+ + <%= render 'shared/sidebar_navigation' %> +
+ +<%= render 'shared/footer_navigation' %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 38344997fb..43819ea8ed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -74,6 +74,8 @@ # Placeholder for attachments being virus-scanned get "/placeholder", to: "placeholder#show" + get "/case-studies/:slug(.:locale)", to: "case_studies#show" + scope "/get-involved" do get "/take-part/:slug", to: "take_part#show" end diff --git a/spec/requests/case_study_spec.rb b/spec/requests/case_study_spec.rb new file mode 100644 index 0000000000..7de3ab0409 --- /dev/null +++ b/spec/requests/case_study_spec.rb @@ -0,0 +1,13 @@ +RSpec.describe "Case Study" do + before do + content_store_has_example_item("/government/case-studies/get-britain-building-carlisle-park", schema: :case_study) + end + + context "GET index" do + it "returns 200" do + get "/government/case-studies/get-britain-building-carlisle-park" + + expect(response).to have_http_status(:ok) + end + end +end diff --git a/spec/system/case_studies_spec.rb b/spec/system/case_studies_spec.rb new file mode 100644 index 0000000000..a91777370c --- /dev/null +++ b/spec/system/case_studies_spec.rb @@ -0,0 +1,41 @@ +RSpec.describe "CaseStudy" do + it_behaves_like "it has meta tags", "case_study", "/government/case-studies/doing-business-in-spain" + it_behaves_like "it has meta tags for images", "case_study", "/government/case-studies/doing-business-in-spain" + + before do + content_store_has_example_item("/government/case-studies/get-britain-building-carlisle-park", schema: :case_study) + content_store_has_example_item("/government/case-studies/doing-business-in-spain", schema: :case_study, example: "doing-business-in-spain") + end + + context "when visiting a Case Study page" do + it "displays the case_study page" do + visit "/government/case-studies/get-britain-building-carlisle-park" + + expect(page).to have_title("Get Britain Building: Carlisle Park - Case study - GOV.UK") + + expect(page).to have_css("h1", text: "Get Britain Building: Carlisle Park") + expect(page).to have_text("Nearly 400 homes are set to be built on the site of a former tar distillery thanks to Gleeson Homes and HCA investment.") + + expect(page).to have_css(".gem-c-translation-nav") + end + + context "when visiting a Withdrawn Case Study page which is also translatable" do + it "displays the case_study page" do + visit "/government/case-studies/doing-business-in-spain" + + expect(page).to have_title("[Withdrawn] Doing business in Spain - Case study - GOV.UK") + + expect(page).to have_css("h1", text: "Doing business in Spain") + expect(page).to have_text("This case study was withdrawn on") + + expect(page).to have_css(".gem-c-translation-nav") + end + end + + it "does not display a single page notification button" do + visit "/government/case-studies/get-britain-building-carlisle-park" + + expect(page).not_to have_css(".gem-c-single-page-notification-button") + end + end +end