class Nutmeg::TagTreeHelper
Attributes
tree[RW]
Public Class Methods
new(tree, options = {})
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 4 def initialize(tree, options = {}) raise "No valid data type" unless tree.class == TagTree @options = options @tree = tree end
Public Instance Methods
append_path()
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 14 def append_path @options[:append_path] || "" end
node_html(node, active, leaf, content, extra_html = nil)
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 44 def node_html(node, active, leaf, content, extra_html = nil) "<li class='level_#{node.level}#{leaf ? ' leaf' : ''}#{active ? ' active' : ''}'> <a #{(leaf) ? "href=\'#{prepend_path}#{node_path(node)}#{append_path}\'" : ''}>#{content}</a>#{extra_html && active && leaf ? extra_html : ''}" end
node_path(node)
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 48 def node_path(node) ([""] + node.parentage.map{|p| p.content[:slug]}.reverse.reject{|n| n == "root"} + [node.content[:slug]]).join("/") end
prepend_path()
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 10 def prepend_path @options[:prepend_path] || "" end
print_html(tags_given, greedy = false, extra_html = nil, do_not_wrap = false)
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 22 def print_html(tags_given, greedy = false, extra_html = nil, do_not_wrap = false) [print_node(@tree.original, tags_given, greedy, extra_html, do_not_wrap)].join("") end
print_node(node, tags_given, greedy = false, extra_html = nil, do_not_wrap = false)
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 52 def print_node(node, tags_given, greedy = false, extra_html = nil, do_not_wrap = false) output = [] if render_node?(node,tags_given) output << node_html(node, (!@tree.get_paths(tags_given).first.nil? && @tree.get_paths(tags_given).first.map(&:name).include?(node.name) || (greedy == true && tags_given.include?(node.content[:slug]))), node.is_leaf?, title(node), extra_html) end if !traverse_node?(node, tags_given) output << "</li>" return output end if node.has_children? output << (do_not_wrap && node.is_root? ? "" : "<ul>") node.children.each do |child| output << print_node(child, tags_given, greedy,extra_html, do_not_wrap) end output << (do_not_wrap && node.is_root? ? "" : "</ul>") end if render_node?(node,tags_given) output << "</li>" end output end
print_relevant_html(tags_given, greedy = false, extra_html = nil, do_not_wrap = false)
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 26 def print_relevant_html(tags_given, greedy = false, extra_html = nil, do_not_wrap = false) html_nodes = [] intersectional_tags(tags_given).each do |node| html_nodes << node_html(node, false, true, title(node), extra_html) + "</li>" end return html_nodes.join end
render_node?(node, tags_given)
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 76 def render_node?(node, tags_given) return false if node.is_root? traverse_node?(node, tags_given) end
title(node)
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 18 def title(node) node.content[:name] || node.content[:slug] end
traverse_node?(node, tags_given)
click to toggle source
# File lib/nutmeg/tag_tree_helper.rb, line 81 def traverse_node?(node, tags_given) true end