Skip to content

Commit

Permalink
it numbers versions sequentially
Browse files Browse the repository at this point in the history
  • Loading branch information
sheadawson committed Sep 17, 2024
1 parent b4906e7 commit c6d2edc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/Concerns/HasPublishing.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public function publish(): static|bool
// fire the published event
$this->fireModelEvent('published');

return $this->fresh();
$this->refresh();

return $this;
}

/**
Expand Down Expand Up @@ -96,7 +98,9 @@ public function unpublish(): static
// fire the unpublished event
$this->fireModelEvent('unpublished');

return $this->fresh();
$this->refresh();

return $this;
}

/**
Expand All @@ -117,7 +121,7 @@ public function setPublishedAttributes(): static
public function applyStateToPublishedRecord(): static
{
// find or make the published record
$published = static::withPublishedTable()->findOrNew($this->{$this->getKey()});
$published = $this->publishedRecord ?? static::withPublishedTable();

// copy the attributes from the base record to the published record
$published->forceFill($this->attributes);
Expand Down
13 changes: 9 additions & 4 deletions src/Concerns/HasVersioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public function initializeHasVersioning(): void
}

/**
* Create a new record in the version table
* Ensure it is_current and other versions are not
* Creates a new record in the version table
* Ensures it is_current and other versions are not
* Updates the current base record to have the new version_number
*/
public function recordNewVersion(): static|bool
{
Expand All @@ -77,6 +78,7 @@ public function recordNewVersion(): static|bool
[
'is_current' => 1,
'record_id' => $this->id,
'version_number' => ($this->versions()->max('version_number') ?? 0) + 1,
]
);
unset($attributes['id']);
Expand All @@ -86,8 +88,11 @@ public function recordNewVersion(): static|bool
// update all other versions to not be current
$this->versions()
->where('is_current', 1)
->whereNot($version->getKeyName(), $version->getKey())
->each(fn ($version) => $version->forceFill(['is_current' => 0])->saveQuietly());
->where($version->getKeyName(), '!=', $version->getKey())
->update(['is_current' => 0]);

// update the current base record to have the new version_number
$this->forceFill(['version_number' => $version->version_number])->saveQuietly();

$this->fireModelEvent('savedNewVersion');

Expand Down
7 changes: 5 additions & 2 deletions src/Revisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public function createTableSchemas(string $baseTableName, \Closure $callback): v
$callback($table);
$table->nullableMorphs('publisher');
$table->timestamp('published_at')->nullable();
$table->boolean('is_current')->default(0);
$table->boolean('is_published')->default(0);
$table->boolean('is_current')->default(0)->index();
$table->boolean('is_published')->default(0)->index();
$table->integer('version_number')->unsigned()->nullable()->index();
$table->foreignId('record_id')->constrained($baseTableName)->cascadeOnDelete();
});

Expand All @@ -35,6 +36,7 @@ public function createTableSchemas(string $baseTableName, \Closure $callback): v
$table->timestamp('published_at')->nullable();
$table->boolean('is_current')->default(0);
$table->boolean('is_published')->default(0);
$table->integer('version_number')->unsigned()->nullable()->index();
});

// create the base table
Expand All @@ -44,6 +46,7 @@ public function createTableSchemas(string $baseTableName, \Closure $callback): v
$table->timestamp('published_at')->nullable();
$table->boolean('is_current')->default(0);
$table->boolean('is_published')->default(0);
$table->integer('version_number')->unsigned()->nullable()->index();
});
}

Expand Down
16 changes: 16 additions & 0 deletions tests/HasVersioningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@
expect($page->versions()->count())->toBe(3);
});

it('numbers versions sequentially', function () {
$page = Page::create(['title' => 'Home']);
$page->publish();

expect($page->currentVersion->version_number)->toBe(1)
->and($page->version_number)->toBe(1)
->and($page->publishedRecord->version_number)->toBe(1);

$page->update(['title' => 'Home 2']);
$page->publish();

expect($page->currentVersion->version_number)->toBe(2)
->and($page->version_number)->toBe(2)
->and($page->publishedRecord->version_number)->toBe(2);
});

it('can rollback versions', function () {
$page = Page::create(['title' => 'Home']);
$page->update(['title' => 'Home 2']);
Expand Down

0 comments on commit c6d2edc

Please sign in to comment.