Sha256: c454ae69dfef5442c438d8690f6ed90efd3e550eda15c5a7aaf9f997dd0e0c7d

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

# rubocop:disable Rails/HelperInstanceVariable
module Katalyst
  module Navigation
    module Frontend
      class Builder
        attr_accessor :template

        delegate_missing_to :@template

        def initialize(template, list: {}, item: {}, **menu_options)
          self.template = template
          @menu_options = menu_options.freeze
          @list_options = list.freeze
          @item_options = item.freeze
        end

        def render(tree)
          tag.ul(**menu_options(tree)) do
            tree.each do |item|
              concat render_item(item)
            end
          end
        end

        def render_item(item)
          return unless item.visible?

          tag.li(**item_options(item)) do
            concat public_send("render_#{item.model_name.param_key}", item)
            concat render_children(item) if item.children.any?
          end
        end

        def render_children(item)
          tag.ul(**list_options(item)) do
            item.children.each do |child|
              concat render_item(child)
            end
          end
        end

        def render_heading(heading)
          tag.span(heading.title)
        end

        def render_link(link)
          link_to(link.title, link.url, link.item_options)
        end

        def render_button(link)
          link_to(link.title, link.url, link.item_options)
        end

        private

        def menu_options(_tree)
          @menu_options
        end

        def list_options(_item)
          @list_options
        end

        def item_options(_item)
          @item_options
        end
      end
    end
  end
end
# rubocop:enable Rails/HelperInstanceVariable

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
katalyst-navigation-1.3.4 app/helpers/katalyst/navigation/frontend/builder.rb
katalyst-navigation-1.3.3 app/helpers/katalyst/navigation/frontend/builder.rb