Skip to content

Commit

Permalink
support ignoring fields at the model level (#7)
Browse files Browse the repository at this point in the history
* support ignoring fields at the model level

* add example for more custom exclude logic
  • Loading branch information
Tom Schlick authored Aug 23, 2019
1 parent 466d391 commit f9f7d3d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ php artisan make:model-auditlog "\App\User"

Replace `\App\User` with your own model name. Model / table options can be tweaked in the config file.

If you need to ignore specific fields on your model, extend the `getAuditLogIgnoredFields()` method and return an array of fields.

```php
public function getAuditLogIgnoredFields() : array
{
return ['posted_at'];
}
```

Using that functionality, you can add more custom logic around what should be logged. An example might be to not log the title changes of a post if the post has not been published yet.
```php
public function getAuditLogIgnoredFields() : array
{
if ($this->postHasBeenPublished()) {
return ['title'];
}

return [];
}
```

### Testing

``` bash
Expand Down
1 change: 1 addition & 0 deletions src/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function recordChanges(int $event_type, $model) : void

collect($changes)
->except(config('model-auditlog.global_ignored_fields'))
->except($model->getAuditLogIgnoredFields())
->except([
$model->getKeyName(), // Ignore the current model's primary key
'created_at',
Expand Down
10 changes: 10 additions & 0 deletions src/Traits/AuditLoggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public function getAuditLogTableName() : string
return $this->getTable() . config('model-auditlog.table_suffix');
}

/**
* Get fields that should be ignored from the auditlog for this model.
*
* @return array
*/
public function getAuditLogIgnoredFields() : array
{
return [];
}

/**
* Get the audit logs for this model.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Fakes/Models/IgnoredFieldsPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace OrisIntel\AuditLog\Tests\Fakes\Models;

use Illuminate\Database\Eloquent\Model;
use OrisIntel\AuditLog\Traits\AuditLoggable;

class IgnoredFieldsPost extends Model
{
use AuditLoggable;

protected $guarded = [];

protected $table = 'posts';

public function getAuditLogIgnoredFields() : array
{
return ['posted_at'];
}
}
12 changes: 12 additions & 0 deletions tests/Fakes/Models/IgnoredFieldsPostAuditLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace OrisIntel\AuditLog\Tests\Fakes\Models;

use OrisIntel\AuditLog\Models\BaseModel;

class IgnoredFieldsPostAuditLog extends BaseModel
{
public $timestamps = false;

public $table = 'posts_auditlog';
}
17 changes: 17 additions & 0 deletions tests/PostModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Collection;
use OrisIntel\AuditLog\EventType;
use OrisIntel\AuditLog\Tests\Fakes\Models\IgnoredFieldsPost;
use OrisIntel\AuditLog\Tests\Fakes\Models\NonSoftDeletePost;
use OrisIntel\AuditLog\Tests\Fakes\Models\Post;
use OrisIntel\AuditLog\Tests\Fakes\Models\PostAuditLog;
Expand Down Expand Up @@ -148,4 +149,20 @@ public function restoring_a_post_triggers_a_revision()
$this->assertEquals('deleted_at', $last->field_name);
$this->assertNull($last->field_value_new);
}

/** @test */
public function fields_can_be_ignored()
{
/** @var Post $post */
$post = IgnoredFieldsPost::create([
'title' => 'Test',
'posted_at' => '2019-04-05 12:00:00',
]);

$this->assertEquals(1, $post->auditLogs()->count());

$post->update(['posted_at' => now()]);

$this->assertEquals(1, $post->auditLogs()->count());
}
}

0 comments on commit f9f7d3d

Please sign in to comment.