Skip to content
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

Adding revision id #271

Open
wants to merge 6 commits into
base: master
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
9 changes: 8 additions & 1 deletion src/Venturecraft/Revisionable/Revisionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -175,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},
Expand All @@ -200,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()},
Expand Down
29 changes: 28 additions & 1 deletion src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -173,11 +194,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],
Expand Down Expand Up @@ -217,9 +240,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},
Expand All @@ -244,9 +269,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()},
Expand Down
30 changes: 30 additions & 0 deletions src/migrations/2017_02_23_062329_update_revisions_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;

class UpdateRevisionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('revisions', function ($table) {
$table->string('revision_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('revisions', function ($table) {
$table->dropColumn('revision_id');
});
}
}