Skip to content

Commit

Permalink
Merge pull request #19 from oddvalue/feature/preview-mode
Browse files Browse the repository at this point in the history
New feature: preview mode
  • Loading branch information
oddvalue authored Feb 21, 2023
2 parents 706bf0a + f273e29 commit c0796ea
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
+ [Interacting with records](#interacting-with-records)
- [Published revision](#published-revision)
- [Current Revision](#current-revision)
- [Revisions](#revisions)
- [Preview mode](#preview-mode)
* [Testing](#testing)
* [Changelog](#changelog)
* [Contributing](#contributing)
Expand Down Expand Up @@ -247,8 +249,6 @@ To fetch the current revision you can call the `current` scope.
$posts = Post::current()->get();
```

You can implement a preview mode for your frontend by calling the `current` scope when fetching records.

#### Revisions

Every time a record is updated a new row/revision will be inserted. The default number of revisions kept is 10, this can be updated in the published config file.
Expand All @@ -268,6 +268,20 @@ If you need to update a record without creating revision
$post->withoutRevision()->update($options);
```

#### Preview Mode

Enabling preview mode will disable the global scope that fetches only published records and will instead fetch the current revision regardless of published state.

```php
# Enable preview mode
\Oddvalue\LaravelDrafts\Facades\LaravelDrafts::previewMode();
\Oddvalue\LaravelDrafts\Facades\LaravelDrafts::previewMode(true);

# Disable preview mode
\Oddvalue\LaravelDrafts\Facades\LaravelDrafts::disablePreviewMode();
\Oddvalue\LaravelDrafts\Facades\LaravelDrafts::previewMode(false);
```

## Testing

```bash
Expand Down
6 changes: 6 additions & 0 deletions src/Concerns/HasDrafts.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public function initializeHasDrafts()

public static function bootHasDrafts(): void
{
static::addGlobalScope('onlyCurrentInPreviewMode', static function (Builder $builder): void {
if (LaravelDrafts::isPreviewModeEnabled()) {
$builder->current();
}
});

static::creating(function (Model $model): void {
$model->{$model->getIsCurrentColumn()} = true;
$model->setPublisher();
Expand Down
7 changes: 6 additions & 1 deletion src/Facades/LaravelDrafts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static \Illuminate\Contracts\Auth\Authenticatable getCurrentUser()
* @method static void previewMode(bool $previewMode = true)
* @method static void disablePreviewMode()
* @method static bool isPreviewModeEnabled()
*
* @see \Oddvalue\LaravelDrafts\LaravelDrafts
*/
class LaravelDrafts extends Facade
{
protected static function getFacadeAccessor()
{
return 'laravel-drafts';
return \Oddvalue\LaravelDrafts\LaravelDrafts::class;
}
}
19 changes: 18 additions & 1 deletion src/LaravelDrafts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@

namespace Oddvalue\LaravelDrafts;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;

class LaravelDrafts
{
public function getCurrentUser()
public function getCurrentUser(): Authenticatable
{
return Auth::guard(config('drafts.auth.guard'))->user();
}

public function previewMode(bool $previewMode = true): void
{
Session::put('drafts.preview', $previewMode);
}

public function disablePreviewMode(): void
{
Session::forget('drafts.preview');
}

public function isPreviewModeEnabled(): bool
{
return Session::get('drafts.preview', false);
}
}
4 changes: 4 additions & 0 deletions src/Scopes/PublishingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Oddvalue\LaravelDrafts\Facades\LaravelDrafts;

class PublishingScope implements Scope
{
Expand All @@ -17,6 +18,9 @@ class PublishingScope implements Scope

public function apply(Builder $builder, Model $model): void
{
if (LaravelDrafts::isPreviewModeEnabled()) {
return;
}
$builder->where($model->getQualifiedIsPublishedColumn(), 1);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/PreviewModeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Oddvalue\LaravelDrafts\Facades\LaravelDrafts;
use Oddvalue\LaravelDrafts\Tests\Post;

it('can enable preview mode', function () {
LaravelDrafts::previewMode();
expect(LaravelDrafts::isPreviewModeEnabled())->toBeTrue();
});

it('can disable preview mode', function () {
LaravelDrafts::previewMode();
expect(LaravelDrafts::isPreviewModeEnabled())->toBeTrue();
LaravelDrafts::disablePreviewMode();
expect(LaravelDrafts::isPreviewModeEnabled())->toBeFalse();
});

it('gets the current draft when preview mode is enabled', function () {
$post = Post::factory()->create(['title' => 'Foo']);
$post->updateAsDraft(['title' => 'Bar']);
$post->updateAsDraft(['title' => 'Baz']);
$post->updateAsDraft(['title' => 'Qux']);

expect(Post::query()->where('uuid', $post->uuid)->first()->title)->toBe('Foo');
LaravelDrafts::previewMode();
expect(Post::query()->where('uuid', $post->uuid)->first()->title)->toBe('Qux');
});

0 comments on commit c0796ea

Please sign in to comment.