Sha256: 820cfa8cdf3a88c99bdff61d719c78ae3de5143e238a4efb8376ec8aca9adf54

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

module GovukPublishingComponents
  class StepNavHelper
    def initialize(content_store_response, current_path)
      @content_item = content_store_response.to_h
      @current_path = current_path
    end

    def step_navs
      @step_navs ||= parsed_step_navs.map do |step_nav|
        StepNav.new(step_nav)
      end
    end

    def show_sidebar?
      step_navs.count == 1
    end
    alias_method :show_header?, :show_sidebar?

    def show_related_links?
      step_navs.any? && step_navs.count < 5
    end

    def related_links
      step_navs.map do |step_nav|
        {
          href: step_nav.base_path,
          text: step_nav.title
        }
      end
    end

    def sidebar
      if show_sidebar?
        @sidebar ||= first_step_nav.content.tap do |sb|
          configure_for_sidebar(sb)
          sb.merge!(small: true, heading_level: 3)
        end
      end
    end

    def header
      if show_header?
        {
          title: first_step_nav.title,
          path: first_step_nav.base_path
        }
      else
        {}
      end
    end

  private

    attr_reader :content_item, :current_path

    def first_step_nav
      step_navs.first
    end

    def steps
      @steps ||= step_nav[:steps]
    end

    def parsed_step_navs
      content_item.dig("links", "part_of_step_navs").to_a
    end

    def configure_for_sidebar(step_nav_content)
      step_nav_content[:steps].each_with_index do |step, step_index|
        step[:contents].each do |content|
          next unless content[:contents]

          content[:contents].each do |link|
            if link[:href] == current_path
              link[:active] = true
              step_nav_content[:show_step] = step_index + 1
              step_nav_content[:highlight_step] = step_index + 1
              return step_nav_content
            end
          end
        end
      end
      step_nav_content
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
govuk_publishing_components-5.4.0 lib/govuk_publishing_components/step_nav_helper.rb
govuk_publishing_components-5.3.0 lib/govuk_publishing_components/step_nav_helper.rb