Skip to content

Commit

Permalink
Merge pull request #3363 from alphagov/contents_list_refactor
Browse files Browse the repository at this point in the history
Minor refactor of contents list logic
  • Loading branch information
hannako authored Oct 14, 2024
2 parents 9c2710e + c606970 commit ecfa1a3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 38 deletions.
8 changes: 0 additions & 8 deletions app/presenters/content_item/contents_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def contents_items
end

def show_contents_list?
return false if exclude_contents_list_for_manual_section?
return false if contents_items.count < 2
return true if contents_items.count > 2
return false if no_first_item?
Expand Down Expand Up @@ -111,12 +110,5 @@ def first_item
def no_first_item?
first_item.nil?
end

def exclude_contents_list_for_manual_section?
# MOJ require contents lists for manual section pages.

moj_content = content_item.dig("links", "organisations", 0, "content_id") == "dcc907d6-433c-42df-9ffb-d9c68be5dc4d"
document_type == "manual_section" && !moj_content
end
end
end
10 changes: 10 additions & 0 deletions app/presenters/manual_section_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class ManualSectionPresenter < ContentItemPresenter
include ContentItem::ContentsList
include ContentItem::ManualSection

MOJ_ORGANISATION_CONTENT_ID = "dcc907d6-433c-42df-9ffb-d9c68be5dc4d".freeze

def base_path
manual["base_path"]
end
Expand Down Expand Up @@ -52,6 +54,14 @@ def main
end
end

def show_contents_list?
organisation_content_id == MOJ_ORGANISATION_CONTENT_ID
end

def organisation_content_id
content_item.dig("links", "organisations", 0, "content_id")
end

private

def manual
Expand Down
31 changes: 1 addition & 30 deletions test/presenters/content_item/contents_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@

class ContentItemContentsListTest < ActiveSupport::TestCase
def setup
content_item = {
"title" => "thing",
"document_type" => "manual_section",
"links" => {
"organisations" => [
{
"content_id" => "dcc907d6-433c-42df-9ffb-d9c68be5dc4d",
},
],
},
}
content_item = { "title" => "thing" }
@contents_list = Object.new
@contents_list.stubs(:content_item).returns(content_item)
@contents_list.stubs(:document_type).returns(content_item["document_type"])
@contents_list.stubs(:view_context)
.returns(ApplicationController.new.view_context)
@contents_list.extend(ContentItem::ContentsList)
Expand Down Expand Up @@ -108,24 +97,6 @@ def body
assert @contents_list.show_contents_list?
end

test "#show_contents_list? returns false if the content item is a manual section but excluded from displaying content lists" do
content_item = {
"title" => "thing",
"document_type" => "manual_section",
"links" => {
"organisations" => [
{
"content_id" => "91cd6143-69d5-4f27-99ff-a52fb0d51c74",
},
],
},
}

@contents_list.stubs(:content_item).returns(content_item)
@contents_list.stubs(:document_type).returns(content_item["document_type"])
assert_not @contents_list.show_contents_list?
end

test "#show_contents_list? returns true if number of contents items is 2 and the first item's character count is above 415 including a list" do
class << @contents_list
def body
Expand Down
14 changes: 14 additions & 0 deletions test/presenters/manual_section_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ class PresentedManualSectionTest < ManualSectionPresenterTestCase
assert_match first_section_content_sample, presented_manual_section.main.first[:content]
end

test "hides the contents list by default" do
manual_section = schema_item("what-is-content-design")
assert_equal false, present_example(manual_section).show_contents_list?
end

test "shows the contents list if organisation is MOJ" do
content_item = schema_item("what-is-content-design", "manual_section")
moj_content_id = ManualSectionPresenter::MOJ_ORGANISATION_CONTENT_ID
content_item["links"]["organisations"] = [{ "content_id" => moj_content_id }]
presented = present_example(content_item)

assert_equal true, presented.show_contents_list?
end

def presented_manual_section(overrides = {})
presented_item("what-is-content-design", overrides)
end
Expand Down

0 comments on commit ecfa1a3

Please sign in to comment.