From cc3a9da64e9d5d36d1e3113f0d92f3167fc07715 Mon Sep 17 00:00:00 2001 From: Rachael Booth Date: Mon, 2 Sep 2024 15:27:21 +0100 Subject: [PATCH] Refactor rendering --- app/helpers/notifications_helper.rb | 30 +++++++++++++++++++ app/models/notification.rb | 16 ---------- .../_notification_banner.html.erb | 2 +- .../notifications/check_answers.html.erb | 2 +- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/app/helpers/notifications_helper.rb b/app/helpers/notifications_helper.rb index ab4c8ec21..5780846c0 100644 --- a/app/helpers/notifications_helper.rb +++ b/app/helpers/notifications_helper.rb @@ -14,4 +14,34 @@ module NotificationsHelper Notification.newest_active_unauthenticated_notification end end + + def render_for_banner(title) + @@banner_renderer ||= NotificationTitleRenderer.new({ invert_link_colour: true, bold_all_text: true }) + @@banner_markdown ||= Redcarpet::Markdown.new(@@banner_renderer, no_intra_emphasis: true) + @@banner_markdown.render(title) + end + + def render_for_summary(title) + @@plain_title_renderer ||= NotificationTitleRenderer.new({ invert_link_colour: false, bold_all_text: false }) + @@plain_title_markdown ||= Redcarpet::Markdown.new(@@plain_title_renderer, no_intra_emphasis: true) + @@plain_title_markdown.render(title) + end + +private + + class NotificationTitleRenderer < Redcarpet::Render::HTML + def initialize(options = {}) + link_class = "govuk-link" + link_class += " govuk-link--inverse" if options[:invert_link_colour] + @bold = options[:bold_all_text] + base_options = { escape_html: true, safe_links_only: true, link_attributes: { class: link_class } } + super base_options + end + + def paragraph(text) + return %(

#{text}

) if @bold + + %(

#{text}

) + end + end end diff --git a/app/models/notification.rb b/app/models/notification.rb index 9049dcd3b..92fd08673 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -19,19 +19,3 @@ class Notification < ApplicationRecord Redcarpet::Markdown.new(renderer, no_intra_emphasis: true).render(title) end end - -class NotificationTitleRenderer < Redcarpet::Render::HTML - def initialize(options = {}) - link_class = "govuk-link" - link_class += " govuk-link--inverse" if options[:invert_link_colour] - @bold = options[:bold_all_text] - base_options = { escape_html: true, safe_links_only: true, link_attributes: { class: link_class } } - super base_options - end - - def paragraph(text) - return %(

#{text}

) if @bold - - %(

#{text}

) - end -end diff --git a/app/views/notifications/_notification_banner.html.erb b/app/views/notifications/_notification_banner.html.erb index 67de16afa..512582109 100644 --- a/app/views/notifications/_notification_banner.html.erb +++ b/app/views/notifications/_notification_banner.html.erb @@ -6,7 +6,7 @@ <% if notification_count > 1 && current_user.present? %>

Notification 1 of <%= notification_count %>

<% end %> - <%== notification.rendered_title(invert_link_colour: true, bold_all_text: true) %> + <%== render_for_banner(notification.title) %> <% if notification.page_content.present? %>
<%= govuk_link_to notification.link_text, notification_path(notification), class: "govuk-link--inverse govuk-!-font-weight-bold" %> diff --git a/app/views/notifications/check_answers.html.erb b/app/views/notifications/check_answers.html.erb index a67818870..78a04e2b0 100644 --- a/app/views/notifications/check_answers.html.erb +++ b/app/views/notifications/check_answers.html.erb @@ -22,7 +22,7 @@ <% summary_list.with_row do |row| %> <% row.with_key { "Title" } %> <% row.with_value do %> - <%== @notification.rendered_title %> + <%== render_for_summary(@notification.title) %> <% end %> <% row.with_action(text: "Change", href: edit_notification_path(@notification)) %> <% end %>