Class: Abc::Html::MenuEntryPresenter
- Inherits:
-
Object
- Object
- Abc::Html::MenuEntryPresenter
- Includes:
- ActionView::Context, ActionView::Helpers::TagHelper
- Defined in:
- app/presenters/abc/html/menu_entry_presenter.rb
Instance Attribute Summary collapse
-
#list_element_pair ⇒ Object
Returns the value of attribute list_element_pair.
-
#menu_entry ⇒ Object
Returns the value of attribute menu_entry.
Instance Method Summary collapse
-
#initialize(menu_entry, options = {}) ⇒ MenuEntryPresenter
constructor
A new instance of MenuEntryPresenter.
-
#to_html ⇒ SafeBuffer
Renders a menu item in HTML format, and, if it has children, renders those too.
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(, = {}) = {:list_element_pair => [:ul, :li]}.merge() self. = self.list_element_pair = [:list_element_pair] end |
Instance Attribute Details
#list_element_pair ⇒ Object
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_entry ⇒ Object
Returns the value of attribute menu_entry.
9 10 11 |
# File 'app/presenters/abc/html/menu_entry_presenter.rb', line 9 def @menu_entry end |
Instance Method Details
#to_html ⇒ SafeBuffer
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.)
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? content_tag(list_element_tag) do if .present? render_with_children_as_html content else content end end.html_safe end |