Class: ExpressTemplates::Components::TreeFor

Inherits:
Container 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) do |role|

role.name

end “‘

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>

Instance Attribute Summary

Attributes inherited from Expander

#handlers, #locals, #stack, #template

Instance Method Summary collapse

Methods included from Capabilities::Parenting

included

Methods inherited from Base

inherited

Methods included from Capabilities::Iterating

included

Methods included from Capabilities::Wrapping

included

Methods included from Capabilities::Rendering

included

Methods included from Capabilities::Templating

included

Methods included from Macro

included

Methods inherited from Expander

#expand, #initialize, #initialize_expander, #method_missing, #process_children!, register_macros_for

Constructor Details

This class inherits a constructor from ExpressTemplates::Expander

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ExpressTemplates::Expander

Instance Method Details

#compileObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/express_templates/components/tree_for.rb', line 61

def compile
  collection = _variablize(@options[:id])
  member = @options[:id].to_s.singularize
  return 'ExpressTemplates::Components::TreeFor.render_in(self) {
  node_renderer = '+node_renderer.gsub(/node/, member)+'
  ExpressTemplates::Indenter.for(:tree) do |ws, wsnl|
    "#{ws}<ul id=\"roles\" class=\"roles tree\">" +
'+collection+'.map do |'+member+'|
  node_renderer.call('+member+', node_renderer)
end.join +
    "#{wsnl}</ul>\n"
  end
}'
end

#node_rendererObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/express_templates/components/tree_for.rb', line 40

def node_renderer
  return (-> (node, renderer) {
    ExpressTemplates::Indenter.for(:tree) do |ws, wsnl|
"#{wsnl}<li>"+
_yield +
if node.children.any?
  ExpressTemplates::Indenter.for(:tree) do |ws, wsnl|
    "#{wsnl}<ul>" +
      node.children.map do |child|
        renderer.call(child, renderer)
      end.join +
    "#{wsnl}</ul>"
  end +
  "#{wsnl}</li>"
else
  "</li>"
end
    end
  }).source.sub(/\W_yield\W/, compile_children.lstrip)
end