Class: Abc::Html::MenuEntryPresenter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(menu_entry, options = {}) ⇒ MenuEntryPresenter

Returns a new instance of MenuEntryPresenter.



11
12
13
14
15
# File 'app/presenters/abc/html/menu_entry_presenter.rb', line 11

def initialize(menu_entry, options = {})
  options = {:list_element_pair => [:ul, :li]}.merge(options)
  self.menu_entry = menu_entry
  self.list_element_pair = options[:list_element_pair]
end

Instance Attribute Details

#list_element_pairObject

Returns the value of attribute list_element_pair.



9
10
11
# File 'app/presenters/abc/html/menu_entry_presenter.rb', line 9

def list_element_pair
  @list_element_pair
end

#menu_entryObject

Returns the value of attribute menu_entry.



9
10
11
# File 'app/presenters/abc/html/menu_entry_presenter.rb', line 9

def menu_entry
  @menu_entry
end

Instance Method Details

#to_htmlSafeBuffer

Renders a menu item in HTML format, and, if it has children, renders those too.

**A note on HTML safety:**

It is up to each menu item presenter to check that it is rendering itself safely. By default, most of the menu has to be declared safe, or else its constituent tags won’t render properly. So we declare vast swaths of the menu HTML safe, and in truth these parts are. The parts that need to be especially watched are any spots where you render user input. By design, that surface is constrained to the ‘render_as_html` method. You must, however, return a string marked with html_safe from that method or else the menu will raise `ActiveSupport::SafeBuffer::SafeConcatError`.

(This doesn’t meant that the entire string must be unsanitized; just that you must make assurances that your content is appropriately escaped. By default, ABC uses ‘ERB::Util#h` to sanitize the user-interpreted parts, but your derivative classes may have to implement this on their own.)

Returns:

  • (SafeBuffer)

    A safe string containing the MenuEntry and its children.

Raises:

  • (ActiveSupport::SafeBuffer::SafeConcatError)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/presenters/abc/html/menu_entry_presenter.rb', line 36

def to_html
  content = render_as_html
  raise ActiveSupport::SafeBuffer::SafeConcatError unless content.html_safe?

  (list_element_tag) do
    if menu_entry_children.present?
      render_with_children_as_html content
    else
      content
    end
  end.html_safe
end