Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Dec 1, 2021
1 parent 2fdf288 commit 0cb88a7
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/Pages/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public static function getSlug(): string
return static::$slug ?? Str::kebab(static::$title ?? class_basename(static::class));
}

public static function getUrl(): string
public static function getUrl(array $parameters = [], bool $absolute = true): string
{
return route(static::getRouteName());
return route(static::getRouteName(), $parameters, $absolute);
}

protected function notify(string $status, string $message): void
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/Pages/Concerns/HasRecordBreadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ protected function getBreadcrumbs(): array
];

if ($resource::hasRecordTitle()) {
if ($resource::canView($this->record)) {
if ($resource::canEdit($this->record)) {
$breadcrumbs[
$resource::getUrl('view', ['record' => $this->record])
$resource::getUrl('edit', ['record' => $this->record])
] = $this->getRecordTitle();
} elseif ($resource::canEdit($this->record)) {
} elseif ($resource::canView($this->record)) {
$breadcrumbs[
$resource::getUrl('edit', ['record' => $this->record])
$resource::getUrl('view', ['record' => $this->record])
] = $this->getRecordTitle();
} else {
$breadcrumbs[] = $this->getRecordTitle();
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/Pages/CreateRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ protected function getRedirectUrl(): ?string
{
$resource = static::getResource();

if ($resource::canView($this->record)) {
return $resource::getUrl('view', ['record' => $this->record]);
}

if ($resource::canEdit($this->record)) {
return $resource::getUrl('edit', ['record' => $this->record]);
}

if ($resource::canView($this->record)) {
return $resource::getUrl('view', ['record' => $this->record]);
}

return null;
}
}
12 changes: 5 additions & 7 deletions src/Resources/Pages/EditRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ public function delete(): void
{
abort_unless(static::getResource()::canDelete($this->record), 403);

$this->callHook('beforeDelete');

$this->record->delete();

$this->callHook('afterDelete');

$this->redirect(static::getResource()::getUrl('index'));
}

Expand Down Expand Up @@ -146,12 +150,6 @@ protected function getForms(): array

protected function getRedirectUrl(): ?string
{
$resource = static::getResource();

if (! $resource::canView($this->record)) {
return null;
}

return $resource::getUrl('view', ['record' => $this->record]);
return null;
}
}
9 changes: 4 additions & 5 deletions src/Resources/Pages/ListRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ protected function getResourceTable(): Table

$resource = static::getResource();

if ($resource::hasPage('view')) {
$table->actions([$this->getViewLinkTableAction()]);
} elseif ($resource::hasPage('edit')) {
$table->actions([$this->getEditLinkTableAction()]);
}
$table->actions(array_merge(
($resource::hasPage('view') ? [$this->getViewLinkTableAction()] : []),
($resource::hasPage('edit') ? [$this->getEditLinkTableAction()] : []),
));

if ($resource::canDeleteAny()) {
$table->bulkActions([$this->getDeleteTableBulkAction()]);
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/RelationManagers/RelationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ protected function callHook(string $hook): void

protected function can(string $action, ?Model $record = null): bool
{
$policy = Gate::getPolicyFor($this->getRelatedModel());
$policy = Gate::getPolicyFor($model = $this->getRelatedModel());

if ($policy === null || ! method_exists($policy, $action)) {
if ($policy === null || (! method_exists($policy, $action))) {
return true;
}

return Gate::check($action, $record);
return Gate::check($action, $record ?? $model);
}

protected function canAccess(): bool
Expand Down
14 changes: 7 additions & 7 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public static function resolveRecordRouteBinding($key): ?Model

public static function can(string $action, ?Model $record = null): bool
{
$policy = Gate::getPolicyFor(static::getModel());
$policy = Gate::getPolicyFor($model = static::getModel());

if ($policy === null || ! method_exists($policy, $action)) {
if ($policy === null || (! method_exists($policy, $action))) {
return true;
}

return Gate::check($action, $record);
return Gate::check($action, $record ?? $model);
}

public static function canAccess(): bool
Expand Down Expand Up @@ -152,14 +152,14 @@ public static function getGlobalSearchResultTitle(Model $record): string

public static function getGlobalSearchResultUrl(Model $record): ?string
{
if (static::canView($record)) {
return static::getUrl('view', ['record' => $record]);
}

if (static::canEdit($record)) {
return static::getUrl('edit', ['record' => $record]);
}

if (static::canView($record)) {
return static::getUrl('view', ['record' => $record]);
}

return null;
}

Expand Down

0 comments on commit 0cb88a7

Please sign in to comment.