Class: ExpressTemplates::Components::Presenters::TreeFor

Inherits:
Configurable
  • Object
show all
Defined in:
lib/express_templates/components/tree_for.rb

Overview

Create an html table or ol (ordered list) for a model object representing a tree of similar objects.

The objects must respond to :children.

The block is passed a NodeBuilder which may accept field names.

Example:

“‘ruby tree_for(:roles) { |role|

role.name

} “‘

If the view has an @roles variable with a Role having children, this will turn into markup such as the following:

<ul id="roles" class="roles tree">
  <li>SuperAdmin
    <ul>
      <li>Admin
        <ul>
          <li>Publisher
            <ul>
               <li>Author</li>
            </ul>
          </li>
          <li>Auditor</li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

Constant Summary

Constants inherited from Base

Base::MAP

Instance Method Summary collapse

Methods included from Capabilities::Configurable

included

Methods inherited from Base

abstract_component, abstract_component?, before_build, #build, builder_method, builder_method_and_class, contains, descendants, has_attributes, inherited, #initialize, require_parent, required_parent, tag

Constructor Details

This class inherits a constructor from ExpressTemplates::Components::Base

Instance Method Details

#list_item(node) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/express_templates/components/tree_for.rb', line 71

def list_item(node)
  li {
    if @customize_block
      @customize_block.call(node)
    else
      text_node "#{node.name}#{"\n" if node.children.any?}"
    end
    if node.children.any?
      ul {
        list_items(node.children)
      }
    end
  }
end

#list_items(nodes) ⇒ Object



65
66
67
68
69
# File 'lib/express_templates/components/tree_for.rb', line 65

def list_items(nodes)
  nodes.each do |node|
    list_item(node)
  end
end

#root_nodeObject



57
58
59
60
61
62
63
# File 'lib/express_templates/components/tree_for.rb', line 57

def root_node
  if config[:root] && config[:root].respond_to?(:call)
    config[:root].call
  else
    send(config[:id])
  end
end