Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for shallow nested resources #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/Routing/ResourceBreadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,23 @@ protected function makeBreadcrumbForAction(string $action, stdClass $config): Ro
protected function getRouteNameForAction(string $action): string
{
$names = $this->options['names'] ?? [];
$action = $names[$action] ?? "{$this->name}.{$action}";


if ($this->isShallow() && in_array($action, ['show', 'edit'])) {
$action = $this->getShallowName().'.'.$action;
} else {
$action = $names[$action] ?? "{$this->name}.{$action}";
}

return trim(Route::mergeWithLastGroup(['as' => $action])['as'], '.');
}

protected function getParameterNamesForAction(string $action): array
{
$parameters = $this->getRouteGroupParameters();

$name = $this->isShallow() ? $this->getShallowName() : $this->name;

if (in_array($action, ['show', 'edit'])) {
$parameters[] = $this->options['parameters'][$this->name] ?? Str::singular($this->name);
$parameters[] = $this->options['parameters'][$name] ?? Str::singular($name);
}

return $parameters;
Expand All @@ -124,4 +130,14 @@ protected function getRouteGroupParameters(): array

return $matches[1] ?? [];
}

private function isShallow(): bool
{
return isset($this->options['shallow']) && $this->options['shallow'];
}

private function getShallowName(): string
{
return last(explode('.', $this->name));
}
}
48 changes: 46 additions & 2 deletions tests/ResourceRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Glhd\Gretel\Routing\ResourceBreadcrumbs;
use Glhd\Gretel\Tests\Models\Note;
use Glhd\Gretel\Tests\Models\User;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\PendingResourceRegistration;
Expand Down Expand Up @@ -333,8 +334,43 @@ public function test_grouped_resource_routes(bool $cache): void
['Create', '/movies/1/actors/create'],
);
}

protected function registerResourceRoute(bool $cache, Closure $setup): self

/**
* @dataProvider cachingProvider
*/
public function test_nested_shallow_resource(bool $cache): void
{
Route::middleware(SubstituteBindings::class)
->group(function() {
Route::resource('users', ResourceRoutesTestController::class)
->breadcrumbs([
'index' => 'Users',
'create' => 'New User',
'edit' => 'Edit',
]);

Route::resource('users.notes', NotesController::class)
->shallow()
->breadcrumbs(fn(ResourceBreadcrumbs $breadcrumbs) => $breadcrumbs
->show(fn(Note $note) => $note->note, 'users.index', fn(Note $note) => $note->user)
->edit('Edit', '.show', fn(Note $note) => $note->user)
);
});

$note = Note::factory()->create(['note' => 'some note']);

$this->setUpCache($cache);

$this->get('/notes/'.$note->id.'/edit');

$this->assertActiveBreadcrumbs(
['Users', '/users'],
['some note', '/notes/'.$note->id],
['Edit', '/notes/'.$note->id.'/edit'],
);
}

protected function registerResourceRoute(bool $cache, Closure $setup): self
{
Route::middleware(SubstituteBindings::class)
->group(function() use ($setup) {
Expand Down Expand Up @@ -393,3 +429,11 @@ public function edit(User $jazzy_dancer)
return $jazzy_dancer->name;
}
}

class NotesController
{
public function edit(Note $note)
{
return $note;
}
}