Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Concerns/IsIgnorable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Scrn\Journal\Concerns;

trait IsIgnorable
{
/**
* Checks whether current event needs to be ignored or not.
*
* @param string $event
*
* @return bool
*/
protected function ignore(string $event): bool
{
$updated_attributes = array_keys($this->getDirty());

foreach ($this->ignore_activities ?? [] as $event_rule => $ignored_attributes) {
if ($event_rule !== $event) {
continue;
}

$similarities = array_intersect($ignored_attributes, $updated_attributes);

// If all ignored attributes are not similar to all updated attributes.
if (count($similarities) !== count($updated_attributes)) {
continue;
}

return true;
}

return false;
}
}
9 changes: 4 additions & 5 deletions src/Concerns/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ trait LogsActivity
{
use DetectsChanges;
use LogsRelatedActivity;
use IsIgnorable;

/**
* @var array
Expand Down Expand Up @@ -162,13 +163,11 @@ public function getLoggedEvents(): array
*/
public function shouldLogEvent(string $event): bool
{
if (array_has($this->getDirty(), 'deleted_at')) {
if ($this->getDirty()['deleted_at'] === null) {
return false;
}
if (array_has($this->getDirty(), 'deleted_at') && $this->getDirty()['deleted_at'] === null) {
return false;
}

return true;
return ! $this->ignore($event);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Feature/Concerns/LogsActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,24 @@ public function it_logs_custom_model_events()
$this->assertEquals(null, $activity->old_data);
$this->assertEquals(null, $activity->new_data);
}

/** @test */
public function it_ignores_model_events()
{
$article = factory(Article::class)->create();

// User created, Article created
$this->assertCount(2, Activity::all());

$article->perish();

// Article perished, and Article also updated but was ignored
$this->assertCount(3, Activity::all());

$activity = Activity::all()->last();
$this->assertEquals($article->id, $activity->subject_id);
$this->assertEquals('perished', $activity->event);
$this->assertEquals(null, $activity->old_data);
$this->assertEquals(null, $activity->new_data);
}
}
18 changes: 16 additions & 2 deletions tests/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class Article extends Model
'content',
];

protected $observables = ['published'];
protected $observables = ['published', 'perished'];

protected $logged = ['published'];
protected $logged = ['published', 'perished'];

protected $ignore_activities = [
'updated' => [
'perished_at'
],
];

public function user(): BelongsTo
{
Expand All @@ -33,4 +39,12 @@ public function publish()
$this->fireModelEvent('published');
return $this;
}

public function perish()
{
$this->perished_at = now();
$this->save();
$this->fireModelEvent('perished');
return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function up()
$table->string('title');
$table->text('content');
$table->timestamp('published_at')->nullable();
$table->timestamp('perished_at')->nullable();
$table->unsignedInteger('user_id');
$table->timestamps();
$table->softDeletes();
Expand Down