Skip to content

Commit

Permalink
overridable templates
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Dec 31, 2023
1 parent c59926e commit b0b2813
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 23 deletions.
8 changes: 7 additions & 1 deletion src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public function paginate(Request $request, Model $model): LengthAwarePaginator
->with($this->with)
->withCount($this->withCount)
->latest()
->paginate($request->input($this->getPerPageKey(), $this->isTurboRequest($request) ? 5 : $relation->getRelated()->getPerPage()))
->paginate($request->input($this->getPerPageKey(), $this->isTurboFrameRequest($request) ? 5 : $relation->getRelated()->getPerPage()))
->withQueryString();
}

Expand Down Expand Up @@ -564,6 +564,8 @@ public function registerRoutes(Request $request, Router $router): void
{
$this->__registerRoutes($request, $router);

// Gate::allowIf($field->authorized($request, $model));

$router->prefix($this->getUriKey())->group(function (Router $router) use ($request): void {
$this->resolveActions($request)->registerRoutes($request, $router);

Expand Down Expand Up @@ -660,6 +662,7 @@ public function toSubResource(Request $request, Model $model): array
public function toIndex(Request $request, Model $model): array
{
return array_merge($this->toSubResource($request, $model), [
'template' => $this->isTurboFrameRequest($request) ? 'root::resources.relation' : 'root::resources.index',
'title' => $this->label,
'model' => $this->getRelation($model)->make(),
'modelName' => $this->getRelatedName(),
Expand Down Expand Up @@ -691,6 +694,7 @@ public function toIndex(Request $request, Model $model): array
public function toCreate(Request $request, Model $model): array
{
return array_merge($this->toSubResource($request, $model), [
'template' => 'root::resources.form',
'title' => __('Create :model', ['model' => $this->getRelatedName()]),
'model' => $related = $this->getRelation($model)->make(),
'action' => $this->modelUrl($model),
Expand All @@ -709,6 +713,7 @@ public function toCreate(Request $request, Model $model): array
public function toShow(Request $request, Model $model, Model $related): array
{
return array_merge($this->toSubResource($request, $model), [
'template' => 'root::resources.show',
'title' => $this->resolveDisplay($related),
'model' => $related,
'action' => $this->relatedUrl($model, $related),
Expand All @@ -730,6 +735,7 @@ public function toShow(Request $request, Model $model, Model $related): array
public function toEdit(Request $request, Model $model, Model $related): array
{
return array_merge($this->toSubResource($request, $model), [
'template' => 'root::resources.form',
'title' => __('Edit :model', ['model' => $this->resolveDisplay($related)]),
'model' => $related,
'action' => $this->relatedUrl($model, $related),
Expand Down
24 changes: 15 additions & 9 deletions src/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ public function index(Request $request, Model $model): Response
{
$field = $request->route('field');

// Gate::allowIf($field->authorized($request, $model));
// if ($field->getPolicy()) {
// $this->authorize('viewAny', $field->getModel());
// }

$data = $field->toIndex($request, $model);

return ResponseFactory::view(
$request->hasHeader('Turbo-Frame') ? 'root::resources.relation' : 'root::resources.index',
$field->toIndex($request, $model)
$data['template'], $data
);
}

Expand All @@ -35,9 +38,10 @@ public function create(Request $request, Model $model): Response
{
$field = $request->route('field');

$data = $field->toCreate($request, $model);

return ResponseFactory::view(
'root::resources.form',
$field->toCreate($request, $model)
$data['template'], $data
);
}

Expand All @@ -63,9 +67,10 @@ public function show(Request $request, Model $model, Model $related): Response
{
$field = $request->route('field');

$data = $field->toShow($request, $model, $related);

return ResponseFactory::view(
'root::resources.show',
$field->toShow($request, $model, $related)
$data['template'], $data
);
}

Expand All @@ -76,9 +81,10 @@ public function edit(Request $request, Model $model, Model $related): Response
{
$field = $request->route('field');

$data = $field->toEdit($request, $model, $related);

return ResponseFactory::view(
'root::resources.form',
$field->toEdit($request, $model, $related)
$data['template'], $data
);
}

Expand Down
20 changes: 12 additions & 8 deletions src/Http/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public function index(Request $request, Resource $resource): Response
$this->authorize('viewAny', $resource->getModel());
}

$data = $resource->toIndex($request);

return ResponseFactory::view(
'root::resources.index',
$resource->toIndex($request)
$data['template'], $data
);
}

Expand All @@ -47,9 +48,10 @@ public function create(Request $request, Resource $resource): Response
$this->authorize('create', $resource->getModel());
}

$data = $resource->toCreate($request);

return ResponseFactory::view(
'root::resources.form',
$resource->toCreate($request)
$data['template'], $data
);
}

Expand Down Expand Up @@ -79,9 +81,10 @@ public function show(Request $request, Resource $resource, Model $model): Respon
$this->authorize('view', $model);
}

$data = $resource->toShow($request, $model);

return ResponseFactory::view(
'root::resources.show',
$resource->toShow($request, $model)
$data['template'], $data
);
}

Expand All @@ -94,9 +97,10 @@ public function edit(Request $request, Resource $resource, Model $model): Respon
$this->authorize('update', $model);
}

$data = $resource->toEdit($request, $model);

return ResponseFactory::view(
'root::resources.form',
$resource->toEdit($request, $model)
$data['template'], $data
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ public function toArray(): array
public function toIndex(Request $request): array
{
return array_merge($this->toArray(), [
'template' => 'root::resources.index',
'title' => $this->getName(),
'actions' => $this->resolveActions($request)
->authorized($request, $this->getModelInstance())
Expand Down Expand Up @@ -457,6 +458,7 @@ public function toIndex(Request $request): array
public function toCreate(Request $request): array
{
return array_merge($this->toArray(), [
'template' => 'root::resources.form',
'title' => __('Create :resource', ['resource' => $this->getModelName()]),
'model' => $model = $this->getModelInstance(),
'action' => $this->getUri(),
Expand All @@ -475,6 +477,7 @@ public function toCreate(Request $request): array
public function toShow(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'template' => 'root::resources.show',
'title' => sprintf('%s: %s', $this->getModelName(), $this->modelTitle($model)),
'model' => $model,
'action' => $this->modelUrl($model),
Expand Down Expand Up @@ -508,6 +511,7 @@ public function toShow(Request $request, Model $model): array
public function toEdit(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'template' => 'root::resources.form',
'title' => __('Edit :resource: :model', ['resource' => $this->getModelName(), 'model' => $this->modelTitle($model)]),
'model' => $model,
'action' => $this->modelUrl($model),
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/InteractsWithTurbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
trait InteractsWithTurbo
{
/**
* Determine if the request is Turbo request.
* Determine if the request is Turbo Frame request.
*/
public function isTurboRequest(Request $request): bool
public function isTurboFrameRequest(Request $request): bool
{
return $request->hasHeader('Turbo-Frame');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function data(Request $request): array
{
return array_merge(parent::data($request), [
'ranges' => $this->ranges(),
'data' => ! $this->async || $this->isTurboRequest($request) ? $this->calculate($request) : [],
'data' => ! $this->async || $this->isTurboFrameRequest($request) ? $this->calculate($request) : [],
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/Trend.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public function data(Request $request): array
{
return array_replace_recursive([
'data' => [
'chart' => $this->isTurboRequest($request) ? $this->config : [],
'chart' => $this->isTurboFrameRequest($request) ? $this->config : [],
'current' => null,
],
], parent::data($request));
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getUriKey(): string
public function data(Request $request): array
{
return array_merge($this->toArray(), [
'isTurbo' => $this->isTurboRequest($request),
'isTurbo' => $this->isTurboFrameRequest($request),
]);
}

Expand Down

0 comments on commit b0b2813

Please sign in to comment.