Sha256: ee75e45febb39baec75dd0d52b6bbada6b19957583475aec1452b183e249f0ea

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 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" do
        render_menu
      end
    end

    protected

    def render_menu(&block)
      content_tag :ul 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

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.24.3 app/presenters/decidim/menu_presenter.rb
decidim-core-0.24.2 app/presenters/decidim/menu_presenter.rb
decidim-core-0.24.1 app/presenters/decidim/menu_presenter.rb
decidim-core-0.24.0 app/presenters/decidim/menu_presenter.rb
decidim-core-0.24.0.rc2 app/presenters/decidim/menu_presenter.rb
decidim-core-0.24.0.rc1 app/presenters/decidim/menu_presenter.rb