Sha256: a06bd153204463f7419668c0ac7d0bd56aa9f83756be93454f729e4c583cab6f

Contents?: true

Size: 2 KB

Versions: 4

Compression:

Stored size: 2 KB

Contents

module TheSortableTreeHelper
  # Publicated by MIT
  # Nested Set View Helper
  # Ilya Zykin, zykin-ilya@ya.ru, Russia, Ivanovo 2009-2012
  # github.com/the-teacher
  #-------------------------------------------------------------------------------------------------------

  # = sortable_tree @pages, :new_url => new_page_url, :max_levels => 5
  # = sortable_tree @products, :new_url => new_product_url, :path => 'products/the_sortable_tree'

  def define_class_of_elements_of tree
    return nil if tree.empty?
    tree.first.class.to_s.downcase
  end

  def sortable_tree(tree, opts= {})
    opts.merge!({
      :path  => opts[:path] || :the_sortable_tree,
      :klass => define_class_of_elements_of(tree),
      :title => opts[:title] || :title,
      :max_levels => opts[:max_levels] || 3
    })
    render :partial => "#{opts[:path]}/tree", :locals => { :tree => sortable_tree_builder(tree, opts), :opts => opts }
  end

  def sortable_tree_builder(tree, options= {})
    result = ''
    opts = {
      :id => :id,           # node id field
      :node => nil,         # node
      :root => false,       # is it root node?
      :level => 0           # recursion level
    }.merge!(options)

    root = opts[:root]
    node = opts[:node]

    unless node
      roots = tree.select{|elem| elem.parent_id.nil?}      
      roots.each do |root|
        _opts = opts.merge({:node => root, :root => true, :level => opts[:level].next})
        result << sortable_tree_builder(tree, _opts)
      end
    else
      children_res = ''
      children = tree.select{|elem| elem.parent_id == node.id}
      opts.merge!({:has_children => children.blank?})
      children.each do |elem|
        _opts = opts.merge({:node => elem, :root => false, :level => opts[:level].next})
        children_res << sortable_tree_builder(tree, _opts)
      end
      result << render(:partial => "#{opts[:path]}/node", :locals => {:opts => opts, :root => root, :node => node, :children => children_res})
    end
    raw result
  end# sortable_tree_builder
end# module

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
the_sortable_tree-1.5.0 app/helpers/the_sortable_tree_helper.rb
the_sortable_tree-1.4.8 app/helpers/the_sortable_tree_helper.rb
the_sortable_tree-1.4.7 app/helpers/the_sortable_tree_helper.rb
the_sortable_tree-1.4.6 app/helpers/the_sortable_tree_helper.rb