Class: Facades::Helpers::Navigation::Navigator
- Inherits:
-
Object
- Object
- Facades::Helpers::Navigation::Navigator
- Defined in:
- lib/facades/helpers/navigation.rb
Instance Attribute Summary collapse
-
#links ⇒ Object
Returns the value of attribute links.
-
#nested ⇒ Object
Returns the value of attribute nested.
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#view ⇒ Object
readonly
Returns the value of attribute view.
Class Method Summary collapse
-
.merge_html_classes(*args) ⇒ Object
Takes an options array and adds any additional classes passed to args.
Instance Method Summary collapse
-
#initialize(tpl, options = {}, nested = false) ⇒ Navigator
constructor
A new instance of Navigator.
-
#link(text, href, link_opts = {}, &block) ⇒ Object
Constructs a link wrapped in a list item for use within a navigation list.
-
#render(&block) ⇒ Object
Renders the resulting html list, wrapped in a <nav> tag.
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, = {}, nested = false) @view, @options, @nested, @links = tpl, , nested, [] @path = view.request.path end |
Instance Attribute Details
#links ⇒ Object
Returns the value of attribute links.
43 44 45 |
# File 'lib/facades/helpers/navigation.rb', line 43 def links @links end |
#nested ⇒ Object
Returns the value of attribute nested.
43 44 45 |
# File 'lib/facades/helpers/navigation.rb', line 43 def nested @nested end |
#options ⇒ Object
Returns the value of attribute options.
43 44 45 |
# File 'lib/facades/helpers/navigation.rb', line 43 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
42 43 44 |
# File 'lib/facades/helpers/navigation.rb', line 42 def path @path end |
#view ⇒ Object (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. 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
#link(text, href, link_opts = {}, &block) ⇒ Object
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. = merge_html_classes("active", link.) end if block_given? = Navigator.new(view, wrap_attrs, true) inner = .render(&block) unless .links.empty? output = link_to(link.text, link.href, link.) << inner content_tag(:li, output, wrap_attrs) else content_tag(:li, link_to(link.text, link.href, link.), wrap_attrs) end else content_tag(:li, link_to(link.text, link.href, link.), 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 = .delete(:wrapper) heading = .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 = content_tag(wrapper, view.capture(self, &block), ) return output if nested? if heading heading = generate_heading(heading) output = heading << output end return output if outer === false content_tag(outer, output, wrap_attrs) end |