Skip to content

Commit

Permalink
v1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ctf0 committed Sep 4, 2018
1 parent 820513d commit 9c71fca
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 89 deletions.
61 changes: 53 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Odin

[![Latest Stable Version](https://img.shields.io/packagist/v/ctf0/odin.svg)](https://packagist.org/packages/ctf0/odin) [![Total Downloads](https://img.shields.io/packagist/dt/ctf0/odin.svg)](https://packagist.org/packages/ctf0/odin) [![Donate with Bitcoin](https://en.cryptobadges.io/badge/micro/16ri7Hh848bw7vxbEevKHFuHXLmsV8Vc9L)](https://en.cryptobadges.io/donate/16ri7Hh848bw7vxbEevKHFuHXLmsV8Vc9L)
[![Latest Stable Version](https://img.shields.io/packagist/v/ctf0/odin.svg)](https://packagist.org/packages/ctf0/odin) [![Total Downloads](https://img.shields.io/packagist/dt/ctf0/odin.svg)](https://packagist.org/packages/ctf0/odin)

Manage model revisions with ease.

>If you are also looking to preview the form data before submitting it to the db, you may want to give [OverSeer](https://github.com/ctf0/OverSeer) a try.
> If you are also looking to preview the form data before submitting to the db, you may want to give [OverSeer](https://github.com/ctf0/OverSeer) a try.
<details><summary>Preview</summary>

Expand Down Expand Up @@ -49,6 +49,10 @@ Manage model revisions with ease.
+ if you are having issues [Check](https://ctf0.wordpress.com/2017/09/12/laravel-mix-es6/).

```js
// app.js

window.Vue = require('vue')

require('../vendor/Odin/js/manager')

new Vue({
Expand All @@ -66,11 +70,12 @@ Manage model revisions with ease.
- support soft deletes.
- [revision preview](https://github.com/ctf0/Odin/wiki/Preview-Revision).
- clear audits for permanently deleted models.

```bash
php artisan odin:gc
```

which can be scheduled as well
+ which can be scheduled as well
```php
$schedule->command('odin:gc')->sundays();
```
Expand Down Expand Up @@ -99,7 +104,7 @@ Manage model revisions with ease.
- run `php artisan migrate`

- add `Revisions` trait & `AuditableContract` contract to your model
+ for `User model` [Check](http://laravel-auditing.com/docs/7.0/audit-resolvers)
+ for `User model` [Check](http://laravel-auditing.com/docs/master/audit-resolvers)

```php

Expand All @@ -111,10 +116,40 @@ Manage model revisions with ease.
{
use Revisions;

/**
* resolve model title/name for the revision relation
* this is needed so we can render
* the model relation attach/detach changes
*/
public function getMiscTitleAttribute()
{
return $this->name;
}

// ...
}
```

- you can disable creating **ghost** audits where both `old/new` values are empty by using
+ remember that without the parent model audit log we cant show the relation changes

```php
// app/Providers/EventServiceProvider

use OwenIt\Auditing\Models\Audit;

public function boot()
{
parent::boot();

Audit::creating(function (Audit $model) {
if (empty($model->old_values) && empty($model->new_values)) {
return false;
}
});
}
```

- inside the model view ex.`post edit view` add

```blade
Expand All @@ -125,10 +160,20 @@ Manage model revisions with ease.

<br>

## Notes For `data:uri`
## Notes

- model `user_id` & `id` are excluded from the audit log by default.

- if you use `data:uri` in your revisionable content, change [`audits_table`](https://github.com/owen-it/laravel-auditing/blob/958a6edd4cd4f9d61aa34f288f708644e150e866/database/migrations/audits.stub#L33-L34) columns type to either `mediumText` or `longText` before migrating to avoid future errors of long data.
- **data:uri**
- if you use `data:uri` in your revisionable content, change [`audits_table`](https://github.com/owen-it/laravel-auditing/blob/958a6edd4cd4f9d61aa34f288f708644e150e866/database/migrations/audits.stub#L33-L34) columns type to either `mediumText` or `longText` before migrating to avoid future errors of long data.

- because `data:uri` is render blocking & isn't readable by humans, we truncate it to 75 char max **(the smallest stable data:uri is 78 char)**,
- because `data:uri` is a render blocking & isn't readable by humans, we truncate it to 75 char max **(the smallest stable data:uri is 78 char)**,<br>
note that this ***ONLY*** effects the displaying of the revision diff, we never touch the data that gets saved to the db.

note that this ***ONLY*** effects the displaying of the revision diff, we never touch the data that gets saved to the db.
- **model-relation**
+ atm the relation revision is limited, it means we can only show the `attach/detach` changes but we cant `undo/redo` any of them through the package it self.
+ also if you use mass update like `Model::update()` make sure to call `$model->touch();` afterwards to make sure an audit is created ex.
```php
$model = Model::update([...]);
$model->touch();
```
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"php" : "~7.0",
"illuminate/support": "~5.4.0|~5.5.0|~5.6.0",
"phpspec/php-diff": "^1.1",
"owen-it/laravel-auditing": "^7.0",
"owen-it/laravel-auditing": "^8.0",
"fico7489/laravel-pivot": "*",
"ctf0/package-changelog": "^1.0"
},
Expand All @@ -48,4 +48,4 @@
"@php artisan auditing:install"
]
}
}
}
8 changes: 4 additions & 4 deletions src/Odin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function toHtml($rev)
$data = array_reverse($data);
}

$old = '';
$new = '';
$old = '';
$new = '';

if (array_key_exists('new', $data)) {
$new = $data['new'];
Expand Down Expand Up @@ -103,8 +103,8 @@ public function toHtml($rev)

// render
foreach ($output as $e => $v) {
$old = isset($v[0]) ? $v[0] : '';
$new = isset($v[1]) ? $v[1] : '';
$old = isset($v[0]) ? $v[0] : '';
$new = isset($v[1]) ? $v[1] : '';

if ($res = $this->renderDiff($old, $new)) {
if (!in_array($col, $exist)) {
Expand Down
10 changes: 6 additions & 4 deletions src/OdinRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ public static function routes()
'prefix' => 'odin',
'as' => 'odin.',
], function () {
app('router')->post('revision/{id}/preview', '\ctf0\Odin\Controllers\OdinController@preview')->name('preview');
app('router')->post('restore/{id}', '\ctf0\Odin\Controllers\OdinController@restore')->name('restore');
app('router')->put('restore-soft/{id}', '\ctf0\Odin\Controllers\OdinController@restoreSoft')->name('restore.soft');
app('router')->delete('remove/{id}', '\ctf0\Odin\Controllers\OdinController@remove')->name('remove');
app('router')->setGroupNamespace('\ctf0\Odin\Controllers');

app('router')->post('revision/{id}/preview', 'OdinController@preview')->name('preview');
app('router')->post('restore/{id}', 'OdinController@restore')->name('restore');
app('router')->put('restore-soft/{id}', 'OdinController@restoreSoft')->name('restore.soft');
app('router')->delete('remove/{id}', 'OdinController@remove')->name('remove');
});
}
}
32 changes: 15 additions & 17 deletions src/OdinServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace ctf0\Odin;

use OwenIt\Auditing\Models\Audit;
use Illuminate\Support\ServiceProvider;
use ctf0\Odin\Commands\GarbageCollector;

Expand All @@ -18,7 +17,7 @@ public function boot()
$this->file = $this->app['files'];

$this->packagePublish();
$this->auditEvent();
$this->registerMacro();
$this->command();

// append extra data
Expand All @@ -27,6 +26,19 @@ public function boot()
}
}

protected function registerMacro()
{
$this->app['router']->macro('setGroupNamespace', function ($namesapce = null) {
$lastGroupStack = array_pop($this->groupStack);
if ($lastGroupStack !== null) {
array_set($lastGroupStack, 'namespace', $namesapce);
$this->groupStack[] = $lastGroupStack;
}

return $this;
});
}

/**
* [packagePublish description].
*
Expand Down Expand Up @@ -57,20 +69,6 @@ protected function packagePublish()
], 'views');
}

/**
* dont save audit when old & new are empty.
*
* @return [type] [description]
*/
protected function auditEvent()
{
Audit::creating(function (Audit $model) {
if (empty($model->old_values) && empty($model->new_values)) {
return false;
}
});
}

/**
* clear audits table of permanently deleted auditable models.
*
Expand Down Expand Up @@ -105,7 +103,7 @@ protected function autoReg()
$search = 'Odin';

if ($this->checkExist($mix_file, $search)) {
$data = "\n// Odin\nmix.sass('resources/assets/vendor/Odin/sass/style.scss', 'public/assets/vendor/Odin/style.css').version();";
$data = "\n// Odin\nmix.sass('resources/assets/vendor/Odin/sass/style.scss', 'public/assets/vendor/Odin/style.css')";

$this->file->append($mix_file, $data);
}
Expand Down
44 changes: 29 additions & 15 deletions src/Traits/Revisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,48 @@ trait Revisions
public static function bootRevisions()
{
static::pivotAttached(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
return $model->savePivotAudit(
if ($pivotIds) {
return $model->savePivotAudit(
'Attached',
$model->getKey(),
get_class($model->$relationName()->getRelated()),
$pivotIds[0],
$model->getKey()
$pivotIds[0]
);
}
});

static::pivotDetached(function ($model, $relationName, $pivotIds) {
return $model->savePivotAudit(
if ($pivotIds) {
return $model->savePivotAudit(
'Detached',
$model->getKey(),
get_class($model->$relationName()->getRelated()),
$pivotIds[0],
$model->getKey()
$pivotIds[0]
);
}
});
}

private function savePivotAudit($eventName, $relationClass, $relationId, $modelId)
private function savePivotAudit($eventName, $id, $relation, $pivotId)
{
return app('db')->table('audits_pivot')->insert([
'event' => $eventName,
'auditable_id' => $modelId,
'auditable_id' => $id,
'relation_type' => $relation,
'relation_id' => $pivotId,
'auditable_type' => $this->getMorphClass(),
'relation_type' => $relationClass,
'relation_id' => $relationId,
'created_at' => now(),
'updated_at' => now(),
]);
}

private function getPivotAudits($type, $id)
private function getPivotAudits($type, $id, $date)
{
return app('db')->table('audits_pivot')
->where('auditable_id', $id)
->where('auditable_type', $type)
->get()
->reverse();
->where('updated_at', $date)
->get();
}

/**
Expand All @@ -65,16 +69,26 @@ public function getAuditExclude(): array
return array_merge($main, $extra);
}

// Accessor for Revisions
/**
* normal : $model->audits.
*/
public function getRevisionsAttribute()
{
return $this->audits->load('user')->reverse();
}

/**
* with relation : $model->auditsWithRelation.
*/
public function getRevisionsWithRelationAttribute()
{
return $this->audits->load('user')->map(function ($item) {
$item['relations'] = $this->getPivotAudits($item->auditable_type, $item->auditable_id);
$item['odin_relations'] = $this->getPivotAudits($item->auditable_type, $item->auditable_id, $item->updated_at)
->groupBy(['created_at', 'relation_id', 'relation_type'])
->flatten(2)
->reject(function ($item) {
return $item->count() == 2;
})->flatten()->reverse();

return $item;
})->reverse();
Expand Down
12 changes: 7 additions & 5 deletions src/resources/assets/js/Odin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ export default {
goTo(id) {
this.updateRev(id)
animateScrollTo(document.getElementById(id), {
maxDuration: 1000,
offset: -28,
element: this.$refs.container,
useKeys: true
this.$nextTick(() => {
animateScrollTo(document.getElementById(id), {
maxDuration: 1000,
offset: -28,
element: this.$refs.container,
useKeys: true
})
})
},
Expand Down
1 change: 0 additions & 1 deletion src/resources/assets/js/manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* Libs */
window.Vue = require('vue')
window.EventHub = require('vuemit')
window.keycode = require('keycode')

Expand Down
4 changes: 4 additions & 0 deletions src/resources/assets/sass/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
.compare-page__body {
padding-top: 10px;

.table {
background-color: white;
}

.title {
margin: 0;
margin-left: 10px;
Expand Down
Loading

0 comments on commit 9c71fca

Please sign in to comment.