Module: ActsAsTree::TreeWalker
- Defined in:
- lib/acts_as_tree.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#walk_tree(options = {}, &block) ⇒ Object
Traverse the tree and call a block with the current node and current depth-level.
Class Method Details
.extended(mod) ⇒ Object
216 217 218 219 220 221 222 223 224 |
# File 'lib/acts_as_tree.rb', line 216 def self.extended(mod) mod.class_eval do def walk_tree( = {}, &block) algorithm = .fetch :algorithm, :dfs where = .fetch :where, {} self.class.send("walk_tree_#{algorithm}", where, self, &block) end end end |
Instance Method Details
#walk_tree(options = {}, &block) ⇒ Object
Traverse the tree and call a block with the current node and current depth-level.
options:
algorithm:
:dfs for depth-first search (default)
:bfs for breadth-first search
where: AR where statement to filter certain nodes
The given block sets two parameters:
first: The current node
second: The current depth-level within the tree
Example of acts_as_tree for model Page (ERB view): <% Page.walk_tree do |page, level| %>
<%= link_to "#{' '*level}#{page.name}", page_path(page) %><br />
<% end %>
There is also a walk_tree instance method that starts walking from the node it is called on.
210 211 212 213 214 |
# File 'lib/acts_as_tree.rb', line 210 def walk_tree( = {}, &block) algorithm = .fetch :algorithm, :dfs where = .fetch :where, {} send("walk_tree_#{algorithm}", where, &block) end |