diff --git a/src/Concerns/IsIgnorable.php b/src/Concerns/IsIgnorable.php new file mode 100644 index 0000000..a02a8ba --- /dev/null +++ b/src/Concerns/IsIgnorable.php @@ -0,0 +1,35 @@ +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; + } +} diff --git a/src/Concerns/LogsActivity.php b/src/Concerns/LogsActivity.php index 61e6171..7168265 100644 --- a/src/Concerns/LogsActivity.php +++ b/src/Concerns/LogsActivity.php @@ -11,6 +11,7 @@ trait LogsActivity { use DetectsChanges; use LogsRelatedActivity; + use IsIgnorable; /** * @var array @@ -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); } /** diff --git a/tests/Feature/Concerns/LogsActivityTest.php b/tests/Feature/Concerns/LogsActivityTest.php index 056612d..5a320ae 100644 --- a/tests/Feature/Concerns/LogsActivityTest.php +++ b/tests/Feature/Concerns/LogsActivityTest.php @@ -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); + } } diff --git a/tests/Models/Article.php b/tests/Models/Article.php index f38b55c..4a00b00 100644 --- a/tests/Models/Article.php +++ b/tests/Models/Article.php @@ -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 { @@ -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; + } } diff --git a/tests/migrations/0000_00_00_000000_create_articles_table.php b/tests/migrations/0000_00_00_000000_create_articles_table.php index d2c0347..8888482 100644 --- a/tests/migrations/0000_00_00_000000_create_articles_table.php +++ b/tests/migrations/0000_00_00_000000_create_articles_table.php @@ -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();