Skip to content

Commit

Permalink
Merge branch '1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
lazychaser committed Jun 5, 2014
2 parents 524cef4 + 6517384 commit 6748af4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5

Expand Down
5 changes: 3 additions & 2 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
### 1.0.1
* `Collection::toDictionary` is now obsolete. Use `Collection::groupBy`.
### 1.1.0
* `Collection::toDictionary` is now obsolete. Use `Collection::groupBy`.
* Laravel 4.2 is required
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
],

"require": {
"php": ">=5.3.0",
"illuminate/support": "~4.1.18",
"illuminate/database": "~4.1.18"
"php": ">=5.4.0",
"illuminate/support": "~4.2.1",
"illuminate/database": "~4.2.1"
},

"require-dev": {
Expand Down
49 changes: 41 additions & 8 deletions src/Kalnoy/Nestedset/Node.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Kalnoy\Nestedset;
<?php

namespace Kalnoy\Nestedset;

use \Illuminate\Database\Eloquent\Model as Eloquent;
use \Illuminate\Database\Query\Builder;
Expand Down Expand Up @@ -41,6 +43,27 @@ class Node extends Eloquent {
*/
const AFTER = 'after';

/**
* Whether model uses soft delete.
*
* @var bool
*
* @since 1.1
*/
static protected $softDelete;

/**
* {@inheritdoc}
*/
protected static function boot()
{
parent::boot();

$instance = new static;

static::$softDelete = method_exists($instance, 'withTrashed');
}

/**
* Get the root node.
*
Expand Down Expand Up @@ -300,7 +323,7 @@ protected function beforeOrAfter(Node $node, $dir)
*/
protected function checkTarget(Node $node)
{
if (!$node->exists || $node->isDirty(static::LFT))
if ( ! $node->exists || $node->isDirty(static::LFT))
{
throw new Exception("Target node is updated but not saved.");
}
Expand Down Expand Up @@ -361,19 +384,19 @@ public function fireModelEvent($event, $halt = true)
{
if ($this->exists)
{
if ($this->isDirty(static::LFT) && !$this->updateTree())
if ($this->isDirty(static::LFT) && ! $this->updateTree())
{
return false;
}
}
else
{
if (!isset($this->attributes[static::LFT]))
if ( ! isset($this->attributes[static::LFT]))
{
throw new Exception("Cannot save node until it is inserted.");
}

if (!$this->updateTree()) return false;
if ( ! $this->updateTree()) return false;
}
}

Expand All @@ -382,7 +405,7 @@ public function fireModelEvent($event, $halt = true)
throw new Exception("Cannot delete root node.");
}

if ($event === 'deleted' && !$this->softDelete) $this->deleteNode();
if ($event === 'deleted' && ! static::$softDelete) $this->deleteNode();

return parent::fireModelEvent($event, $halt);
}
Expand Down Expand Up @@ -591,6 +614,16 @@ protected function newBaseQueryBuilder()
return new QueryBuilder($conn, $grammar, $conn->getPostProcessor(), $this);
}

/**
* Get a new query including deleted nodes.
*
* @since 1.1
*/
protected function newQueryWithDeleted()
{
return static::$softDelete ? $this->withTrashed() : $this->newQuery();
}

/**
* Create a new NestedSet Collection instance.
*
Expand All @@ -610,7 +643,7 @@ public function newCollection(array $models = array())
*/
public function getNodeHeight()
{
if (!$this->exists) return 2;
if ( ! $this->exists) return 2;

return $this->attributes[static::RGT] - $this->attributes[static::LFT] + 1;
}
Expand All @@ -635,7 +668,7 @@ public function getDescendantCount()
*/
public function setParentIdAttribute($value)
{
if (!isset($this->attributes[static::PARENT_ID]) || $this->attributes[static::PARENT_ID] != $value)
if ( ! isset($this->attributes[static::PARENT_ID]) || $this->attributes[static::PARENT_ID] != $value)
{
$this->appendTo(static::findOrFail($value));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function assertTreeNotBroken($table = 'categories')
// Check if lft and rgt values are unique
$checks[] = "from $table c1, $table c2 where c1.id <> c2.id and ".
"(c1._lft=c2._lft or c1._rgt=c2._rgt or c1._lft=c2._rgt or c1._rgt=c2._lft)";

// Check if parent_id is set correctly
$checks[] = "from $table c, $table p, $table m where c.parent_id=p.id and m.id <> p.id and m.id <> c.id and ".
"(c._lft not between p._lft and p._rgt or c._lft between m._lft and m._rgt and m._lft between p._lft and p._rgt)";
Expand Down

0 comments on commit 6748af4

Please sign in to comment.