Skip to content

Commit

Permalink
Merge pull request neurony#12 from 23G/revision-timestamps
Browse files Browse the repository at this point in the history
Allow for revisions to contain timestamps
  • Loading branch information
zbiller authored Apr 16, 2019
2 parents 41bbfec + 9a4d037 commit 1d6af05
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/Options/RevisionOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class RevisionOptions
*/
private $createRevisionWhenRollingBack = true;

/**
* Flag indicating whether to include timestamps in the revision.
*
* @var bool
*/
private $revisionTimestamps = false;

/**
* Get the value of a property of this class.
*
Expand Down Expand Up @@ -158,4 +165,16 @@ public function disableRevisioningWhenRollingBack(): self

return $this;
}

/**
* Enable the revisioning of timestamps.
*
* @return $this
*/
public function withTimestamps()
{
$this->revisionTimestamps = true;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/Traits/SaveRevisionJsonRepresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function buildRevisionDataFromModel(): array

unset($data[$this->getKeyName()]);

if ($this->usesTimestamps()) {
if ($this->usesTimestamps() && !$this->revisionOptions->revisionTimestamps) {
unset($data[$this->getCreatedAtColumn()]);
unset($data[$this->getUpdatedAtColumn()]);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/HasRevisionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ public function getRevisionOptions() : RevisionOptions
$this->assertArrayHasKey('views', $revision->metadata);
}

/** @test */
public function it_can_include_timestamps_when_creating_a_revision()
{
$model = new class extends Post {
public function getRevisionOptions() : RevisionOptions
{
return parent::getRevisionOptions()->withTimestamps();
}
};

$this->makeModels($model);
$this->modifyPost();

$revision = $this->post->revisions()->first();

$this->assertArrayHasKey('created_at', $revision->metadata);
$this->assertArrayHasKey('updated_at', $revision->metadata);
}

/** @test */
public function it_can_save_belongs_to_relations_when_creating_a_revision()
{
Expand Down

0 comments on commit 1d6af05

Please sign in to comment.