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

Allow revision data overload #252

Open
wants to merge 1 commit 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
35 changes: 35 additions & 0 deletions src/Venturecraft/Revisionable/MakeRevisionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php namespace Venturecraft\Revisionable;

/*
* This file is part of the Revisionable package by Venture Craft
*
* (c) Venture Craft <http://www.venturecraft.com.au>
*
*/

/**
* Trait MakeRevisionTrait
* @package Venturecraft\Revisionable
*/
trait MakeRevisionTrait
{
/**
* @param $data
* @return array
*/
protected function makeRevision($data)
{
$default = [
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'key' => null,
'old_value' => null,
'new_value' => null,
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
];

return array_merge($default, $data);
}
}
36 changes: 11 additions & 25 deletions src/Venturecraft/Revisionable/Revisionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
class Revisionable extends Eloquent
{
use MakeRevisionTrait;

/**
* @var
*/
Expand Down Expand Up @@ -140,16 +142,11 @@ public function postSave()
$revisions = array();

foreach ($changes_to_record as $key => $change) {
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'key' => $key,
'old_value' => array_get($this->originalData, $key),
'new_value' => $this->updatedData[$key],
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);
$revisions[] = $this->makeRevision([
'key' => $key,
'old_value' => array_get($this->originalData, $key),
'new_value' => $this->updatedData[$key],
]);
}

if (count($revisions) > 0) {
Expand All @@ -175,17 +172,11 @@ public function postCreate()

if ((!isset($this->revisionEnabled) || $this->revisionEnabled))
{
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
$revisions[] = $this->makeRevision([
'key' => self::CREATED_AT,
'old_value' => null,
'new_value' => $this->{self::CREATED_AT},
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);

]);
$revision = new Revision;
\DB::table($revision->getTable())->insert($revisions);

Expand All @@ -200,16 +191,11 @@ public function postDelete()
if ((!isset($this->revisionEnabled) || $this->revisionEnabled)
&& $this->isSoftDelete()
&& $this->isRevisionable($this->getDeletedAtColumn())) {
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
$revisions[] = $this->makeRevision([
'key' => $this->getDeletedAtColumn(),
'old_value' => null,
'new_value' => $this->{$this->getDeletedAtColumn()},
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);
]);
$revision = new \Venturecraft\Revisionable\Revision;
\DB::table($revision->getTable())->insert($revisions);
}
Expand Down
32 changes: 8 additions & 24 deletions src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
trait RevisionableTrait
{
use MakeRevisionTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -175,16 +177,11 @@ public function postSave()
$revisions = array();

foreach ($changes_to_record as $key => $change) {
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
$revisions[] = $this->makeRevision([
'key' => $key,
'old_value' => array_get($this->originalData, $key),
'new_value' => $this->updatedData[$key],
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);
]);
}

if (count($revisions) > 0) {
Expand Down Expand Up @@ -217,17 +214,10 @@ public function postCreate()

if ((!isset($this->revisionEnabled) || $this->revisionEnabled))
{
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
$revisions[] = $this->makeRevision([
'key' => self::CREATED_AT,
'old_value' => null,
'new_value' => $this->{self::CREATED_AT},
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);

]);
$revision = new Revision;
\DB::table($revision->getTable())->insert($revisions);
\Event::fire('revisionable.created', array('model' => $this, 'revisions' => $revisions));
Expand All @@ -244,16 +234,10 @@ public function postDelete()
&& $this->isSoftDelete()
&& $this->isRevisionable($this->getDeletedAtColumn())
) {
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
$revisions[] = $this->makeRevision([
'key' => $this->getDeletedAtColumn(),
'old_value' => null,
'new_value' => $this->{$this->getDeletedAtColumn()},
'user_id' => $this->getSystemUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);
]);
$revision = new \Venturecraft\Revisionable\Revision;
\DB::table($revision->getTable())->insert($revisions);
\Event::fire('revisionable.deleted', array('model' => $this, 'revisions' => $revisions));
Expand Down