Sha256: 3b21ac69e631f1c7d634bbbd82da6ca12feb6ed21c6e37b53446dba6d50801ae

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 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')
      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.1 lib/ancestry/treeview/instance_methods.rb