Sha256: 94f58e71f097640a2af726890ba3ce322c5ce9dde3d6f97538962ba931b018df

Contents?: true

Size: 1.21 KB

Versions: 31

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Decidim
  # A presenter to render menus
  class MenuPresenter
    #
    # Initializes a menu for presentation
    #
    # @param name [Symbol] The name of the menu registry to be rendered
    # @param view [ActionView::Base] The view scope to render the menu
    # @param options [Hash] The rendering options for the menu entries
    #
    def initialize(name, view, options = {})
      @name = name
      @view = view
      @options = options
    end

    delegate :items, to: :evaluated_menu
    delegate :content_tag, :safe_join, to: :@view

    def evaluated_menu
      @evaluated_menu ||= begin
        menu = Menu.new(@name)
        menu.build_for(@view)
        menu
      end
    end

    def render
      content_tag :nav, class: "main-nav", "aria-label": @options.fetch(:label, nil) do
        render_menu
      end
    end

    protected

    def render_menu(&block)
      content_tag :ul, @options.fetch(:container_options, {}) do
        elements = block_given? ? [block.call(@view)] : []
        safe_join(elements + menu_items)
      end
    end

    def menu_items
      items.map do |menu_item|
        MenuItemPresenter.new(menu_item, @view, @options).render
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
decidim-core-0.27.9 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.8 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.7 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.6 app/presenters/decidim/menu_presenter.rb
decidim-core-0.26.10 app/presenters/decidim/menu_presenter.rb
decidim-core-0.26.9 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.5 app/presenters/decidim/menu_presenter.rb
decidim-core-0.26.8 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.4 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.3 app/presenters/decidim/menu_presenter.rb
decidim-core-0.26.7 app/presenters/decidim/menu_presenter.rb
decidim-core-0.26.5 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.2 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.1 app/presenters/decidim/menu_presenter.rb
decidim-core-0.26.4 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.0 app/presenters/decidim/menu_presenter.rb
decidim-core-0.26.3 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.0.rc2 app/presenters/decidim/menu_presenter.rb
decidim-core-0.27.0.rc1 app/presenters/decidim/menu_presenter.rb
decidim-core-0.26.2 app/presenters/decidim/menu_presenter.rb