# Finding our current depth in a tree (acts\_as\_tree)

**URL:** <https://www.ruby-forum.com/t/finding-our-current-depth-in-a-tree-acts-as-tree/141560>\
**Category:** Rails\
**Created:** [June 27, 2008, 1:50am UTC](https://www.ruby-forum.com/t/finding-our-current-depth-in-a-tree-acts-as-tree/141560 "2008-06-27T01:50:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brendon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f9b8f/32.png) [@brendon](https://www.ruby-forum.com/u/brendon)\
**Post date:** [June 27, 2008, 1:50am UTC](https://www.ruby-forum.com/t/finding-our-current-depth-in-a-tree-acts-as-tree/141560/1 "2008-06-27T01:50:55Z")

</div>

Hi there, I’m building an indexbar that has two parameters:

- It’ll display the current item, its siblings and it’s parents  
siblings (recursively) to a height provided in variable.
- It’ll stop climbing if we reach a certain depth as defined by  
another variable.

An example:

```
* Home
* Products
      o Products Home
      o Product Category 1
      o Product Category 2
            + Category Home
            + Product 1
            + Product 2
                  # Overview
                  # Specifications
                  # Enquire
            + Product 3
            + Product 4
      o Product Category 3
      o Product Category 4
* Applications
* Services
* Links
* About Us
* Contact Us

```

The current page is Overview and the rest is built around it.

The problem I’m facing is that I’d like to top it from ever climbing  
higher than level 2 (That is the level that has the product  
categories), so the indexbar would look like:

```
      o Products Home
      o Product Category 1
      o Product Category 2
            + Category Home
            + Product 1
            + Product 2
                  # Overview
                  # Specifications
                  # Enquire
            + Product 3
            + Product 4
      o Product Category 3

```

I would like to be able to provide a variable (the number 2 in this  
case) and have my code stop when it detects that the current tree  
depth is equal to two.

I can think of many hacky ways to do this, but I’d like this check to  
be as inexpensive as possible. Does anyone have any suggestions?

Here’s the code:

def index\_bar\_range(tree, component\_instance\_id, bar\_parameters,  
rett=nil)  
# This indexbar will behave as follows:  
# It starts at the level of the current component instance and  
shows its siblings  
# It then shows its parents siblings surrounging it and its  
siblings (recursively)  
# It will stop when it gets to the max depth or when it gets to  
the level of the site  
# that is defined as the top level to display - or it hits home  
first.

```
return unless bar_parameters.length == 2
component_instance =

```

ComponentInstance.find\_current\_by\_id(component\_instance\_id)  
@depth ||= bar\_parameters[1].to\_i  
ret = “\n

”  
tree.each do |node|  
if node.parent\_id && node.parent\_id ==  
component\_instance.parent\_id && availability\_checks(node)  
ret += “\n\t\<li” + if @component\_instance && node.id ==  
@component\_instance.id then " class=“uber”" else ‘’ end + “\>”  
ret += link\_to h(node.name), {:action =\> ‘show’, :controller  
=\> ‘/’ + node.component.technical\_name, :id =\> node.instance.id }  
ret += rett if rett && node.id == component\_instance.id  
ret += “\n”  
end  
end  
ret += “
\n”  
@depth -= 1  
# Go upward and put all those parents in  
if component\_instance.parent\_id && @depth != 0 ############ This  
is where I’d put the check for the level  
index\_bar\_range(tree, component\_instance.parent\_id,  
bar\_parameters, ret)  
else  
@depth = nil  
return ret  
end  
end

def availability\_checks(node)  
node.isEnabledRecursive? && node.viewable?(user) &&  
node.parent.authenticated?(session, user) && !  
node.component.backend\_only && !(node.instance.respond\_to?(‘private’)  
&& node.instance.private)  
end

def sitetree  
@sitetree ||= ComponentInstance.find\_current(:all, :order =\>  
‘position’)  
end

This is all in a helper and we just call it from a view providing the  
parameters and the currently viewed item.id. I’m using acts\_as\_tree.

Cheers,

Brendon

---

<div class="post-metadata">

**Author:** ![brendon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5f9b8f/32.png) [@brendon](https://www.ruby-forum.com/u/brendon)\
**Post date:** [June 27, 2008, 1:57am UTC](https://www.ruby-forum.com/t/finding-our-current-depth-in-a-tree-acts-as-tree/141560/2 "2008-06-27T01:57:57Z")

</div>

Here’s a paste of the code above:

[http://pastie.org/223022](http://pastie.org/223022)
