Skip to content

Commit

Permalink
Merge pull request #969 from pxlrbt/master
Browse files Browse the repository at this point in the history
Add support for immutable_date casts
  • Loading branch information
Gummibeer authored Oct 6, 2021
2 parents 7028cba + 867731b commit f4705d4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"require": {
"php": "^8.0",
"illuminate/config": "^8.0",
"illuminate/database": "^8.0",
"illuminate/database": "^8.53",
"illuminate/support": "^8.0",
"spatie/laravel-package-tools": "^1.6.3"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public static function logChanges(Model $model): array
if ($model->hasCast($attribute)) {
$cast = $model->getCasts()[$attribute];

if ($model->isCustomDateTimeCast($cast)) {
if ($model->isCustomDateTimeCast($cast) || $model->isImmutableCustomDateTimeCast($cast)) {
$changes[$attribute] = $model->asDateTime($changes[$attribute])->format(explode(':', $cast, 2)[1]);
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/DetectsChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,44 @@ public function getActivitylogOptions(): LogOptions
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_use_custom_immutable_date_cast_as_loggable_attributes()
{
$userClass = new class() extends User {
use LogsActivity;

protected $fillable = ['name', 'text'];
protected $casts = [
'created_at' => 'immutable_date:d.m.Y',
];

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logAll();
}
};

Carbon::setTestNow(Carbon::create(2017, 1, 1, 12, 0, 0));
$user = new $userClass();
$user->name = 'my name';
$user->text = 'my text';
$user->save();

$expectedChanges = [
'attributes' => [
'id' => $user->getKey(),
'name' => 'my name',
'text' => 'my text',
'created_at' => '01.01.2017',
'updated_at' => '2017-01-01T12:00:00.000000Z',
'deleted_at' => null,
],
];

$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_store_the_changes_of_json_attributes()
{
Expand Down

0 comments on commit f4705d4

Please sign in to comment.