Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sheadawson committed Sep 16, 2024
1 parent e402490 commit aef9f48
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 47 deletions.
16 changes: 0 additions & 16 deletions src/Concerns/HasPublishing.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,6 @@ public static function bootHasPublishing(): void
$model->publish();
}
});

// static::deleted(function (Model $model): void {
// $model->revisions()->delete();
// });
//
// if (method_exists(static::class, 'restored')) {
// static::restored(function (Model $model): void {
// $model->revisions()->restore();
// });
// }
//
// if (method_exists(static::class, 'forceDeleted')) {
// static::forceDeleted(function (Model $model): void {
// $model->revisions()->forceDelete();
// });
// }
}

public function initializeHasPublishing(): void
Expand Down
18 changes: 1 addition & 17 deletions src/Concerns/HasRevisor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function setWithVersionTable(bool $bool = true): static
}

/**
* TODO: This needs a rethink...
* Override the fireModelEvent method to prevent events from firing on
* the version or published tables.
*/
Expand All @@ -60,21 +59,6 @@ protected function fireModelEvent($event, $halt = true): mixed
return true;
}

// First, we will get the proper method to call on the event dispatcher, and then we
// will attempt to fire a custom, object based event for the given event. If that
// returns a result we can return that result, or we'll call the string events.
$method = $halt ? 'until' : 'dispatch';

$result = $this->filterModelEventResults(
$this->fireCustomModelEvent($event, $method)
);

if ($result === false) {
return false;
}

return ! empty($result) ? $result : static::$dispatcher->{$method}(
"eloquent.{$event}: ".static::class, $this
);
return parent::fireModelEvent($event, $halt);
}
}
7 changes: 7 additions & 0 deletions src/Concerns/HasVersioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ public function shouldRecordNewVersionOnCreated(): bool
$this->recordNewVersionOnCreated;
}

public function recordNewVersionOnUpdated(bool $bool = true): static
{
$this->recordNewVersionOnUpdated = $bool;

return $this;
}

public function shouldRecordNewVersionOnUpdated(): bool
{
return is_null($this->recordNewVersionOnUpdated) ?
Expand Down
43 changes: 31 additions & 12 deletions tests/PublishingTest.php → tests/HasPublishingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,50 @@
Revisor::getAllTablesFor('pages')->each(fn ($table) => DB::table($table)->truncate());
});

it('does not publish a record on save when not configured to do so', function () {
$page = Page::create(['title' => 'Homes']);
it('publishes on created only when configured to do so', function () {
$page = Page::create(['title' => 'Home']);
$page->refresh();
expect($page->is_published)->toBeFalse()
->and(Page::withPublishedTable()->find($page->id))->toBeNull();
});

it('publishes a record on save when configured to do so globally', function () {
config()->set('revisor.publish_on_save', true);
$page = Page::make(['title' => 'Homer']);
$page->save();
// global on
config()->set('revisor.publish_on_created', true);
$page = Page::create(['title' => 'Home 2']);
$page->refresh();
expect($page->is_published)->toBeTrue()
->and($page->publishedRecord->title)->toBe($page->title);

// global off + instance on
config()->set('revisor.publish_on_created', false);
$page = Page::make(['title' => 'Home 3']);
$page->publishOnCreated();
$page->save();
$page->refresh();
expect($page->is_published)->toBeTrue()
->and($page->publishedRecord->id)->toBe($page->id)
->and($page->publishedRecord->title)->toBe($page->title);
});

it('publishes a record on save when configured to do so on the model', function () {
$page = Page::make(['title' => 'Homer'])->publishOnSave();
$page->save();
it('publishes on updated only when configured to do so', function () {
$page = Page::create(['title' => 'Home']);
$page->refresh();
expect($page->is_published)->toBeFalse()
->and(Page::withPublishedTable()->find($page->id))->toBeNull();

// global on
config()->set('revisor.publish_on_updated', true);
$page = Page::create(['title' => 'Home 2']);
$page->update(['title' => 'Home 3']);
$page->refresh();
expect($page->is_published)->toBeTrue()
->and($page->publishedRecord->title)->toBe($page->title);

// global off + instance on
config()->set('revisor.publish_on_updated', false);
$page = Page::create(['title' => 'Home 4']);
$page->publishOnUpdated();
$page->update(['title' => 'Home 5']);
$page->refresh();
expect($page->is_published)->toBeTrue()
->and($page->publishedRecord->id)->toBe($page->id)
->and($page->publishedRecord->title)->toBe($page->title);
});

Expand Down
24 changes: 22 additions & 2 deletions tests/VersioningTest.php → tests/HasVersioningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,47 @@
});

it('creates a new version on created only when configured to do so', function () {
// global on
$page = Page::create(['title' => 'Home']);

expect($page->versions()->count())->toBe(1)
->and($page->currentVersion)->toBeInstanceOf(Page::class)
->and($page->currentVersion->title)->toBe('Home');

// global off
config()->set('revisor.record_new_version_on_created', false);
$page = Page::create(['title' => 'About']);
expect($page->versions()->count())->toBe(0);

// global off + instance on
$page = Page::make(['title' => 'Services']);
$page->recordNewVersionOnCreated();
$page->save();
expect($page->versions()->count())->toBe(1);
});

it('creates a new version on updated only when configured to do so', function () {
// global on
$page = Page::create(['title' => 'Home']);
expect($page->versions()->count())->toBe(1);

$page->update(['title' => 'Home 2']);
expect($page->versions()->count())->toBe(2);

// global off
config()->set('revisor.record_new_version_on_updated', false);
$page->update(['title' => 'Home 3']);
expect($page->versions()->count())->toBe(2);

// global off + instance on
$page->recordNewVersionOnUpdated()->update(['title' => 'Home 4']);
expect($page->versions()->count())->toBe(3);
});

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

dd($page->versions()->count());
// dd($page->versions()->count());
});

0 comments on commit aef9f48

Please sign in to comment.