Skip to content

Commit

Permalink
add custom operation meta support (#19)
Browse files Browse the repository at this point in the history
* add custom operation meta support to `HasHistories` trait
  • Loading branch information
adbhutagaurangadas authored Sep 22, 2021
1 parent 4fd4d03 commit 10b7c61
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 22 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,32 @@ To fix this, you'll need to enable custom auth guards scanning in config file:
'auth_guards' => null
```

### Custom meta

You can define your own method for meta data. By default for `updating` event meta consists of modified keys and for other events meta is `null`.

Just redefine the method `getModelMeta` for the trait.

Example:

```php
class Article extends Model
{
use HasHistories;

...

public function getModelMeta($event)
{
// using defaults for updating
if($event == 'updating') return parent::getModelMeta($event);
// passing full model to meta
// ['key1' => 'value1', 'key2' => 'value2', ...]
else return $this;
}
}
```

### Known issues

1. When updating a model, if its model label(attributes returned from `getModelLabel`) has been modified, the history message will use its new attributes, which might not be what you expect.
Expand Down
39 changes: 38 additions & 1 deletion src/HasHistories.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,47 @@ public static function bootHasHistories()
static::observe(HistoryObserver::class);
}

/**
* Get the model's meta in history.
*
* @return array
*/
public function getModelMeta($event)
{
switch($event)
{
case 'updating':
/*
* Gets the model's altered values and tracks what had changed
*/
$changes = $this->getDirty();

$changed = [];
foreach ($changes as $key => $value) {
if(static::isIgnored($this, $key)) continue;

array_push($changed, ['key' => $key, 'old' => $this->getOriginal($key), 'new' => $this->$key]);
}
return $changed;
case 'created':
case 'deleting':
case 'restored':
return null;
}
}

public static function isIgnored($model, $key)
{
$blacklist = config('history.attributes_blacklist');
$name = get_class($model);
$array = isset($blacklist[$name])? $blacklist[$name]: null;
return !empty($array) && in_array($key, $array);
}

/**
* Get the model's label in history.
*
* @return string
*/
public abstract function getModelLabel();
}
}
30 changes: 9 additions & 21 deletions src/HistoryObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class HistoryObserver
* @return void
*/
public function created($model)
{
{
if(!static::filter('created')) return;

$model->morphMany(History::class, 'model')->create([
'message' => trans('panoscape::history.created', ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]),
'meta' => $model->getModelMeta('created'),
'user_id' => static::getUserID(),
'user_type' => static::getUserType(),
'performed_at' => time(),
Expand All @@ -32,7 +33,7 @@ public function created($model)
* @return void
*/
public function updating($model)
{
{
if(!static::filter('updating')) return;

/*
Expand All @@ -43,17 +44,10 @@ public function updating($model)
* Bypass restoring event
*/
if(array_key_exists('deleted_at', $changes)) return;

$changed = [];
foreach ($changes as $key => $value) {
if(static::isIgnored($model, $key)) continue;

array_push($changed, ['key' => $key, 'old' => $model->getOriginal($key), 'new' => $model->$key]);
}

$model->morphMany(History::class, 'model')->create([
'message' => trans('panoscape::history.updating', ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]),
'meta' => $changed,
'meta' => $model->getModelMeta('updating'),
'user_id' => static::getUserID(),
'user_type' => static::getUserType(),
'performed_at' => time(),
Expand All @@ -67,11 +61,12 @@ public function updating($model)
* @return void
*/
public function deleting($model)
{
{
if(!static::filter('deleting')) return;

$model->morphMany(History::class, 'model')->create([
'message' => trans('panoscape::history.deleting', ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]),
'meta' => $model->getModelMeta('deleting'),
'user_id' => static::getUserID(),
'user_type' => static::getUserType(),
'performed_at' => time(),
Expand All @@ -85,11 +80,12 @@ public function deleting($model)
* @return void
*/
public function restored($model)
{
{
if(!static::filter('restored')) return;

$model->morphMany(History::class, 'model')->create([
'message' => trans('panoscape::history.restored', ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]),
'meta' => $model->getModelMeta('restored'),
'user_id' => static::getUserID(),
'user_type' => static::getUserType(),
'performed_at' => time(),
Expand All @@ -115,14 +111,6 @@ public static function getUserType()
return static::getAuth()->check() ? get_class(static::getAuth()->user()) : null;
}

public static function isIgnored($model, $key)
{
$blacklist = config('history.attributes_blacklist');
$name = get_class($model);
$array = isset($blacklist[$name])? $blacklist[$name]: null;
return !empty($array) && in_array($key, $array);
}

public static function filter($action)
{
if(!static::getAuth()->check()) {
Expand Down Expand Up @@ -158,5 +146,5 @@ private static function activeGuard()
}
return null;
}

}

0 comments on commit 10b7c61

Please sign in to comment.