Sha256: 9bb4dad03a8f694a76354c3175df7eadc3ba854cd1934f8aaf49bbd65191c17a

Contents?: true

Size: 1.82 KB

Versions: 223

Compression:

Stored size: 1.82 KB

Contents

module GovukPublishingComponents
  module Presenters
    class Breadcrumbs
      def initialize(breadcrumbs)
        @breadcrumbs = breadcrumbs
      end

      def structured_data
        {
          "@context" => "http://schema.org",
          "@type" => "BreadcrumbList",
          "itemListElement" => item_list_element,
        }
      end

    private

      attr_reader :breadcrumbs

      def item_list_element
        breadcrumbs.each_with_index.map { |crumb, index| Breadcrumb.new(crumb, index) }
          .select(&:is_link?)
          .map(&:item_list_element)
      end
    end

    class Breadcrumb
      extend Forwardable
      def_delegators :@crumb, :[]

      def initialize(crumb, index)
        @crumb = crumb
        @index = index + 1
      end

      def item_list_element
        {
          "@type" => "ListItem",
          "position" => index,
          "item" => list_item_item,
        }
      end

      def is_link?
        crumb[:url].present?
      end

      def path
        crumb[:url]
      end

      def tracking_data(breadcrumbs_length)
        data = {
          track_category: "breadcrumbClicked",
          track_action: index,
          track_label: path,
          track_options: {
            dimension28: breadcrumbs_length.to_s,
            dimension29: crumb[:title],
          },
        }

        is_homepage = crumb[:url] == "/"

        if is_homepage
          data[:track_category] = "homeLinkClicked"
          data[:track_action] = "homeBreadcrumb"
          data[:track_label] = ""
          data[:track_options] = "{}"
        end

        data
      end

    private

      attr_reader :crumb, :index

      def list_item_item
        path = crumb[:url]
        item = { "name" => crumb[:title] }
        item["@id"] = Plek.new.website_root + path if path
        item
      end
    end
  end
end

Version data entries

223 entries across 223 versions & 1 rubygems

Version Path
govuk_publishing_components-34.10.1 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.10.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.9.1 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.9.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.8.1 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.8.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.7.1 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.7.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.6.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.5.1 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.5.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.4.2 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.4.1 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.4.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.3.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.2.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.1.3 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.1.2 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.1.1 lib/govuk_publishing_components/presenters/breadcrumbs.rb
govuk_publishing_components-34.1.0 lib/govuk_publishing_components/presenters/breadcrumbs.rb