From f5e3b16d58f4dc4d678b3f43cc14affa8daf9814 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Tue, 15 Mar 2022 15:34:02 +0000 Subject: [PATCH 1/3] Add helper to render Markdown content pages --- Gemfile | 2 ++ Gemfile.lock | 5 +++++ app/controllers/content_controller.rb | 11 +++++++++++ app/helpers/content_helper.rb | 14 ++++++++++++++ app/views/content/accessibility_statement.md | 0 app/views/content/page.html.erb | 9 +++++++++ app/views/content/privacy_notice.md | 0 config/routes.rb | 4 ++++ spec/helpers/content_helper_spec.rb | 13 +++++++++++++ 9 files changed, 58 insertions(+) create mode 100644 app/controllers/content_controller.rb create mode 100644 app/helpers/content_helper.rb create mode 100644 app/views/content/accessibility_statement.md create mode 100644 app/views/content/page.html.erb create mode 100644 app/views/content/privacy_notice.md create mode 100644 spec/helpers/content_helper_spec.rb diff --git a/Gemfile b/Gemfile index 9ce05d2b5..442986219 100644 --- a/Gemfile +++ b/Gemfile @@ -19,6 +19,8 @@ gem "bootsnap", ">= 1.4.4", require: false gem "govuk-components" # GOV UK component form builder DSL gem "govuk_design_system_formbuilder" +# Convert Markdown into GOV.UK frontend-styled HTML +gem "govuk_markdown" # GOV UK Notify gem "notifications-ruby-client" # Turbo and Stimulus diff --git a/Gemfile.lock b/Gemfile.lock index 1c5f83389..512665f41 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -181,6 +181,9 @@ GEM activemodel (>= 6.1) activesupport (>= 6.1) deep_merge (~> 1.2.1) + govuk_markdown (1.0.0) + activesupport + redcarpet has_scope (0.8.0) actionpack (>= 5.2) activesupport (>= 5.2) @@ -328,6 +331,7 @@ GEM rb-fsevent (0.11.1) rb-inotify (0.10.1) ffi (~> 1.0) + redcarpet (3.5.1) redis (4.6.0) regexp_parser (2.2.1) request_store (1.5.1) @@ -476,6 +480,7 @@ DEPENDENCIES factory_bot_rails govuk-components govuk_design_system_formbuilder + govuk_markdown hotwire-rails json-schema listen (~> 3.3) diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb new file mode 100644 index 000000000..1ee9f55d4 --- /dev/null +++ b/app/controllers/content_controller.rb @@ -0,0 +1,11 @@ +class ContentController < ApplicationController + include ContentHelper + + def accessibility_statement + render_content_page :accessibility_statement + end + + def privacy_notice + render_content_page :privacy_notice, page_title: "Privacy notice for tenants and buyers of new social housing" + end +end diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb new file mode 100644 index 000000000..08dc0b0cd --- /dev/null +++ b/app/helpers/content_helper.rb @@ -0,0 +1,14 @@ +module ContentHelper + def render_content_page(page_name, page_title: nil, locals: {}) + raw_content = File.read("app/views/content/#{page_name}.md") + content_with_erb_tags_replaced = ApplicationController.renderer.render( + inline: raw_content, + locals: locals + ) + + @page_title = page_title || page_name.to_s.humanize + @page_content = GovukMarkdown.render(content_with_erb_tags_replaced).html_safe + + render "content/page" + end +end diff --git a/app/views/content/accessibility_statement.md b/app/views/content/accessibility_statement.md new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/content/page.html.erb b/app/views/content/page.html.erb new file mode 100644 index 000000000..39acc46f9 --- /dev/null +++ b/app/views/content/page.html.erb @@ -0,0 +1,9 @@ +<%= content_for :title, @page_title %> + +
+
+

<%= @page_title %>

+ + <%= @page_content %> +
+
diff --git a/app/views/content/privacy_notice.md b/app/views/content/privacy_notice.md new file mode 100644 index 000000000..e69de29bb diff --git a/config/routes.rb b/config/routes.rb index 2b6b4aa7d..867980bc1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,10 @@ Rails.application.routes.draw do root to: "start#index" + # Content pages + get "/accessibility-statement", to: "content#accessibility_statement" + get "/privacy-notice", to: "content#privacy_notice" + resources :users do member do get "password/edit", to: "users#edit_password" diff --git a/spec/helpers/content_helper_spec.rb b/spec/helpers/content_helper_spec.rb new file mode 100644 index 000000000..8c5172226 --- /dev/null +++ b/spec/helpers/content_helper_spec.rb @@ -0,0 +1,13 @@ +require "rails_helper" + +RSpec.describe ContentHelper do + let(:page_name) { "privacy_notice" } + + describe "render content page" do + it "returns the page" do + expected_html = "Privacy notice" + expect(render_content_page(page_name)).to match(expected_html) + expect(page).to have_title("Privacy notice") + end + end +end From 987158b7f448c4d762c33046d820f8aff056d6aa Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Wed, 16 Mar 2022 09:58:16 +0000 Subject: [PATCH 2/3] Un-anger the cop --- app/helpers/content_helper.rb | 8 ++++---- app/views/content/page.html.erb | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb index 08dc0b0cd..1f81e9a3c 100644 --- a/app/helpers/content_helper.rb +++ b/app/helpers/content_helper.rb @@ -3,12 +3,12 @@ module ContentHelper raw_content = File.read("app/views/content/#{page_name}.md") content_with_erb_tags_replaced = ApplicationController.renderer.render( inline: raw_content, - locals: locals + locals:, ) - @page_title = page_title || page_name.to_s.humanize - @page_content = GovukMarkdown.render(content_with_erb_tags_replaced).html_safe + page_title ||= page_name.to_s.humanize + page_content = GovukMarkdown.render(content_with_erb_tags_replaced).html_safe - render "content/page" + render "content/page", locals: { page_title:, page_content: } end end diff --git a/app/views/content/page.html.erb b/app/views/content/page.html.erb index 39acc46f9..87b59160e 100644 --- a/app/views/content/page.html.erb +++ b/app/views/content/page.html.erb @@ -1,9 +1,9 @@ -<%= content_for :title, @page_title %> +<%= content_for :title, page_title %>
-

<%= @page_title %>

+

<%= page_title %>

- <%= @page_content %> + <%= page_content %>
From c4c0467274c7374eeca72a950a8ac1bf472dce3a Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Wed, 16 Mar 2022 10:34:49 +0000 Subject: [PATCH 3/3] Add a request spec --- app/controllers/content_controller.rb | 17 ++++++++++-- app/helpers/content_helper.rb | 14 ---------- app/views/content/page.html.erb | 6 ++--- spec/helpers/content_helper_spec.rb | 13 --------- spec/requests/content_controller_spec.rb | 34 ++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 32 deletions(-) delete mode 100644 app/helpers/content_helper.rb delete mode 100644 spec/helpers/content_helper_spec.rb create mode 100644 spec/requests/content_controller_spec.rb diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index 1ee9f55d4..ecac6ee5a 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -1,6 +1,4 @@ class ContentController < ApplicationController - include ContentHelper - def accessibility_statement render_content_page :accessibility_statement end @@ -8,4 +6,19 @@ class ContentController < ApplicationController def privacy_notice render_content_page :privacy_notice, page_title: "Privacy notice for tenants and buyers of new social housing" end + +private + + def render_content_page(page_name, page_title: nil, locals: {}) + raw_content = File.read("app/views/content/#{page_name}.md") + content_with_erb_tags_replaced = ApplicationController.renderer.render( + inline: raw_content, + locals:, + ) + + @page_title = page_title || page_name.to_s.humanize + @page_content = GovukMarkdown.render(content_with_erb_tags_replaced).html_safe + + render "content/page" + end end diff --git a/app/helpers/content_helper.rb b/app/helpers/content_helper.rb deleted file mode 100644 index 1f81e9a3c..000000000 --- a/app/helpers/content_helper.rb +++ /dev/null @@ -1,14 +0,0 @@ -module ContentHelper - def render_content_page(page_name, page_title: nil, locals: {}) - raw_content = File.read("app/views/content/#{page_name}.md") - content_with_erb_tags_replaced = ApplicationController.renderer.render( - inline: raw_content, - locals:, - ) - - page_title ||= page_name.to_s.humanize - page_content = GovukMarkdown.render(content_with_erb_tags_replaced).html_safe - - render "content/page", locals: { page_title:, page_content: } - end -end diff --git a/app/views/content/page.html.erb b/app/views/content/page.html.erb index 87b59160e..39acc46f9 100644 --- a/app/views/content/page.html.erb +++ b/app/views/content/page.html.erb @@ -1,9 +1,9 @@ -<%= content_for :title, page_title %> +<%= content_for :title, @page_title %>
-

<%= page_title %>

+

<%= @page_title %>

- <%= page_content %> + <%= @page_content %>
diff --git a/spec/helpers/content_helper_spec.rb b/spec/helpers/content_helper_spec.rb deleted file mode 100644 index 8c5172226..000000000 --- a/spec/helpers/content_helper_spec.rb +++ /dev/null @@ -1,13 +0,0 @@ -require "rails_helper" - -RSpec.describe ContentHelper do - let(:page_name) { "privacy_notice" } - - describe "render content page" do - it "returns the page" do - expected_html = "Privacy notice" - expect(render_content_page(page_name)).to match(expected_html) - expect(page).to have_title("Privacy notice") - end - end -end diff --git a/spec/requests/content_controller_spec.rb b/spec/requests/content_controller_spec.rb new file mode 100644 index 000000000..78a236709 --- /dev/null +++ b/spec/requests/content_controller_spec.rb @@ -0,0 +1,34 @@ +require "rails_helper" + +RSpec.describe ContentController, type: :request do + let(:headers) { { "Accept" => "text/html" } } + let(:page) { Capybara::Node::Simple.new(response.body) } + + describe "render privacy notice content page" do + before do + get "/privacy-notice", headers: headers, params: {} + end + + it "returns a 200" do + expect(response).to have_http_status(:success) + end + + it "returns the page" do + expect(page).to have_title("Privacy notice") + end + end + + describe "render accessibility statement content page" do + before do + get "/accessibility-statement", headers: headers, params: {} + end + + it "returns a 200" do + expect(response).to have_http_status(:success) + end + + it "returns the page" do + expect(page).to have_title("Accessibility statement") + end + end +end