Skip to content

Commit

Permalink
APPDEV-9066 adding transaction_id and ip_address
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharon Luong committed Dec 20, 2019
1 parent 6c05edc commit 1fe0652
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Venturecraft/Revisionable/Revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ private function getValue($which = 'new')
// if there was an issue
// or, if it's a normal value

$mutator = 'get' . Str::studly($this->field) . 'Attribute';
// the accessor method is called getFieldDisplayAttribute in JB
$mutator = 'get' . Str::studly($this->field) . 'DisplayAttribute';
if (method_exists($main_model, $mutator)) {
return $this->format($this->field, $main_model->$mutator($this->$which_value));
}
Expand Down
8 changes: 7 additions & 1 deletion src/Venturecraft/Revisionable/Revisionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ public function postSave()
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'field' => $key,
'transaction_id' => $this->getTransactionId(),
'ip_address' => \Request::ip(),
'field' => $key,
'old_value' => Arr::get($this->originalData, $key),
'new_value' => $this->updatedData[$key],
'user_id' => $this->getSystemUserId(),
Expand Down Expand Up @@ -188,6 +190,8 @@ public function postCreate()
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'transaction_id' => $this->getTransactionId(),
'ip_address' => \Request::ip(),
'field' => self::CREATED_AT,
'old_value' => null,
'new_value' => $this->{self::CREATED_AT},
Expand All @@ -212,6 +216,8 @@ public function postDelete()
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'transaction_id' => $this->getTransactionId(),
'ip_address' => \Request::ip(),
'field' => $this->getDeletedAtColumn(),
'old_value' => null,
'new_value' => $this->{$this->getDeletedAtColumn()},
Expand Down
20 changes: 19 additions & 1 deletion src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ public function postSave()
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'transaction_id' => $this->getTransactionId(),
'ip_address' => \Request::ip(),
'field' => $key,
'old_value' => Arr::get($this->originalData, $key),
'new_value' => $this->updatedData[$key],
Expand Down Expand Up @@ -231,6 +233,8 @@ public function postCreate()
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'transaction_id' => $this->getTransactionId(),
'ip_address' => \Request::ip(),
'field' => self::CREATED_AT,
'old_value' => null,
'new_value' => $this->{self::CREATED_AT},
Expand Down Expand Up @@ -258,6 +262,8 @@ public function postDelete()
$revisions[] = array(
'revisionable_type' => $this->getMorphClass(),
'revisionable_id' => $this->getKey(),
'transaction_id' => $this->getTransactionId(),
'ip_address' => \Request::ip(),
'field' => $this->getDeletedAtColumn(),
'old_value' => null,
'new_value' => $this->{$this->getDeletedAtColumn()},
Expand Down Expand Up @@ -293,6 +299,16 @@ public function getSystemUserId()
return null;
}

/**
* Get the transaction that this revision is a part of. This value
* should be set from within the transaction block while saving
* your revisionable model.
**/
public function getTransactionId()
{
return \DB::select('select @transaction_id as id;')[0]->id;
}

/**
* Get all of the changes that have been made, that are also supposed
* to have their changes recorded
Expand Down Expand Up @@ -329,7 +345,9 @@ private function changedRevisionableFields()
*/
private function isRevisionable($key)
{

if ($field === 'updated_at') {
return false;
}
// If the field is explicitly revisionable, then return true.
// If it's explicitly not revisionable, return false.
// Otherwise, if neither condition is met, only return true if
Expand Down
2 changes: 2 additions & 0 deletions src/migrations/2013_04_09_062329_create_revisions_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function up()
$table->increments('id');
$table->string('revisionable_type');
$table->integer('revisionable_id');
$table->string('transaction_id');
$table->string('ip_address');
$table->integer('user_id')->nullable();
$table->string('field');
$table->text('old_value')->nullable();
Expand Down

0 comments on commit 1fe0652

Please sign in to comment.