Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 22, 2023
1 parent fd50e5d commit d8ad977
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
39 changes: 27 additions & 12 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public function mapRelated(Request $request, Model $model, Model $related): arra
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $related)
->visible('relation.index')
->visible('index')
->mapToDisplay($request, $related),
];
}
Expand All @@ -420,6 +420,21 @@ public function relatedUrl(Model $model, Model $related): string
return sprintf('%s/%s', $this->modelUrl($model), $related->getKey());
}

/**
* Handle the request.
*/
public function handleFormRequest(Request $request, Model $model): void
{
$this->validateFormRequest($request, $model);

$this->resolveFields($request)
->authorized($request, $model)
->visible($request->isMethod('POST') ? 'create' : 'update')
->persist($request, $model);

$model->save();
}

/**
* Resolve the resource model for a bound value.
*/
Expand Down Expand Up @@ -488,7 +503,7 @@ public function toInput(Request $request, Model $model): array
}

/**
* Get the sub resource representation of the relation.
* Get the sub resource representation of the
*/
public function toSubResource(Request $request, Model $model): array
{
Expand All @@ -499,15 +514,15 @@ public function toSubResource(Request $request, Model $model): array
}

/**
* Get the index representation of the relation.
* Get the index representation of the
*/
public function toIndex(Request $request, Model $model): array
{
return array_merge($this->toSubResource($request, $model), [
'title' => $this->label,
'actions' => $this->resolveActions($request)
->authorized($request, $model)
->visible('relation.index')
->visible('index')
->mapToForms($request, $model),
'data' => $this->paginate($request, $model)->through(function (Model $related) use ($request, $model): array {
return $this->mapRelated($request, $model, $related);
Expand Down Expand Up @@ -537,46 +552,46 @@ public function toCreate(Request $request, Model $model): array
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $related)
->visible('relation.create')
->visible('create')
->mapToInputs($request, $related),
]);
}

/**
* Get the edit representation of the relation.
* Get the edit representation of the
*/
public function toShow(Request $request, Model $model, Model $related): array
{
return array_merge($this->toSubResource($request, $model), [
'title' => sprintf('%s #%s', $this->getRelatedName(), $related->getKey()),
'title' => $this->resolveDisplay($related),
'model' => $related,
'action' => $this->relatedUrl($model, $related),
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $related)
->visible('relation.show')
->visible('show')
->mapToDisplay($request, $related),
'actions' => $this->resolveActions($request)
->authorized($request, $related)
->visible('relation.show')
->visible('show')
->mapToForms($request, $related),
]);
}

/**
* Get the edit representation of the relation.
* Get the edit representation of the
*/
public function toEdit(Request $request, Model $model, Model $related): array
{
return array_merge($this->toSubResource($request, $model), [
'title' => __('Edit :model', ['model' => sprintf('%s #%s', $this->getRelatedName(), $related->getKey())]),
'title' => __('Edit :model', ['model' => $this->resolveDisplay($related)]),
'model' => $related,
'action' => $this->relatedUrl($model, $related),
'method' => 'PATCH',
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $related)
->visible('relation.update')
->visible('update')
->mapToInputs($request, $related),
]);
}
Expand Down
14 changes: 11 additions & 3 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ public function modelUrl(Model $model): string
return sprintf('%s/%s', $this->getUri(), $model->exists ? $model->getKey() : '');
}

/**
* Get the title for the model.
*/
public function modelTitle(Model $model): string
{
return sprintf('#%s', $model->getKey());
}

/**
* Define the filters for the object.
*/
Expand Down Expand Up @@ -313,7 +321,7 @@ public function handleFormRequest(Request $request, Model $model): void

$this->resolveFields($request)
->authorized($request, $model)
->visible($request->method() === 'POST' ? 'create' : 'update')
->visible($request->isMethod('POST') ? 'create' : 'update')
->persist($request, $model);

$model->save();
Expand Down Expand Up @@ -403,7 +411,7 @@ public function toCreate(Request $request): array
public function toShow(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'title' => sprintf('%s #%s', $this->getModelName(), $model->getKey()),
'title' => sprintf('%s %s', $this->getModelName(), $this->modelTitle($model)),
'model' => $model,
'action' => $this->modelUrl($model),
'fields' => $this->resolveFields($request)
Expand Down Expand Up @@ -434,7 +442,7 @@ public function toShow(Request $request, Model $model): array
public function toEdit(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'title' => __('Edit :model', ['model' => sprintf('%s #%s', $this->getModelName(), $model->getKey())]),
'title' => __('Edit :model', ['model' => sprintf('%s %s', $this->modelTitle($model))]),
'model' => $model,
'action' => $this->modelUrl($model),
'method' => 'PATCH',
Expand Down

0 comments on commit d8ad977

Please sign in to comment.