Sha256: 8a7469edf328cc5508fffea26c288d264ddf198759a3334767b9c2b9882ce710
Contents?: true
Size: 1.25 KB
Versions: 1
Compression:
Stored size: 1.25 KB
Contents
module Ancestry module InstanceMethods def tree(params = {}) return '' unless self subtree = (params[:to_depth].present? ? self.subtree(to_depth: params[:to_depth]) : self.subtree).order('path ASC') if params[:conditions].is_a?(Array) || params[:conditions].is_a?(Hash) subtree = subtree.where(params[:conditions]) end html = '<div class="ancestry-treeview">' open_ul = 0 prev_depth = self.depth - 1 subtree.each do |node| curr_depth = node.depth diff = curr_depth - prev_depth if curr_depth > prev_depth html += '<ul><li style="list-style:none">' * (diff - 1).abs html += node.is_childless? ? '<ul><li class="leaf">' : '<ul><li>' open_ul += diff elsif curr_depth == prev_depth html += node.is_childless? ? '</li><li class="leaf">' : '</li><li>' elsif curr_depth < prev_depth html += '</li></ul>' * diff.abs html += node.is_childless? ? '</li><li class="leaf">' : '</li><li>' open_ul -= diff end prev_depth = curr_depth html += block_given? ? yield(node) : "<a href=#>#{node.id}</a>" end html += '</li></ul>' * open_ul.abs html += '</div>' html end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ancestry-treeview-0.0.2 | lib/ancestry/treeview/instance_methods.rb |