Skip to content

Commit

Permalink
Action improvement (#416)
Browse files Browse the repository at this point in the history
* Prevent action handlers from being called when iterating over non-actionable fields

* Replacement fix
  • Loading branch information
dsindrilaru authored Aug 27, 2021
1 parent 4a2e4d5 commit a191c46
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Fields/Concerns/HasAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait HasAction
{
protected ?Action $actionHandler = null;
public ?Action $actionHandler = null;

public function action(Action $action): self
{
Expand Down
48 changes: 15 additions & 33 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,41 +649,23 @@ public function afterStore(Closure $callback)

public function invokeAfter(RestifyRequest $request, Model $model): void
{
if ($request->isStoreRequest()) {
$request->repository()
->collectFields($request)
->forStore($request, $request->repository())
->withActions($request, $this)
->authorizedStore($request)
->each(fn (Field $field) => $field->actionHandler->handle($request, $model));

if (is_callable($this->afterStoreCallback)) {
call_user_func(
$this->afterStoreCallback,
data_get($model, $this->attribute),
$model,
$request
);
}
if ($request->isStoreRequest() && is_callable($this->afterStoreCallback)) {
call_user_func(
$this->afterStoreCallback,
data_get($model, $this->attribute),
$model,
$request
);
}

if ($request->isUpdateRequest()) {
$request->repository()
->collectFields($request)
->forUpdate($request, $request->repository())
->withActions($request, $this)
->authorizedUpdate($request)
->each(fn (Field $field) => $field->actionHandler->handle($request, $model));

if (is_callable($this->afterUpdateCallback)) {
call_user_func(
$this->afterUpdateCallback,
$this->resolveAttribute($model, $this->attribute),
$this->valueBeforeUpdate,
$model,
$request
);
}
if ($request->isUpdateRequest() && is_callable($this->afterUpdateCallback)) {
call_user_func(
$this->afterUpdateCallback,
$this->resolveAttribute($model, $this->attribute),
$this->valueBeforeUpdate,
$model,
$request
);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,13 @@ public function store(RestifyRequest $request)
}

$fields->each(fn (Field $field) => $field->invokeAfter($request, $this->resource));

$request->repository()
->collectFields($request)
->forStore($request, $this)
->withActions($request, $this)
->authorizedStore($request)
->each(fn (Field $field) => $field->actionHandler->handle($request, $this->resource));
});

static::stored($this->resource, $request);
Expand Down Expand Up @@ -702,6 +709,13 @@ public function update(RestifyRequest $request, $repositoryId)
fn (Field $field) => $field->invokeAfter($request, $this->resource)
);

$request->repository()
->collectFields($request)
->forUpdate($request, $this)
->withActions($request, $this)
->authorizedUpdate($request)
->each(fn (Field $field) => $field->actionHandler->handle($request, $this->resource));

return $this->response()
->data($this->serializeForShow($request))
->success();
Expand Down

0 comments on commit a191c46

Please sign in to comment.