Skip to content
Merged
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
13 changes: 13 additions & 0 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Spatie\Activitylog\Models\Activity as Model;

class Activity extends Model
{
/**
* The name of the "updated at" column.
*/
public const ?string UPDATED_AT = null;
}
2 changes: 2 additions & 0 deletions app/Models/Allergen.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Concerns\ActivatableTrait;
use App\Models\Concerns\HasHelloFreshIdsTrait;
use App\Models\Concerns\LogsModificationsTrait;
use Database\Factories\AllergenFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -24,6 +25,7 @@ class Allergen extends Model

use HasHelloFreshIdsTrait;
use HasTranslations;
use LogsModificationsTrait;

/**
* The attributes that are translatable.
Expand Down
90 changes: 90 additions & 0 deletions app/Models/Concerns/LogsActivityTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Models\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

/**
* @phpstan-require-extends Model
*/
trait LogsActivityTrait
{
use LogsActivity;

/**
* @return list<string>
*/
protected function getDontLogIfAttributesChangedOnly(): array
{
return ['created_at', 'updated_at', 'deleted_at'];
}

/**
* @return string[]
*/
protected function logExceptAttributes(): array
{
return ['created_at', 'updated_at'];
}

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->dontSubmitEmptyLogs()
->logAll()
->logExcept($this->logExceptAttributes())
->dontLogIfAttributesChangedOnly($this->getDontLogIfAttributesChangedOnly())
->logOnlyDirty()
->useLogName($this->getLogName());
}

protected function getLogName(): string
{
return $this->formatLogName($this::class);
}

protected function formatLogName(string $class): string
{
$className = Str::kebab(class_basename($class));

return Str::plural(explode('-', $className)[0]);
}

/**
* Get the event names that should be recorded.
*
* @return Collection<int, string>
*/
protected static function eventsToBeRecorded(): Collection
{
$class = static::class;

/** @var list<string> $doNotRecordEvents */
$doNotRecordEvents = property_exists($class, 'doNotRecordEvents') ? $class::$doNotRecordEvents : [];
$reject = collect($doNotRecordEvents);

if (property_exists($class, 'recordEvents')) {
/** @var list<string> $recordEvents */
$recordEvents = $class::$recordEvents;

return collect($recordEvents)->reject(fn (string $eventName): bool => $reject->contains($eventName));
}

$events = collect([
'created',
'updated',
'deleted',
]);

if (collect(class_uses_recursive(static::class))->contains(SoftDeletes::class)) {
$events->push('restored');
}

return $events->reject(fn (string $eventName): bool => $reject->contains($eventName));
}
}
17 changes: 17 additions & 0 deletions app/Models/Concerns/LogsModificationsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Models\Concerns;

trait LogsModificationsTrait
{
use LogsActivityTrait;

/**
* Events to not record for activity logging.
*
* @var list<string>
*/
protected static array $doNotRecordEvents = [
'created',
];
}
2 changes: 2 additions & 0 deletions app/Models/Cuisine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Concerns\ActivatableTrait;
use App\Models\Concerns\HasHelloFreshIdsTrait;
use App\Models\Concerns\LogsModificationsTrait;
use Database\Factories\CuisineFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -24,6 +25,7 @@ class Cuisine extends Model

use HasHelloFreshIdsTrait;
use HasTranslations;
use LogsModificationsTrait;

/**
* The attributes that are translatable.
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Ingredient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Concerns\ActivatableTrait;
use App\Models\Concerns\HasHelloFreshIdsTrait;
use App\Models\Concerns\LogsModificationsTrait;
use Database\Factories\IngredientFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -26,6 +27,7 @@ class Ingredient extends Model

use HasHelloFreshIdsTrait;
use HasTranslations;
use LogsModificationsTrait;

/**
* The attributes that are translatable.
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Concerns\ActivatableTrait;
use App\Models\Concerns\HasHandlesTrait;
use App\Models\Concerns\LogsModificationsTrait;
use Database\Factories\LabelFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -24,6 +25,7 @@ class Label extends Model

use HasHandlesTrait;
use HasTranslations;
use LogsModificationsTrait;

/**
* The attributes that are translatable.
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Models\Concerns\ActivatableTrait;
use App\Models\Concerns\LogsModificationsTrait;
use Carbon\Carbon;
use Database\Factories\MenuFactory;
use Illuminate\Database\Eloquent\Attributes\Scope;
Expand All @@ -25,6 +26,8 @@ class Menu extends Model
/** @use HasFactory<MenuFactory> */
use HasFactory;

use LogsModificationsTrait;

/**
* The attributes that are mass assignable.
*
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Models\Concerns\ActivatableTrait;
use App\Models\Concerns\LogsModificationsTrait;
use App\Observers\RecipeObserver;
use App\Support\HelloFresh\HelloFreshAsset;
use Database\Factories\RecipeFactory;
Expand Down Expand Up @@ -30,6 +31,7 @@ class Recipe extends Model
use HasFactory;

use HasTranslations;
use LogsModificationsTrait;
use SoftDeletes;

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Concerns\ActivatableTrait;
use App\Models\Concerns\HasHelloFreshIdsTrait;
use App\Models\Concerns\LogsModificationsTrait;
use Database\Factories\TagFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -24,6 +25,7 @@ class Tag extends Model

use HasHelloFreshIdsTrait;
use HasTranslations;
use LogsModificationsTrait;

/**
* The attributes that are translatable.
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Utensil.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Concerns\ActivatableTrait;
use App\Models\Concerns\HasHelloFreshIdsTrait;
use App\Models\Concerns\LogsModificationsTrait;
use Database\Factories\UtensilFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -24,6 +25,7 @@ class Utensil extends Model

use HasHelloFreshIdsTrait;
use HasTranslations;
use LogsModificationsTrait;

/**
* The attributes that are translatable.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"livewire/flux-pro": "^2.11",
"norman-huth/gd-text": "^1.0",
"sentry/sentry-laravel": "^4.20",
"spatie/laravel-activitylog": "^4.10",
"spatie/laravel-medialibrary": "^11.17",
"spatie/laravel-translatable": "^6.12"
},
Expand Down
93 changes: 92 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading