From 6dbefab2ed477a6328f57227494b4561f811f239 Mon Sep 17 00:00:00 2001 From: Jessica Jones Date: Tue, 15 Oct 2024 11:42:19 +0100 Subject: [PATCH] Ensure show_contents_list? returns a boolean The original implementation could result in the method returning nil rather than true or false. Which is unexpected, and has been a barrier to changing when and where we display contents lists because of confusing test failures. --- app/presenters/content_item/contents_list.rb | 33 ++++++++------------ 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/app/presenters/content_item/contents_list.rb b/app/presenters/content_item/contents_list.rb index 72f046e27..2fb462803 100644 --- a/app/presenters/content_item/contents_list.rb +++ b/app/presenters/content_item/contents_list.rb @@ -23,14 +23,23 @@ def show_contents_list? return true if contents_items.count > 2 return false if no_first_item? - first_item_has_long_content? || - first_item_has_long_table? || - first_item_has_image_and_long_content? || - first_item_has_image_and_long_table? + first_item_size_requirements_met?(character_count, table_row_count) end private + def first_item_size_requirements_met?(char_count, table_row_count) + first_item_character_count > char_count || first_item_table_rows > table_row_count + end + + def character_count + first_item_has_image? ? MINIMUM_CHARACTER_COUNT_IF_IMAGE_PRESENT : MINIMUM_CHARACTER_COUNT + end + + def table_row_count + first_item_has_image? ? MINIMUM_TABLE_ROW_COUNT_IF_IMAGE_PRESENT : MINIMUM_TABLE_ROW_COUNT + end + def extract_headings_with_ids headings = parsed_body.css("h2").map do |heading| id = heading.attribute("id") @@ -39,10 +48,6 @@ def extract_headings_with_ids headings.compact end - def first_item_has_long_content? - first_item_character_count > MINIMUM_CHARACTER_COUNT - end - def first_item_content element = first_item first_item_text = "" @@ -60,10 +65,6 @@ def first_item_character_count @first_item_character_count ||= first_item_content.length end - def first_item_has_long_table? - first_item_table_rows > MINIMUM_TABLE_ROW_COUNT - end - def find_first_table element = first_item @@ -91,14 +92,6 @@ def first_item_has_image? end end - def first_item_has_image_and_long_content? - first_item_has_image? && first_item_character_count > MINIMUM_CHARACTER_COUNT_IF_IMAGE_PRESENT - end - - def first_item_has_image_and_long_table? - first_item_has_image? && first_item_table_rows > MINIMUM_TABLE_ROW_COUNT_IF_IMAGE_PRESENT - end - def parsed_body @parsed_body ||= Nokogiri::HTML(body) end