Class: Abc::Html::MenuPresenter

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::TagHelper
Defined in:
app/presenters/abc/html/menu_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(menu, options = {}) ⇒ MenuPresenter

Returns a new instance of MenuPresenter.



12
13
14
15
16
17
18
19
# File 'app/presenters/abc/html/menu_presenter.rb', line 12

def initialize(menu, options = {})
  options = default_initialize_options.merge(options)

  self.menu = menu
  self.menu_element = options[:menu_element]
  self.list_element_pair = options[:list_element_pair]
  self.menu_entry_presenter_class = options[:menu_entry_presenter_class]
end

Instance Attribute Details

#list_element_pairObject

Returns the value of attribute list_element_pair.



10
11
12
# File 'app/presenters/abc/html/menu_presenter.rb', line 10

def list_element_pair
  @list_element_pair
end

Returns the value of attribute menu.



10
11
12
# File 'app/presenters/abc/html/menu_presenter.rb', line 10

def menu
  @menu
end

Returns the value of attribute menu_element.



10
11
12
# File 'app/presenters/abc/html/menu_presenter.rb', line 10

def menu_element
  @menu_element
end

#menu_entry_presenter_classObject

Returns the value of attribute menu_entry_presenter_class.



10
11
12
# File 'app/presenters/abc/html/menu_presenter.rb', line 10

def menu_entry_presenter_class
  @menu_entry_presenter_class
end

Instance Method Details

#to_htmlObject

Outputs the menu in HTML format.

Returns:

  • An HTML-safe string containing the markup of the menu in question.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/presenters/abc/html/menu_presenter.rb', line 24

def to_html
  (menu_element) do
    (list_container_tag) do
      # Reduce will concat into a first SafeBuffer here, so if the first
      # node is html_safe, this will produce expected outcome. And of course,
      # MenuEntries are expected to be html_safe.
      menu_entries.reduce(::ActiveSupport::SafeBuffer.new) do |buffer, child|
        menu_entry_presenter = menu_entry_presenter_class.new(
          child, :list_element_pair => list_element_pair
        )
        buffer.safe_concat menu_entry_presenter.to_html
      end
    end
  end.html_safe
end