Skip to content
Permalink
Browse files

Merge pull request #627 from spatie/issue-623

respect date casts
  • Loading branch information
Gummibeer committed Mar 13, 2020
2 parents c73d0d2 + 7dd19a7 commit 461bd83ae8c595e187ebefe9fb867a26ce4d261a
Showing with 65 additions and 12 deletions.
  1. +5 −1 CHANGELOG.md
  2. +26 −11 src/Traits/DetectsChanges.php
  3. +34 −0 tests/DetectsChangesTest.php
@@ -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

@@ -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]);
}
}
}
@@ -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 {
@@ -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()
{

0 comments on commit 461bd83

Please sign in to comment.
You can’t perform that action at this time.