Skip to content

Feature/revisions #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions database/migrations/2021_04_02_000000_create_revisions_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRevisionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('revisions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('table');
$table->bigInteger('relation_id');
$table->json('data');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('revisions');
}
}
17 changes: 17 additions & 0 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function __construct(array $attributes = [])
*/
protected $cacheable = false;

/**
* @var bool
*/
protected $revisionable = false;

/**
* Trigger Model Observers
*/
Expand All @@ -60,6 +65,9 @@ protected static function boot()
self::created(function (Model $model) use ($observer) {
$observer->created($model);
});
self::updating(function (Model $model) use ($observer) {
$observer->updating($model);
});
self::updated(function (Model $model) use ($observer) {
$observer->updated($model);
});
Expand All @@ -86,6 +94,15 @@ public function cacheable()
return $this->cacheable;
}

/**
* If model is revisionable and should clear cache on created/updated/deleted
* @return bool
*/
public function revisionable()
{
return $this->revisionable;
}

/**
* Return associations
* @return array
Expand Down
14 changes: 14 additions & 0 deletions src/Models/Revision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Tychovbh\Mvc\Models;

use Illuminate\Database\Eloquent\Model;

class Revision extends Model
{
public function __construct(array $attributes = [])
{
$this->fillable = ['table', 'relation_id', 'data'];
parent::__construct($attributes);
}
}
21 changes: 21 additions & 0 deletions src/Observers/ModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace Tychovbh\Mvc\Observers;

use App\Repositories\RevisionRepository;
use Illuminate\Support\Facades\Cache;
use Tychovbh\Mvc\Models\Model;
use Tychovbh\Mvc\Models\Revision;

class ModelObserver
{
protected $revision = [];

/**
* Created event.
* @param Model $model
Expand All @@ -16,12 +20,29 @@ public function created(Model $model)
$this->flushIndex($model);
}

/**
* Updating event.
* @param Model $model
*/
public function updating(Model $model)
{
$this->revision = [
'table' => $model->getTable(),
'relation_id' => $model->id,
'data' => json_encode($model->getOriginal())
];
}

/**
* Updated event.
* @param Model $model
*/
public function updated(Model $model)
{
$revision = new Revision();
$revision->fill($this->revision);
$revision->save();

$this->flushIndex($model);
$this->flushShow($model);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Repositories/RevisionRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Tychovbh\Mvc\Repositories;

use Tychovbh\Mvc\Repositories\Repository;
use Tychovbh\Mvc\Repositories\AbstractRepository;

class RevisionRepository extends AbstractRepository implements Repository
{
//
}
2 changes: 2 additions & 0 deletions tests/app/TestUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class TestUser extends Model
{
protected $cacheable = true;

protected $revisions = true;

/**
* TestUser constructor.
* @param array $attributes
Expand Down
25 changes: 25 additions & 0 deletions tests/feature/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Tychovbh\Tests\Mvc\Feature;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Carbon;
use Tychovbh\Mvc\Models\Form;
Expand Down Expand Up @@ -142,6 +143,30 @@ public function itCanUpdate()
$this->update('users.update', UserResource::make($update), $params);
}

/**
* @test
*/
public function itCanUpdateWithRevisions()
{
$user = factory(User::class)->create([
'avatar' => null
]);
$update = factory(User::class)->make([
'email' => $user->email
]);
$params = $update->toArray();
$update->id = $user->id;
$update->updated_at = Carbon::now();

$this->update('users.update', UserResource::make($update), $params);

$revision = DB::table('revisions')->where('relation_id', $user->id)->first();

$this->assertDatabaseHas('revisions', (array)$revision);

$this->markAsRisky();
}

/**
* @test
*/
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1