Skip to content

Commit

Permalink
conditional action log (#441)
Browse files Browse the repository at this point in the history
* fix: Fixing query matches. (#439)

* fix: Use only query params for matching.

* fix: Conditional action loggable.

* Fix styling

Co-authored-by: binaryk <[email protected]>
  • Loading branch information
binaryk and binaryk authored Nov 25, 2021
1 parent 96021a0 commit 837bf21
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Binaryk\LaravelRestify\Http\Requests\ActionRequest;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Models\ActionLog;
use Binaryk\LaravelRestify\Models\Concerns\HasActionLogs;
use Binaryk\LaravelRestify\Restify;
use Binaryk\LaravelRestify\Traits\AuthorizedToSee;
use Binaryk\LaravelRestify\Traits\Make;
Expand Down Expand Up @@ -82,8 +82,8 @@ public function uriKey()
/**
* Determine if the action is executable for the given request.
*
* @param Request $request
* @param Model $model
* @param Request $request
* @param Model $model
* @return bool
*/
public function authorizedToRun(Request $request, $model)
Expand All @@ -94,7 +94,7 @@ public function authorizedToRun(Request $request, $model)
/**
* Set the callback to be run to authorize running the action.
*
* @param Closure $callback
* @param Closure $callback
* @return $this
*/
public function canRun(Closure $callback)
Expand All @@ -107,8 +107,8 @@ public function canRun(Closure $callback)
/**
* Get the payload available on the action.
*
* @deprecated Use rules instead
* @return array
* @deprecated Use rules instead
*/
public function payload(): array
{
Expand All @@ -128,7 +128,7 @@ public function rules(): array
/**
* Make current action being standalone. No model query will be performed.
*
* @param bool $standalone
* @param bool $standalone
* @return self
*/
public function standalone(bool $standalone = true): self
Expand Down Expand Up @@ -167,7 +167,11 @@ public function handleRequest(ActionRequest $request)
Transaction::run(function () use ($models, $request, &$response) {
$response = $this->handle($request, $models);

$models->each(fn (Model $model) => ActionLog::forRepositoryAction($this, $model, $request->user())->save());
$models->each(function (Model $model) use ($request) {
if (in_array(HasActionLogs::class, class_uses_recursive($model), true)) {
Restify::actionLog()::forRepositoryAction($this, $model, $request->user())->save();
}
});
});
});
} else {
Expand All @@ -179,11 +183,13 @@ public function handleRequest(ActionRequest $request)
})->firstOrFail()
);

Restify::actionLog()::forRepositoryAction(
$this,
$model,
$request->user()
)->save();
if (in_array(HasActionLogs::class, class_uses_recursive($model), true)) {
Restify::actionLog()::forRepositoryAction(
$this,
$model,
$request->user()
)->save();
}
});
}

Expand Down

0 comments on commit 837bf21

Please sign in to comment.