Browse Source

Refactor rendering

pull/2613/head
Rachael Booth 2 years ago
parent
commit
cc3a9da64e
  1. 30
      app/helpers/notifications_helper.rb
  2. 16
      app/models/notification.rb
  3. 2
      app/views/notifications/_notification_banner.html.erb
  4. 2
      app/views/notifications/check_answers.html.erb

30
app/helpers/notifications_helper.rb

@ -14,4 +14,34 @@ module NotificationsHelper
Notification.newest_active_unauthenticated_notification Notification.newest_active_unauthenticated_notification
end end
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 %(<p class="govuk-!-font-weight-bold">#{text}</p>) if @bold
%(<p>#{text}</p>)
end
end
end end

16
app/models/notification.rb

@ -19,19 +19,3 @@ class Notification < ApplicationRecord
Redcarpet::Markdown.new(renderer, no_intra_emphasis: true).render(title) Redcarpet::Markdown.new(renderer, no_intra_emphasis: true).render(title)
end end
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 %(<p class="govuk-!-font-weight-bold">#{text}</p>) if @bold
%(<p>#{text}</p>)
end
end

2
app/views/notifications/_notification_banner.html.erb

@ -6,7 +6,7 @@
<% if notification_count > 1 && current_user.present? %> <% if notification_count > 1 && current_user.present? %>
<p>Notification 1 of <%= notification_count %></p> <p>Notification 1 of <%= notification_count %></p>
<% end %> <% end %>
<%== notification.rendered_title(invert_link_colour: true, bold_all_text: true) %> <%== render_for_banner(notification.title) %>
<% if notification.page_content.present? %> <% if notification.page_content.present? %>
<div class="govuk-body"> <div class="govuk-body">
<%= govuk_link_to notification.link_text, notification_path(notification), class: "govuk-link--inverse govuk-!-font-weight-bold" %> <%= govuk_link_to notification.link_text, notification_path(notification), class: "govuk-link--inverse govuk-!-font-weight-bold" %>

2
app/views/notifications/check_answers.html.erb

@ -22,7 +22,7 @@
<% summary_list.with_row do |row| %> <% summary_list.with_row do |row| %>
<% row.with_key { "Title" } %> <% row.with_key { "Title" } %>
<% row.with_value do %> <% row.with_value do %>
<%== @notification.rendered_title %> <%== render_for_summary(@notification.title) %>
<% end %> <% end %>
<% row.with_action(text: "Change", href: edit_notification_path(@notification)) %> <% row.with_action(text: "Change", href: edit_notification_path(@notification)) %>
<% end %> <% end %>

Loading…
Cancel
Save