Class: Xebec::NavBarRenderer
- Inherits:
-
Object
- Object
- Xebec::NavBarRenderer
- Defined in:
- lib/xebec/nav_bar_renderer.rb
Overview
A renderer for a Xebec::NavBar that knows how to turn the NavBar into an HTML list using ActionView helper methods.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(bar, helper) ⇒ NavBarRenderer
constructor
Create a new NavBar renderer object.
- #inspect ⇒ Object
- #method_missing(sym, *args, &block) ⇒ Object
- #respond_to?(sym) ⇒ Boolean
-
#to_s ⇒ String
The HREF for each navigation item is generated as follows:.
Constructor Details
#initialize(bar, helper) ⇒ NavBarRenderer
Create a new NavBar renderer object. The renderer will pass all methods on to the NavBar except for to_s
, which will render the NavBar as an HTML list.
16 17 18 19 20 21 |
# File 'lib/xebec/nav_bar_renderer.rb', line 16 def initialize(, helper) raise ArgumentError.new("#{ || '<nil>'} is not a NavBar") unless .kind_of?(NavBar) raise ArgumentError.new("#{helper || '<nil>'} does not seem to be a view helper") unless helper.respond_to?(:content_tag) && helper.respond_to?(:link_to_unless_current) @bar, @helper = , helper end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
63 64 65 66 |
# File 'lib/xebec/nav_bar_renderer.rb', line 63 def method_missing(sym, *args, &block) return .send(sym, *args, &block) if .respond_to?(sym) super end |
Instance Method Details
#inspect ⇒ Object
54 55 56 |
# File 'lib/xebec/nav_bar_renderer.rb', line 54 def inspect to_s end |
#respond_to?(sym) ⇒ Boolean
58 59 60 61 |
# File 'lib/xebec/nav_bar_renderer.rb', line 58 def respond_to?(sym) return true if .respond_to?(sym) super end |
#to_s ⇒ String
The HREF for each navigation item is generated as follows:
-
if the item was declared with a link (e.g.
nav_item :faq, page_path(:page => :faq)
), use that link -
else, try to use the route named after the navigation item (e.g.
nav_item :home
useshome_path
)
The link text for each navigation is generated as follows:
-
if the internationalization key
navbar.{{nav bar name}}.{{nav item name}}
is defined, use that value -
else, use
nav_item.name.titleize
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/xebec/nav_bar_renderer.rb', line 39 def to_s root_element, = * if .empty? helper.tag(root_element, , false) else helper.content_tag(root_element, ) do helper.content_tag(*list_tag) do .items.inject(''.html_safe) do |content, item| content << render_nav_item(item) end end end end end |