Skip to content

Commit

Permalink
Merge pull request #627 from spatie/issue-623
Browse files Browse the repository at this point in the history
respect date casts
  • Loading branch information
Gummibeer authored Mar 13, 2020
2 parents c73d0d2 + 7dd19a7 commit 461bd83
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

All notable changes to `spatie/laravel-activitylog` will be documented in this file

## 3.12.0 - 2020-03-13

- respect custom date casts [#627](https://github.com/spatie/laravel-activitylog/pull/627)

## 3.11.4 - 2020-03-11

- remove `spatie/string` dependency
- remove `spatie/string` dependency [#690](https://github.com/spatie/laravel-activitylog/pull/690)

## 3.11.3 - 2020-03-10

Expand Down
37 changes: 26 additions & 11 deletions src/Traits/DetectsChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,37 @@ public static function logChanges(Model $model): array
foreach ($attributes as $attribute) {
if (Str::contains($attribute, '.')) {
$changes += self::getRelatedModelAttributeValue($model, $attribute);
} elseif (Str::contains($attribute, '->')) {

continue;
}

if (Str::contains($attribute, '->')) {
Arr::set(
$changes,
str_replace('->', '.', $attribute),
static::getModelAttributeJsonValue($model, $attribute)
);
} else {
$changes[$attribute] = $model->getAttribute($attribute);

if (
in_array($attribute, $model->getDates())
&& ! is_null($changes[$attribute])
) {
$changes[$attribute] = $model->serializeDate(
$model->asDateTime($changes[$attribute])
);

continue;
}

$changes[$attribute] = $model->getAttribute($attribute);

if (is_null($changes[$attribute])) {
continue;
}

if ($model->isDateAttribute($attribute)) {
$changes[$attribute] = $model->serializeDate(
$model->asDateTime($changes[$attribute])
);
}

if ($model->hasCast($attribute)) {
$cast = $model->getCasts()[$attribute];

if ($model->isCustomDateTimeCast($cast)) {
$changes[$attribute] = $model->asDateTime($changes[$attribute])->format(explode(':', $cast, 2)[1]);
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/DetectsChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ public function it_can_use_casted_as_loggable_attribute()
$this->assertIsFloat($changes['attributes']['price']);
}

/** @test */
public function it_can_use_nullable_date_as_loggable_attributes()
{
$userClass = new class() extends User {
Expand Down Expand Up @@ -1145,6 +1146,39 @@ public function it_can_use_nullable_date_as_loggable_attributes()
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_use_custom_date_cast_as_loggable_attributes()
{
$userClass = new class() extends User {
protected $fillable = ['name', 'text'];
protected static $logAttributes = ['*'];
protected $casts = [
'created_at' => 'date:d.m.Y',
];

use LogsActivity;
};

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' => $this->isLaravel6OrLower() ? '2017-01-01 12:00:00' : '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 461bd83

Please sign in to comment.