Class: Xebec::NavBarRenderer

Inherits:
Object
  • Object
show all
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.

Instance Method Summary collapse

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.

Parameters:

  • bar (Xebec::NavBar)

    the navigation bar to renderer

  • helper (#tag AND #content_tag AND #link_to AND #current_page?)

    the ActionView helper

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/xebec/nav_bar_renderer.rb', line 16

def initialize(bar, helper)
  raise ArgumentError.new("#{bar || '<nil>'} is not a NavBar") unless bar.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 = bar, 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 bar.send(sym, *args, &block) if bar.respond_to?(sym)
  super
end

Instance Method Details

#inspectObject



54
55
56
# File 'lib/xebec/nav_bar_renderer.rb', line 54

def inspect
  to_s
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/xebec/nav_bar_renderer.rb', line 58

def respond_to?(sym)
  return true if bar.respond_to?(sym)
  super
end

#to_sString

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 uses home_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

Returns:

  • (String)

    the proxied navigation bar as an HTML list.



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, options = *root_navbar_tag
  if bar.empty?
    helper.tag(root_element, options, false)
  else
    helper.(root_element, options) do
      helper.(*list_tag) do
        bar.items.inject(''.html_safe) do |content, item|
          content << render_nav_item(item)
        end
      end
    end
  end
end