Sha256: bfbf6f636f4a94d32f8f35100d91e9140ffa2d1a3c829d868582f24fce46406b

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 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_attributes)
          self.template    = template
          @menu_attributes = menu_attributes.freeze
          @list_attributes = list.freeze
          @item_attributes = item.freeze
        end

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

        def render_item(item)
          return unless item.visible?

          tag.li(**item_attributes(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_attributes(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.link_attributes)
        end

        def render_button(link)
          link_to(link.title, link.url, **link.link_attributes)
        end

        private

        def menu_attributes(_tree)
          @menu_attributes
        end

        def list_attributes(_item)
          @list_attributes
        end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
katalyst-navigation-1.8.4 app/helpers/katalyst/navigation/frontend/builder.rb
katalyst-navigation-1.8.3 app/helpers/katalyst/navigation/frontend/builder.rb
katalyst-navigation-1.8.2 app/helpers/katalyst/navigation/frontend/builder.rb
katalyst-navigation-1.8.1 app/helpers/katalyst/navigation/frontend/builder.rb
katalyst-navigation-1.8.0 app/helpers/katalyst/navigation/frontend/builder.rb
katalyst-navigation-1.6.0 app/helpers/katalyst/navigation/frontend/builder.rb