Skip to content

Commit

Permalink
lazychaser#81: moving node now makes model dirty before saving it
Browse files Browse the repository at this point in the history
  • Loading branch information
lazychaser committed Feb 22, 2016
1 parent efd1144 commit 33a1b2c
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* #82: Fixing tree now handles case when nodes pointing non-existing parent
* The number of missing parent is now returned when using `countErrors`
* #79: implemented scoping feature
* #81: moving node now makes model dirty before saving it

### 3.1.1

Expand Down
74 changes: 63 additions & 11 deletions src/NodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,15 @@ protected function actionAppendOrPrepend(self $parent, $prepend = false)
* Apply parent model.
*
* @param Model|null $value
*
* @return $this
*/
protected function setParent($value)
{
$this->attributes[$this->getParentIdName()] = $value
? $value->getKey()
: null;
$this->setParentId( $value ? $value->getKey() : null)
->setRelation('parent', $value);

$this->setRelation('parent', $value);
return $this;
}

/**
Expand Down Expand Up @@ -372,6 +373,10 @@ public function makeRoot()
*/
public function saveAsRoot()
{
if ($this->exists && $this->isRoot()) {
return true;
}

return $this->makeRoot()->save();
}

Expand Down Expand Up @@ -431,11 +436,10 @@ public function prependToNode(self $parent)
*/
public function appendOrPrependTo(self $parent, $prepend = false)
{
if ( ! $parent->exists) {
throw new LogicException('Cannot use non-existing node as a parent.');
}
$this->assertNodeExists($parent)
->assertNotDescendant($parent);

$this->setParent($parent);
$this->setParent($parent)->dirtyBounds();

return $this->setAction('appendOrPrepend', $parent, $prepend);
}
Expand Down Expand Up @@ -472,14 +476,14 @@ public function beforeNode(self $node)
*/
public function beforeOrAfterNode(self $node, $after = false)
{
if ( ! $node->exists) {
throw new LogicException('Cannot insert before/after a node that does not exists.');
}
$this->assertNodeExists($node)->assertNotDescendant($node);

if ( ! $this->isSiblingOf($node)) {
$this->setParent($node->getRelationValue('parent'));
}

$this->dirtyBounds();

return $this->setAction('beforeOrAfter', $node, $after);
}

Expand Down Expand Up @@ -1069,26 +1073,74 @@ public function getBounds()

/**
* @param $value
*
* @return $this
*/
public function setLft($value)
{
$this->attributes[$this->getLftName()] = $value;

return $this;
}

/**
* @param $value
*
* @return $this
*/
public function setRgt($value)
{
$this->attributes[$this->getRgtName()] = $value;

return $this;
}

/**
* @param $value
*
* @return $this
*/
public function setParentId($value)
{
$this->attributes[$this->getParentIdName()] = $value;

return $this;
}

/**
* @return $this
*/
protected function dirtyBounds()
{
return $this->setLft(null)->setRgt(null);
}

/**
* @param NodeTrait $node
*
* @return $this
*/
protected function assertNotDescendant(self $node)
{
if ($node == $this || $node->isDescendantOf($this)) {
throw new LogicException('Node must not be a descendant.');
}

return $this;
}

/**
* @param NodeTrait $node
*
* @return $this
*/
protected function assertNodeExists(self $node)
{
if ( ! $node->getLft() || ! $node->getRgt()) {
throw new LogicException('Node must exists.');
}

return $this;
}

}
4 changes: 1 addition & 3 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,10 @@ public function reversed()
/**
* Move a node to the new position.
*
* @param int $key
* @param mixed $key
* @param int $position
*
* @return int
*
* @throws \LogicException
*/
public function moveNode($key, $position)
{
Expand Down
23 changes: 22 additions & 1 deletion tests/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function testFailsToAppendIntoItself()
{
$node = $this->findCategory('notebooks');

$node->appendTo($node)->save();
$node->appendToNode($node)->save();
}

/**
Expand Down Expand Up @@ -677,6 +677,27 @@ public function testParentIdDirtiness()
$this->assertTrue($node->isDirty('parent_id'));
}

public function testIsDirtyMovement()
{
$node = $this->findCategory('apple');
$otherNode = $this->findCategory('samsung');

$this->assertFalse($node->isDirty());

$node->afterNode($otherNode);

$this->assertTrue($node->isDirty());

$node = $this->findCategory('apple');
$otherNode = $this->findCategory('samsung');

$this->assertFalse($node->isDirty());

$node->appendToNode($otherNode);

$this->assertTrue($node->isDirty());
}

public function testRootNodesMoving()
{
$node = $this->findCategory('store');
Expand Down
13 changes: 13 additions & 0 deletions tests/ScopedNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ public function testDepth()
$this->assertEquals(1, $result->first()->depth);
}

public function testSaveAsRoot()
{
$node = MenuItem::find(5);

$node->saveAsRoot();

$this->assertEquals(5, $node->getLft());

$node = MenuItem::find(3);

$this->assertEquals(1, $node->getLft());
}

public function testInsertion()
{
$node = MenuItem::create([ 'menu_id' => 1, 'parent_id' => 5 ]);
Expand Down

0 comments on commit 33a1b2c

Please sign in to comment.