Skip to content

Commit

Permalink
Merge pull request #1743 from thingasd/fix-getAncestorsSlug
Browse files Browse the repository at this point in the history
ancestors sort by position desc to ensure the slug is following the tree
  • Loading branch information
haringsrob authored Jul 20, 2022
2 parents 4699cce + d230210 commit 09efaee
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Models/Behaviors/HasNesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace A17\Twill\Models\Behaviors;

use Kalnoy\Nestedset\NodeTrait;
use A17\Twill\Models\NestedsetCollection;
use Kalnoy\Nestedset\NodeTrait;

trait HasNesting
{
Expand Down Expand Up @@ -38,7 +38,7 @@ public function getNestedSlugAttribute()
*/
public function getAncestorsSlug($locale = null)
{
return collect($this->ancestors ?? [])
return collect($this->ancestors->sortByDesc('position') ?? [])
->map(function ($i) use ($locale) { return $i->getSlug($locale); })
->implode('/');
}
Expand Down
54 changes: 53 additions & 1 deletion tests/integration/NestedModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace A17\Twill\Tests\Integration;

use App\Http\Controllers\Admin\NodeController;
use App\Models\Node;
use App\Repositories\NodeRepository;
use App\Http\Controllers\Admin\NodeController;

class NestedModuleTest extends TestCase
{
Expand Down Expand Up @@ -115,4 +115,56 @@ public function testNestedModuleBrowseParentsAndChildren()
// Then all items are returned
$this->assertEquals(6, count($result['data']));
}

public function testAncestorsSlugCreationOrder()
{
$repository = app(NodeRepository::class);
$childlvl2 = $repository->create(
[
'title' => 'child level 2',
'published' => true,
'position' => 2,
]
);

$childlvl1 = $repository->create(
[
'title' => 'child level 1',
'published' => true,
'position' => 3,
]
);

$childlvl0 = $repository->create(
[
'title' => 'parent',
'published' => true,
'position' => 4,
]
);

$data = [
[
'id' => $childlvl0->id,
'children' => [
[
'id' => $childlvl1->id,
'children' => [
[
'id' => $childlvl2->id,
'children' => [],
],
],
],
],
],
];

$this->postJson('/twill/nodes/reorder', ['ids' => $data])->assertOk();

$this->assertEquals(
'parent/child-level-1/child-level-2',
$childlvl2->refresh()->ancestorsSlug . '/' . $childlvl2->getSlug()
);
}
}
12 changes: 10 additions & 2 deletions tests/stubs/nested_module/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

namespace App\Models;

use A17\Twill\Models\Behaviors\HasPosition;
use A17\Twill\Models\Behaviors\HasNesting;
use A17\Twill\Models\Behaviors\HasPosition;
use A17\Twill\Models\Behaviors\HasSlug;
use A17\Twill\Models\Behaviors\Sortable;
use A17\Twill\Models\Model;
use Illuminate\Support\Str;

class Node extends Model implements Sortable
{
use HasPosition, HasNesting;
use HasPosition;
use HasNesting;

protected $fillable = [
'published',
'title',
'description',
'position',
];

public function getSlug()
{
return Str::slug($this->title);
}
}

0 comments on commit 09efaee

Please sign in to comment.