Skip to content

Commit

Permalink
341 Refactor in preparation for adding topic breadcrumbs
Browse files Browse the repository at this point in the history
Rename ContentBreadcrumbsBasedOnParent to ContentBreadcrumbsBasedOnAncestors
to clarify when working directly with parent and when with a group of parents
in context navigation. Also removed breadcrumb outer hash as redundant.

Separated finder_breadcrumbs from default breadcrumbs to simplify logic
  • Loading branch information
reggieb committed Jun 17, 2020
1 parent 2b8dcce commit 65503b0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lib/govuk_publishing_components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require "govuk_publishing_components/presenters/related_navigation_helper"
require "govuk_publishing_components/presenters/step_by_step_nav_helper"
require "govuk_publishing_components/presenters/page_with_step_by_step_navigation"
require "govuk_publishing_components/presenters/content_breadcrumbs_based_on_parent"
require "govuk_publishing_components/presenters/content_breadcrumbs_based_on_ancestors"
require "govuk_publishing_components/presenters/content_breadcrumbs_based_on_priority"
require "govuk_publishing_components/presenters/content_breadcrumbs_based_on_taxons"
require "govuk_publishing_components/presenters/checkboxes_helper"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,24 @@ def parent_breadcrumbs
}
end

def parent_is_step_by_step?
parent_item_options[:step_by_step]
end

def best_match_option
return content_item_options unless content_item_navigation.html_publication_with_parent?

step_by_step_header = parent_item_options[:step_by_step]

{
step_by_step: step_by_step_header,
breadcrumbs: step_by_step_header ? parent_breadcrumbs.first : parent_breadcrumbs,
step_by_step: parent_is_step_by_step?,
breadcrumbs: parent_is_step_by_step? ? parent_breadcrumbs.first : parent_breadcrumbs,
}
end

def options(navigation)
if navigation.content_tagged_to_a_finder?
{
step_by_step: false,
breadcrumbs: navigation.breadcrumbs,
breadcrumbs: navigation.finder_breadcrumbs,
}
elsif navigation.content_tagged_to_current_step_by_step?
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module GovukPublishingComponents
module Presenters
# @private
class ContentBreadcrumbsBasedOnAncestors
def initialize(content_item)
@content_item = ContentItem.new(content_item)
end

def breadcrumbs
ordered_ancestors = all_ancestors.map do |ancestor|
{ title: ancestor.title, url: ancestor.base_path }
end

ordered_ancestors << { title: "Home", url: "/" }
ordered_ancestors.reverse!
ordered_ancestors
end

private

attr_reader :content_item

def all_ancestors
ancestors = []

parent = content_item.parent
while parent
ancestors << parent

parent = parent.parent
end

ancestors
end
end
end
end

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def priority_breadcrumbs
end

def breadcrumbs
return breadcrumbs_based_on_parent unless content_tagged_to_a_finder?
breadcrumbs_based_on_ancestors
end

def finder_breadcrumbs
return [] unless parent_finder

[
Expand Down Expand Up @@ -119,8 +122,8 @@ def content_tagged_to_other_step_by_steps?
step_nav_helper.show_also_part_of_step_nav?
end

def breadcrumbs_based_on_parent
ContentBreadcrumbsBasedOnParent.new(content_item).breadcrumbs[:breadcrumbs]
def breadcrumbs_based_on_ancestors
ContentBreadcrumbsBasedOnAncestors.new(content_item).breadcrumbs
end

def step_nav_helper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Presenters
# @private
# Only used by the step by step component
class PageWithStepByStepNavigation
MAX_SECTION_LENGTH = RelatedNavigationHelper::MAX_SECTION_LENGTH

def initialize(content_store_response, current_path, query_parameters = {})
@content_item = content_store_response.to_h.deep_symbolize_keys
@current_path = current_path
Expand Down Expand Up @@ -48,7 +50,7 @@ def show_related_links?
end

def show_also_part_of_step_nav?
active_step_by_step? && also_part_of_step_nav.any? && step_navs_combined_list.count < 5
active_step_by_step? && also_part_of_step_nav.any? && step_navs_combined_list.count < MAX_SECTION_LENGTH
end

def related_links
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec_helper"

RSpec.describe GovukPublishingComponents::Presenters::ContentBreadcrumbsBasedOnParent do
RSpec.describe GovukPublishingComponents::Presenters::ContentBreadcrumbsBasedOnAncestors do
describe "#breadcrumbs" do
it "can handle any valid content item" do
payload = GovukSchemas::RandomExample.for_schema(frontend_schema: "placeholder")
Expand All @@ -13,7 +13,7 @@
breadcrumbs = breadcrumbs_for(content_item)

expect(breadcrumbs).to eq(
breadcrumbs: [
[
{ title: "Home", url: "/" },
],
)
Expand All @@ -24,7 +24,7 @@
breadcrumbs = breadcrumbs_for(content_item)

expect(breadcrumbs).to eq(
breadcrumbs: [
[
{ title: "Home", url: "/" },
],
)
Expand All @@ -42,7 +42,7 @@
breadcrumbs = breadcrumbs_for(content_item)

expect(breadcrumbs).to eq(
breadcrumbs: [
[
{ title: "Home", url: "/" },
{ title: "A-parent", url: "/a-parent" },
],
Expand Down Expand Up @@ -71,7 +71,7 @@
breadcrumbs = breadcrumbs_for(content_item)

expect(breadcrumbs).to eq(
breadcrumbs: [
[
{ title: "Home", url: "/" },
{ title: "Another-parent", url: "/another-parent" },
{ title: "A-parent", url: "/a-parent" },
Expand Down

0 comments on commit 65503b0

Please sign in to comment.