From 33d20363dbb133e903aa119b739212bca6293b0b Mon Sep 17 00:00:00 2001 From: Leena Gupte Date: Wed, 16 Oct 2024 15:59:26 +0100 Subject: [PATCH 1/4] Add a render_block helper The render_block method fails silently if a block doesn't exist. This means that the page won't fail if a block is added to the YAML before the block has been built, or if the block name has a typo in it. The page won't fail, the block just won't display. --- app/helpers/block_helper.rb | 7 +++++++ spec/helpers/block_helper_spec.rb | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/app/helpers/block_helper.rb b/app/helpers/block_helper.rb index 766a2786fb..7890debc6e 100644 --- a/app/helpers/block_helper.rb +++ b/app/helpers/block_helper.rb @@ -1,4 +1,11 @@ module BlockHelper + def render_block(block) + render("landing_page/blocks/#{block.type}", block:) + rescue ActionView::MissingTemplate + Rails.logger.warn("Missing template for block #{block.type}") + "" + end + def tab_items_to_component_format(tab_items) tab_items.map do |ti| { diff --git a/spec/helpers/block_helper_spec.rb b/spec/helpers/block_helper_spec.rb index 4345b62c95..1efa2dec54 100644 --- a/spec/helpers/block_helper_spec.rb +++ b/spec/helpers/block_helper_spec.rb @@ -60,4 +60,11 @@ end end end + + describe "#render_block" do + it "returns an empty string when a partial template doesn't exist" do + block = double(type: "not_a_block") + expect(render_block(block)).to be_empty + end + end end From 7a11f0c05c0ba8ccb01ec01c82ce72a79d6d5db0 Mon Sep 17 00:00:00 2001 From: Leena Gupte Date: Wed, 16 Oct 2024 16:00:18 +0100 Subject: [PATCH 2/4] Update landing page views to use the new block_helper methods --- app/views/landing_page/blocks/_blocks_container.html.erb | 2 +- app/views/landing_page/blocks/_card.html.erb | 2 +- app/views/landing_page/blocks/_featured.html.erb | 2 +- app/views/landing_page/blocks/_grid_container.html.erb | 2 +- app/views/landing_page/blocks/_hero.html.erb | 2 +- app/views/landing_page/blocks/_two_column_layout.html.erb | 4 ++-- app/views/landing_page/show.html.erb | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/views/landing_page/blocks/_blocks_container.html.erb b/app/views/landing_page/blocks/_blocks_container.html.erb index 36bf2980d5..3c5ea65df3 100644 --- a/app/views/landing_page/blocks/_blocks_container.html.erb +++ b/app/views/landing_page/blocks/_blocks_container.html.erb @@ -1,5 +1,5 @@
<% block.children.each do |child| %> - <%= render "landing_page/blocks/#{child.type}", block: child %> + <%= render_block(child) %> <% end %>
diff --git a/app/views/landing_page/blocks/_card.html.erb b/app/views/landing_page/blocks/_card.html.erb index 59482f0802..c6a3a9ede1 100644 --- a/app/views/landing_page/blocks/_card.html.erb +++ b/app/views/landing_page/blocks/_card.html.erb @@ -7,7 +7,7 @@
<% block.card_content.each do |subblock| %> - <%= render "landing_page/blocks/#{subblock.type}", block: subblock, inverse: true %> + <%= render_block(subblock) %> <% end %>
diff --git a/app/views/landing_page/blocks/_featured.html.erb b/app/views/landing_page/blocks/_featured.html.erb index 0c134dcce8..2226e31c0d 100644 --- a/app/views/landing_page/blocks/_featured.html.erb +++ b/app/views/landing_page/blocks/_featured.html.erb @@ -4,7 +4,7 @@