From ab5b4af215bd47059abf737be0fcddab8ed87caa Mon Sep 17 00:00:00 2001 From: Marek Toth Date: Thu, 23 Feb 2017 13:40:15 +0100 Subject: [PATCH 1/6] adding unique revision identifier --- .../Revisionable/Revisionable.php | 4 ++- ...17_02_23_062329_update_revisions_table.php | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/migrations/2017_02_23_062329_update_revisions_table.php diff --git a/src/Venturecraft/Revisionable/Revisionable.php b/src/Venturecraft/Revisionable/Revisionable.php index 7099ef50..1222b985 100644 --- a/src/Venturecraft/Revisionable/Revisionable.php +++ b/src/Venturecraft/Revisionable/Revisionable.php @@ -138,11 +138,13 @@ public function postSave() $changes_to_record = $this->changedRevisionableFields(); $revisions = array(); - + $uniqid = uniqid(); + foreach ($changes_to_record as $key => $change) { $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => $key, 'old_value' => array_get($this->originalData, $key), 'new_value' => $this->updatedData[$key], diff --git a/src/migrations/2017_02_23_062329_update_revisions_table.php b/src/migrations/2017_02_23_062329_update_revisions_table.php new file mode 100644 index 00000000..d6df2af9 --- /dev/null +++ b/src/migrations/2017_02_23_062329_update_revisions_table.php @@ -0,0 +1,30 @@ +string('revision_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('revisions', function (Blueprint $table) { + $table->dropColumn('revision_id'); + }); + } +} From 833a9c107178b5234a10ed867b5b367b1c7d1019 Mon Sep 17 00:00:00 2001 From: Marek Toth Date: Thu, 23 Feb 2017 13:46:22 +0100 Subject: [PATCH 2/6] updating migration namespace --- src/migrations/2017_02_23_062329_update_revisions_table.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/migrations/2017_02_23_062329_update_revisions_table.php b/src/migrations/2017_02_23_062329_update_revisions_table.php index d6df2af9..bc5a93d9 100644 --- a/src/migrations/2017_02_23_062329_update_revisions_table.php +++ b/src/migrations/2017_02_23_062329_update_revisions_table.php @@ -11,7 +11,7 @@ class CreateRevisionsTable extends Migration */ public function up() { - Schema::table('revisions', function (Blueprint $table) { + Schema::table('revisions', function ($table) { $table->string('revision_id'); }); } @@ -23,7 +23,7 @@ public function up() */ public function down() { - Schema::table('revisions', function (Blueprint $table) { + Schema::table('revisions', function ($table) { $table->dropColumn('revision_id'); }); } From 0e8c07eb382e2c934c3b0202246d237a305799bd Mon Sep 17 00:00:00 2001 From: Marek Toth Date: Thu, 23 Feb 2017 13:47:47 +0100 Subject: [PATCH 3/6] updating migration namespace --- src/migrations/2017_02_23_062329_update_revisions_table.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/migrations/2017_02_23_062329_update_revisions_table.php b/src/migrations/2017_02_23_062329_update_revisions_table.php index bc5a93d9..4bb9cbbe 100644 --- a/src/migrations/2017_02_23_062329_update_revisions_table.php +++ b/src/migrations/2017_02_23_062329_update_revisions_table.php @@ -2,7 +2,7 @@ use Illuminate\Database\Migrations\Migration; -class CreateRevisionsTable extends Migration +class UpdateRevisionsTable extends Migration { /** * Run the migrations. From 38764b4de1a2470fc62f12d580f1965641688d27 Mon Sep 17 00:00:00 2001 From: Marek Toth Date: Thu, 23 Feb 2017 13:52:05 +0100 Subject: [PATCH 4/6] unique revision identifier --- src/Venturecraft/Revisionable/Revisionable.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Venturecraft/Revisionable/Revisionable.php b/src/Venturecraft/Revisionable/Revisionable.php index 1222b985..ece66ef4 100644 --- a/src/Venturecraft/Revisionable/Revisionable.php +++ b/src/Venturecraft/Revisionable/Revisionable.php @@ -177,9 +177,11 @@ public function postCreate() if ((!isset($this->revisionEnabled) || $this->revisionEnabled)) { + $uniqid = uniqid(); $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => self::CREATED_AT, 'old_value' => null, 'new_value' => $this->{self::CREATED_AT}, @@ -202,9 +204,12 @@ public function postDelete() if ((!isset($this->revisionEnabled) || $this->revisionEnabled) && $this->isSoftDelete() && $this->isRevisionable($this->getDeletedAtColumn())) { + + $uniqid = uniqid(); $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => $this->getDeletedAtColumn(), 'old_value' => null, 'new_value' => $this->{$this->getDeletedAtColumn()}, From ad1f22c014841bb78940b0c0d81abe1f94aed266 Mon Sep 17 00:00:00 2001 From: Marek Toth Date: Thu, 23 Feb 2017 13:55:00 +0100 Subject: [PATCH 5/6] unique revision identifier --- src/Venturecraft/Revisionable/RevisionableTrait.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index f298dcf7..088fb2a5 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -173,11 +173,13 @@ public function postSave() $changes_to_record = $this->changedRevisionableFields(); $revisions = array(); - + $uniqid = uniqid(); + foreach ($changes_to_record as $key => $change) { $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => $key, 'old_value' => array_get($this->originalData, $key), 'new_value' => $this->updatedData[$key], @@ -217,9 +219,11 @@ public function postCreate() if ((!isset($this->revisionEnabled) || $this->revisionEnabled)) { + $uniqid = uniqid(); $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => self::CREATED_AT, 'old_value' => null, 'new_value' => $this->{self::CREATED_AT}, @@ -244,9 +248,11 @@ public function postDelete() && $this->isSoftDelete() && $this->isRevisionable($this->getDeletedAtColumn()) ) { + $uniqid = uniqid(); $revisions[] = array( 'revisionable_type' => $this->getMorphClass(), 'revisionable_id' => $this->getKey(), + 'revision_id' => $uniqid, 'key' => $this->getDeletedAtColumn(), 'old_value' => null, 'new_value' => $this->{$this->getDeletedAtColumn()}, From 8037c592752ae92ae7ca7cfc7fa3031d4af439bb Mon Sep 17 00:00:00 2001 From: Marek Toth Date: Sun, 26 Feb 2017 14:16:00 +0100 Subject: [PATCH 6/6] formatting update --- .../Revisionable/RevisionableTrait.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Venturecraft/Revisionable/RevisionableTrait.php b/src/Venturecraft/Revisionable/RevisionableTrait.php index 088fb2a5..1e62a466 100644 --- a/src/Venturecraft/Revisionable/RevisionableTrait.php +++ b/src/Venturecraft/Revisionable/RevisionableTrait.php @@ -93,6 +93,27 @@ public function revisionHistory() return $this->morphMany('\Venturecraft\Revisionable\Revision', 'revisionable'); } + /** + * @return mixed + */ + public function revisionHistoryMapped() + { + $out = []; + $data = $this->revisionHistory()->get()->sortByDesc(function ($item) { + return $item->created_at; + }); + foreach ($data as $key => $item) { + $out[$item->revision_id]['item'] = $item; + $out[$item->revision_id]['revision_data'][] = [ + 'key' => $item->key, + 'old_value' => $item->oldValue(), + 'new_value' => $item->newValue() + ]; + } + + return $out; + } + /** * Generates a list of the last $limit revisions made to any objects of the class it is being called from. *