Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor contents list logic #3366

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions app/presenters/content_item/contents_list.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module ContentItem
module ContentsList
CHARACTER_LIMIT = 415
CHARACTER_LIMIT_WITH_IMAGE = 224
TABLE_ROW_LIMIT = 13
TABLE_ROW_LIMIT_WITH_IMAGE = 6
MINIMUM_CHARACTER_COUNT = 415
MINIMUM_CHARACTER_COUNT_IF_IMAGE_PRESENT = 224
MINIMUM_TABLE_ROW_COUNT = 13
MINIMUM_TABLE_ROW_COUNT_IF_IMAGE_PRESENT = 6

def contents
@contents ||=
Expand All @@ -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")
Expand All @@ -39,10 +48,6 @@ def extract_headings_with_ids
headings.compact
end

def first_item_has_long_content?
first_item_character_count > CHARACTER_LIMIT
end

def first_item_content
element = first_item
first_item_text = ""
Expand All @@ -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 > TABLE_ROW_LIMIT
end

def find_first_table
element = first_item

Expand Down Expand Up @@ -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 > CHARACTER_LIMIT_WITH_IMAGE
end

def first_item_has_image_and_long_table?
first_item_has_image? && first_item_table_rows > TABLE_ROW_LIMIT_WITH_IMAGE
end

def parsed_body
@parsed_body ||= Nokogiri::HTML(body)
end
Expand Down
2 changes: 1 addition & 1 deletion test/presenters/content_item/contents_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def body
end
end

@contents_list.expects(:contents_items).returns([{ text: "A heading", id: "custom" }]).once
@contents_list.expects(:show_contents_list?).returns(true).once
@contents_list.contents
@contents_list.contents
end
Expand Down
Loading