Skip to content

Commit

Permalink
Add immediateDescendants/getImmediateDescendants query scope and getter.
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepat committed Jul 3, 2013
1 parent 7f54302 commit 3a36f53
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ remember to call `get()` or `first()`.
* `leaves()`: Instance scope targeting all of its nested children which do not have children.
* `descendantsAndSelf()`: Scope targeting itself and all of its nested children.
* `descendants()`: Set of all children & nested children.
* `immediateDescendants()`: Set of all children nodes (non-recursive).

Second, as **methods** which return actual `Baum\Node` instances.

Expand All @@ -379,6 +380,7 @@ Second, as **methods** which return actual `Baum\Node` instances.
* `getLeaves()`: Return all of its nested children which do not have children.
* `getDescendantsAndSelf()`: Retrieve all nested children and self.
* `getDescendants()`: Retrieve all of its children & nested children.
* `getImmediateDescendants()`: Retrieve all of its children nodes (non-recursive).

Here's a simple example for iterating a node's descendants (provided a name
attribute is available):
Expand Down
19 changes: 19 additions & 0 deletions src/Baum/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,25 @@ public function getDescendants($columns = array('*')) {
return $this->descendants()->get($columns);
}

/**
* Set of "immediate" descendants (aka children), alias for the children relation.
*
* @return \Illuminate\Database\Query\Builder
*/
public function immediateDescendants() {
return $this->children();
}

/**
* Retrive all of its "immediate" descendants.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getImmediateDescendants($columns = array('*')) {
return $this->children()->get($columns);
}

/**
* Returns the level of this node in the tree.
* Root level is 0.
Expand Down
10 changes: 10 additions & 0 deletions tests/BaumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ public function testChildrenRespectDefaultOrdering() {
$this->assertEquals($children[2], $this->categories('Child 3'));
}

public function testGetImmediateDescendants() {
$expected = array($this->categories('Child 1'), $this->categories('Child 2'), $this->categories('Child 3'));

$this->assertEquals($expected, $this->categories('Root 1')->getImmediateDescendants()->all());

$this->assertEquals(array($this->categories('Child 2.1')), $this->categories('Child 2')->getImmediateDescendants()->all());

$this->assertEmpty($this->categories('Root 2')->getImmediateDescendants()->all());
}

public function testIsSelfOrAncestorOf() {
$this->assertTrue($this->categories('Root 1')->isSelfOrAncestorOf($this->categories('Child 1')));
$this->assertTrue($this->categories('Root 1')->isSelfOrAncestorOf($this->categories('Child 2.1')));
Expand Down

0 comments on commit 3a36f53

Please sign in to comment.