Skip to content

Commit

Permalink
Merge pull request #972 from sebastiansson/master
Browse files Browse the repository at this point in the history
Add new option to config to allow for casted attributes to be logged raw
  • Loading branch information
Gummibeer authored Oct 20, 2021
2 parents f10d99a + 4045a04 commit 9b1776c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/api/log-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public array $logExceptAttributes = [];

public array $dontLogIfAttributesChangedOnly = [];

public array $attributeRawValues = [];

public ?Closure $descriptionForEvent = null;
```

Expand Down Expand Up @@ -165,6 +167,15 @@ public function submitEmptyLogs(): LogOption;
public function useLogName(string $logName): LogOption;
```

### useAttributeRawValues

```php
/**
* Skip using mutators for these attributes when logged
*/
public function useAttributeRawValues(array $attributes): LogOption;
```

### setDescriptionForEvent

```php
Expand Down
12 changes: 12 additions & 0 deletions src/LogOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class LogOptions

public array $dontLogIfAttributesChangedOnly = [];

public array $attributeRawValues = [];

public ?Closure $descriptionForEvent = null;

/**
Expand Down Expand Up @@ -151,4 +153,14 @@ public function setDescriptionForEvent(Closure $callback): self

return $this;
}

/**
* Exclude these attributes from being casted.
*/
public function useAttributeRawValues(array $attributes): self
{
$this->attributeRawValues = $attributes;

return $this;
}
}
4 changes: 3 additions & 1 deletion src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ public static function logChanges(Model $model): array
continue;
}

$changes[$attribute] = $model->getAttribute($attribute);
$changes[$attribute] = in_array($attribute, $model->activitylogOptions->attributeRawValues)
? $model->getAttributeFromArray($attribute)
: $model->getAttribute($attribute);

if (is_null($changes[$attribute])) {
continue;
Expand Down
27 changes: 27 additions & 0 deletions tests/LogsActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,33 @@ public function it_will_log_the_retrieval_of_the_model()
$this->assertEquals('retrieved', $activity->description);
}

/** @test */
public function it_will_not_log_casted_attribute_of_the_model_if_attribute_raw_values_is_used()
{
$articleClass = new class() extends Article {
use LogsActivity;

protected $casts = [
'name' => 'encrypted',
];

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(['name'])->useAttributeRawValues(["name"]);
}
};

$article = new $articleClass();
$article->name = 'my name';
$article->save();

$this->assertInstanceOf(get_class($articleClass), $this->getLastActivity()->subject);
$this->assertEquals($article->id, $this->getLastActivity()->subject->id);
$this->assertNotEquals($article->name, $this->getLastActivity()->properties["attributes"]["name"]);
$this->assertEquals('created', $this->getLastActivity()->description);
$this->assertEquals('created', $this->getLastActivity()->event);
}

public function loginWithFakeUser()
{
$user = new $this->user();
Expand Down

0 comments on commit 9b1776c

Please sign in to comment.