Class: Facades::Helpers::Navigation::Navigator

Inherits:
Object
  • Object
show all
Defined in:
lib/facades/helpers/navigation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tpl, options = {}, nested = false) ⇒ Navigator

Returns a new instance of Navigator.



45
46
47
48
# File 'lib/facades/helpers/navigation.rb', line 45

def initialize(tpl, options = {}, nested = false)
  @view, @options, @nested, @links = tpl, options, nested, []
  @path = view.request.path
end

Instance Attribute Details

Returns the value of attribute links.



43
44
45
# File 'lib/facades/helpers/navigation.rb', line 43

def links
  @links
end

#nestedObject

Returns the value of attribute nested.



43
44
45
# File 'lib/facades/helpers/navigation.rb', line 43

def nested
  @nested
end

#optionsObject

Returns the value of attribute options.



43
44
45
# File 'lib/facades/helpers/navigation.rb', line 43

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



42
43
44
# File 'lib/facades/helpers/navigation.rb', line 42

def path
  @path
end

#viewObject (readonly)

Returns the value of attribute view.



42
43
44
# File 'lib/facades/helpers/navigation.rb', line 42

def view
  @view
end

Class Method Details

.merge_html_classes(*args) ⇒ Object

Takes an options array and adds any additional classes passed to args. If a :class key exists, it is updated. If not, it is added unless the result is an empty string.



125
126
127
128
129
130
# File 'lib/facades/helpers/navigation.rb', line 125

def merge_html_classes(*args)
  opts    = args.extract_options!
  klasses = (opts.delete(:class) || "").split(" ")
  klasses = [klasses, args].flatten.compact.reject{ |c| c.to_s.blank? }.join(" ")
  (klasses.blank? ? opts : opts.merge(:class => klasses))
end

Instance Method Details

Constructs a link wrapped in a list item for use within a navigation list.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/facades/helpers/navigation.rb', line 91

def link(text, href, link_opts = {}, &block)          
  
  wrap_attrs = link_opts.delete(:wrapper) || {}
  link_opts.merge!(:path => path)
  link = NavigationLink.new(text, href, link_opts)
  links << link
  
  if link.active?
    wrap_attrs   = merge_html_classes("active", wrap_attrs)
    link.options = merge_html_classes("active", link.options)
  end
  
  if block_given?
    subnav = Navigator.new(view, wrap_attrs, true)
    inner  = subnav.render(&block)
    unless subnav.links.empty?
      output = link_to(link.text, link.href, link.options) << inner
      (:li, output, wrap_attrs)
    else
      (:li, link_to(link.text, link.href, link.options), wrap_attrs)
    end
  else
    (:li, link_to(link.text, link.href, link.options), wrap_attrs)
  end        
end

#render(&block) ⇒ Object

Renders the resulting html list, wrapped in a <nav> tag.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/facades/helpers/navigation.rb', line 55

def render(&block)
  wrap_attrs = options.delete(:wrapper)
  heading    = options.delete(:heading)
  outer      = :nav
  
  unless wrap_attrs.is_a?(Hash)
    
    if wrap_attrs === false
      wrapper = :ul
      outer   = false
    else            
      wrapper  = wrap_attrs || :ul
    end
    wrap_attrs = {}
  else
    wrapper = wrap_attrs.delete(:tag) || :ul
  end
  
  @wrapper_attrs = wrap_attrs
  @wrapper       = wrapper
  
  output = (wrapper, view.capture(self, &block), options)
  return output if nested?
  
  if heading
    heading = generate_heading(heading)
    output  = heading << output
  end
  return output if outer === false        
  (outer, output, wrap_attrs)
end