Module: Insightful::NavigationHelper
- Defined in:
- lib/insightful/navigation_helper.rb
Instance Method Summary collapse
- #breadcrumb(item, options = {}, &block) ⇒ Object
- #breadcrumb_builder(current_url, options, &block) ⇒ Object
-
#menu(nodes, options = {}, &block) ⇒ Object
-
menu(c(:menu)) do |attributes, node| - attributes = “one two” - node.
-
- #menu_builder(nodes, level = 0, options = {}, &block) ⇒ Object
- #nested_set_menu(clazz, options = {}, &block) ⇒ Object
-
#simple_nested_set(clazz) ⇒ Object
Post for example.
Instance Method Details
#breadcrumb(item, options = {}, &block) ⇒ Object
96 97 98 99 100 |
# File 'lib/insightful/navigation_helper.rb', line 96 def (item, = {}, &block) [:from] ||= "/" + item.url.gsub(/^\//, "").split("/").first [:to] ||= item.url ([:from], , &block) end |
#breadcrumb_builder(current_url, options, &block) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/insightful/navigation_helper.rb', line 102 def (current_url, , &block) item = site.find_by_url(current_url) return "" unless item haml_tag :a, .merge(:href => current_url) do if block_given? yield item else haml_concat item.title end end if [:to] != current_url haml_concat [:delimiter] || " » " next_node = [:to].gsub(/#{current_url}\/?/, "").split("/").first ("#{current_url}/#{next_node}", , &block) end end |
#menu(nodes, options = {}, &block) ⇒ Object
-
menu(c(:menu)) do |attributes, node|
- attributes = “one two” - node
7 8 9 |
# File 'lib/insightful/navigation_helper.rb', line 7 def (nodes, = {}, &block) (nodes, 0, , &block) end |
#menu_builder(nodes, level = 0, options = {}, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/insightful/navigation_helper.rb', line 26 def (nodes, level = 0, = {}, &block) return "" if nodes.empty? = [:menu_attributes] if .has_key?(:menu_attributes) .delete(:menu_attributes) haml_tag :ul, do nodes.each_with_index do |node, i| # set the attributes attributes = [:li_attributes].blank? ? {} : [:li_attributes].dup attributes[:class] ||= "" if i == 0 and .has_key?(:first) attributes[:class] << " #{[:first]}".squeeze(" ") elsif i == nodes.length - 1 and .has_key?(:last) attributes[:class] << " #{[:last]}".squeeze(" ") end haml_tag :li, attributes do children = nil if node.has_key?(:children) children = node[:children] elsif node.respond_to?(:children) children = node.children end if block_given? case block.arity when 1 yield node when 2 yield node, children when 3 yield node, children, level end end # pass it in again (children, level + 1, , &block) unless children.empty? end end end end |
#nested_set_menu(clazz, options = {}, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/insightful/navigation_helper.rb', line 11 def (clazz, = {}, &block) (simple_nested_set(clazz), ) do |node, children, level| if block_given? case block.arity when 1 yield node[:node] when 2 yield node[:node], children when 3 yield node[:node], children, level end end end end |
#simple_nested_set(clazz) ⇒ Object
Post for example
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/insightful/navigation_helper.rb', line 69 def simple_nested_set(clazz) # Post for example stack = [] # Post for example result = [] clazz.all(:order => "lft").each do |node| if stack.empty? stack.push({:node => node, :children => []}) result << stack.last next end if stack.last[:node].lft < node.lft && node.lft < stack.last[:node].rgt child = {:node => node, :children => []} stack.last[:children] << child unless node.leaf? # (node.rgt - node.lft == 1) stack.push(child) end if node.rgt + 1 == stack.last[:node].rgt stack.pop end else stack.pop end end result end |