Skip to content
Permalink
Browse files

Merge pull request #514 from akoSalman/master

Control logging when there is no changes
  • Loading branch information...
Gummibeer committed Apr 15, 2019
2 parents 3d7cb41 + 2793d24 commit 6e827c09846e5d722252baca73b199a3fdef9001
Showing with 39 additions and 1 deletion.
  1. +17 −1 src/Traits/LogsActivity.php
  2. +22 −0 tests/LogsActivityTest.php
@@ -32,10 +32,16 @@ protected static function bootLogsActivity()
return;
}
$attrs = $model->attributeValuesToBeLogged($eventName);
if ($model->isLogEmpty($attrs) && ! $model->shouldSubmitEmptyLogs()) {
return;
}
$logger = app(ActivityLogger::class)
->useLog($logName)
->performedOn($model)
->withProperties($model->attributeValuesToBeLogged($eventName));
->withProperties($attrs);
if (method_exists($model, 'tapActivity')) {
$logger->tap([$model, 'tapActivity'], $eventName);
@@ -46,6 +52,16 @@ protected static function bootLogsActivity()
});
}
public function shouldSubmitEmptyLogs(): bool
{
return ! isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs;
}
public function isLogEmpty($attrs): bool
{
return empty($attrs['attributes'] ?? []) && empty($attrs['old'] ?? []);
}
public function disableLogging()
{
$this->enableLoggingModelsEvents = false;
@@ -330,6 +330,28 @@ public function tapActivity(Activity $activity, string $eventName)
$this->assertEquals(Carbon::yesterday()->startOfDay()->format('Y-m-d H:i:s'), $firstActivity->created_at->format('Y-m-d H:i:s'));
}
/** @test */
public function it_will_not_submit_log_when_there_is_no_changes()
{
$model = new class() extends Article {
use LogsActivity;
protected static $submitEmptyLogs = false;
protected static $logAttributes = ['text'];
protected static $logOnlyDirty = true;
};
$entity = new $model(['text' => 'test']);
$entity->save();
$this->assertCount(1, Activity::all());
$entity->name = 'my name';
$entity->save();
$this->assertCount(1, Activity::all());
}
public function loginWithFakeUser()
{
$user = new $this->user();

0 comments on commit 6e827c0

Please sign in to comment.
You can’t perform that action at this time.