Skip to content

Commit

Permalink
Updated activity log with pivotable.
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemjohn committed Oct 26, 2022
1 parent 37855b8 commit deff622
Showing 1 changed file with 3 additions and 56 deletions.
59 changes: 3 additions & 56 deletions src/Litepie/Activities/Traits/LogsActivityWithPivots.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,12 @@ trait LogsActivityWithPivots
protected static function bootLogsActivityWithPivots()
{

static::eventsToBeRecorded()->each(function ($eventName) {
if ($eventName === 'updated') {
static::updating(function (Model $model) {
$oldValues = (new static())->setRawAttributes($model->getRawOriginal());
$model->oldAttributes = static::logChanges($oldValues);
});
}

static::$eventName(function (Model $model) use ($eventName) {
$model->activitylogOptions = $model->getActivitylogOptions();

if (! $model->shouldLogEvent($eventName)) {
return;
}

$changes = $model->attributeValuesToBeLogged($eventName);

$description = $model->getDescriptionForEvent($eventName);

$logName = $model->getLogNameToUse();

// Submitting empty description will cause place holder replacer to fail.
if ($description == '') {
return;
}

if ($model->isLogEmpty($changes) && ! $model->activitylogOptions->submitEmptyLogs) {
return;
}

// User can define a custom pipelines to mutate, add or remove from changes
// each pipe receives the event carrier bag with changes and the model in
// question every pipe should manipulate new and old attributes.
$event = app(Pipeline::class)
->send(new EventLogBag($eventName, $model, $changes, $model->activitylogOptions))
->through(static::$changesPipes)
->thenReturn();

// Actual logging
$logger = app(ActivityLogger::class)
->useLog($logName)
->event($eventName)
->performedOn($model)
->withProperties($event->changes);

if (method_exists($model, 'tapActivity')) {
$logger->tap([$model, 'tapActivity'], $eventName);
}

$logger->log($description);

// Reset log options so the model can be serialized.
$model->activitylogOptions = null;
});
});

collect(['pivotAttached', 'pivotDetached', 'pivotUpdated'])->each(function ($eventName) {
return static::$eventName(function (Model $model, $relationName, $pivotIds, $pivotIdsAttributes) use ($eventName) {
$logger = app(ActivityLogger::class)->useLog($model->getLogNameToUse($eventName));
$model->activitylogOptions = $model->getActivitylogOptions();
$logger = app(ActivityLogger::class)
->useLog($model->getLogNameToUse($eventName));

foreach ($pivotIds as $pivotId) {
$properties = [
Expand Down

0 comments on commit deff622

Please sign in to comment.