Skip to content

Feat: Ability to disable auditable #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions src/AuditableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
/**
* @property Model $creator
* @property Model $updater
* @property bool $auditing
*/
trait AuditableTrait
{
protected static bool $auditing = true;

/**
* Boot the audit trait for a model.
*/
Expand All @@ -20,6 +23,29 @@ public static function bootAuditableTrait(): void
static::observe(new AuditableTraitObserver);
}

/**
* Disable auditing.
*/
public static function withoutAudits(callable $callback)
{
$previousState = static::$auditing;
static::$auditing = false;

try {
return $callback();
} finally {
static::$auditing = $previousState;
}
}

/**
* Check is auditing is enabled.
*/
public function isAuditable(): bool
{
return static::$auditing;
}

/**
* Get user model who created the record.
*/
Expand Down
8 changes: 4 additions & 4 deletions src/AuditableTraitObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
*/
public function creating(Model $model): void
{
if (method_exists($model, 'getCreatedByColumn')) {
if (method_exists($model, 'getCreatedByColumn') && $model->isAuditable()) {

Check failure on line 14 in src/AuditableTraitObserver.php

View workflow job for this annotation

GitHub Actions / Source Code

Call to an undefined method Illuminate\Database\Eloquent\Model::isAuditable().
$createdBy = $model->getCreatedByColumn();

if (! $model->$createdBy) {
$model->$createdBy = $this->getAuthenticatedUserId();
}
}

if (method_exists($model, 'getUpdatedByColumn')) {
if (method_exists($model, 'getUpdatedByColumn') && $model->isAuditable()) {

Check failure on line 22 in src/AuditableTraitObserver.php

View workflow job for this annotation

GitHub Actions / Source Code

Call to an undefined method Illuminate\Database\Eloquent\Model::isAuditable().
$updatedBy = $model->getUpdatedByColumn();

if (! $model->$updatedBy) {
Expand All @@ -41,7 +41,7 @@
*/
public function updating(Model $model): void
{
if (method_exists($model, 'getUpdatedByColumn')) {
if (method_exists($model, 'getUpdatedByColumn') && $model->isAuditable()) {

Check failure on line 44 in src/AuditableTraitObserver.php

View workflow job for this annotation

GitHub Actions / Source Code

Call to an undefined method Illuminate\Database\Eloquent\Model::isAuditable().
$updatedBy = $model->getUpdatedByColumn();

if (! $model->isDirty($updatedBy)) {
Expand All @@ -55,7 +55,7 @@
*/
public function saved(Model $model): void
{
if (method_exists($model, 'getUpdatedByColumn')) {
if (method_exists($model, 'getUpdatedByColumn') && $model->isAuditable()) {

Check failure on line 58 in src/AuditableTraitObserver.php

View workflow job for this annotation

GitHub Actions / Source Code

Call to an undefined method Illuminate\Database\Eloquent\Model::isAuditable().
$updatedBy = $model->getUpdatedByColumn();

if ($this->getAuthenticatedUserId() && $this->getAuthenticatedUserId() != $model->$updatedBy && $model->isDirty()) {
Expand Down
4 changes: 2 additions & 2 deletions src/AuditableWithDeletesTraitObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public function deleting(Model $model): void
{
if (method_exists($model, 'getDeletedByColumn')) {
if (method_exists($model, 'getDeletedByColumn') && $model->isAuditable()) {

Check failure on line 14 in src/AuditableWithDeletesTraitObserver.php

View workflow job for this annotation

GitHub Actions / Source Code

Call to an undefined method Illuminate\Database\Eloquent\Model::isAuditable().
$deletedBy = $model->getDeletedByColumn();

$model->$deletedBy = $this->getAuthenticatedUserId();
Expand All @@ -32,7 +32,7 @@
*/
public function restoring(Model $model): void
{
if (method_exists($model, 'getDeletedByColumn')) {
if (method_exists($model, 'getDeletedByColumn') && $model->isAuditable()) {

Check failure on line 35 in src/AuditableWithDeletesTraitObserver.php

View workflow job for this annotation

GitHub Actions / Source Code

Call to an undefined method Illuminate\Database\Eloquent\Model::isAuditable().
$deletedBy = $model->getDeletedByColumn();

$model->$deletedBy = null;
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/AuditableModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,29 @@
expect($model->created_by)->toBe($anotherUser->id);
expect($model->updated_by)->toBe($anotherUser->id);
});

test('a post can be created without audit', function () {
$user = User::forceCreate([
'name' => 'John Doe',
'email' => '[email protected]',
]);

actingAs($user);

Post::withoutAudits(function () {
$post = new Post;
$post->title = 'Hello World';
$post->save();

expect($post->created_by)->toBe(null);
expect($post->updated_by)->toBe(null);
expect($post->deleted_by)->toBe(null);
});

$post = Post::first();
$post->title = 'Hello World 2';
$post->save();

expect($post->updated_by)->toBe($user->id);
expect($post->deleted_by)->toBe(null);
});
Loading