Add support for "relation" with Tree models

- This makes the list slightly more efficient for small collections, less efficient for larger collections. If this becomes a problem in future we may need to look at a solution that grabs all the root nodes to start, then lazy loads any expanded nodes as secondary AJAX requests.
- Write tests for the Tree trait improvements
Fixes #1647
This commit is contained in:
Samuel Georges 2016-02-27 11:29:07 +11:00
parent 21f3ac93c1
commit 98eb4f8239
3 changed files with 74 additions and 16 deletions

View File

@ -493,15 +493,16 @@ class Lists extends WidgetBase
*/
protected function getRecords()
{
$model = $this->prepareModel();
if ($this->showTree) {
$records = $this->model->getAllRoot();
$records = $model->getNested();
}
elseif ($this->showPagination) {
$records = $model->paginate($this->recordsPerPage, $this->currentPageNumber);
}
else {
$model = $this->prepareModel();
$records = ($this->showPagination)
? $model->paginate($this->recordsPerPage, $this->currentPageNumber)
: $model->get();
$records = $model->get();
}
return $this->records = $records;

View File

@ -15,9 +15,27 @@ class NestedTreeModelTest extends PluginTestCase
$this->seedSampleTree();
}
public function testGetNested()
{
$items = CategoryNested::getNested();
// Eager loaded
$items->each(function($item) {
$this->assertTrue($item->relationLoaded('children'));
});
$this->assertEquals(2, $items->count());
}
public function testGetAllRoot()
{
$items = CategoryNested::make()->getAllRoot();
$items = CategoryNested::getAllRoot();
// Not eager loaded
$items->each(function($item) {
$this->assertFalse($item->relationLoaded('children'));
});
$this->assertEquals(2, $items->count());
}

View File

@ -15,31 +15,63 @@ class SimpleTreeModelTest extends PluginTestCase
$this->seedSampleTree();
}
public function testGetChildren()
public function testGetNested()
{
// Not eager loaded
$item = CategorySimple::first();
$this->assertEquals(6, $item->getChildren()->count());
$items = CategorySimple::getNested();
// Eager loaded
$item = CategorySimple::make()->getAllRoot()->first();
$this->assertEquals(6, $item->getChildren()->count());
$items->each(function($item) {
$this->assertTrue($item->relationLoaded('children'));
});
$this->assertEquals(3, $items->count());
}
public function testGetAllRoot()
{
$items = CategorySimple::make()->getAllRoot();
$items = CategorySimple::getAllRoot();
// Not eager loaded
$items->each(function($item) {
$this->assertFalse($item->relationLoaded('children'));
});
$this->assertEquals(3, $items->count());
}
public function testGetChildren()
{
// Not eager loaded
$item = CategorySimple::first();
$this->assertFalse($item->relationLoaded('children'));
$this->assertEquals(6, $item->getChildren()->count());
// Not eager loaded
$item = CategorySimple::getAllRoot()->first();
$this->assertFalse($item->relationLoaded('children'));
$this->assertEquals(6, $item->getChildren()->count());
// Eager loaded
$item = CategorySimple::getNested()->first();
$this->assertTrue($item->relationLoaded('children'));
$this->assertEquals(6, $item->getChildren()->count());
}
public function testGetChildCount()
{
// Not eager loaded
$item = CategorySimple::first();
$this->assertFalse($item->relationLoaded('children'));
$this->assertEquals(9, $item->getChildCount());
// Not eager loaded
$item = CategorySimple::getAllRoot()->first();
$this->assertFalse($item->relationLoaded('children'));
$this->assertEquals(9, $item->getChildCount());
// Eager loaded
$item = CategorySimple::make()->getAllRoot()->first();
$item = CategorySimple::getNested()->first();
$this->assertTrue($item->relationLoaded('children'));
$this->assertEquals(9, $item->getChildCount());
}
@ -47,10 +79,17 @@ class SimpleTreeModelTest extends PluginTestCase
{
// Not eager loaded
$item = CategorySimple::first();
$this->assertFalse($item->relationLoaded('children'));
$this->assertEquals(9, $item->getAllChildren()->count());
// Not eager loaded
$item = CategorySimple::getAllRoot()->first();
$this->assertFalse($item->relationLoaded('children'));
$this->assertEquals(9, $item->getAllChildren()->count());
// Eager loaded
$item = CategorySimple::make()->getAllRoot()->first();
$item = CategorySimple::getNested()->first();
$this->assertTrue($item->relationLoaded('children'));
$this->assertEquals(9, $item->getAllChildren()->count());
}