Skip to content

Commit da24fe8

Browse files
committed
Merge branch 'main' into feature/scheduled-drafts
2 parents d4141de + 16fa512 commit da24fe8

File tree

8 files changed

+39
-34
lines changed

8 files changed

+39
-34
lines changed

.github/workflows/dependabot-auto-merge.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
- name: Dependabot metadata
1515
id: metadata
16-
uses: dependabot/[email protected].3
16+
uses: dependabot/[email protected].6
1717
with:
1818
github-token: "${{ secrets.GITHUB_TOKEN }}"
1919

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
coverage: none
2121

2222
- name: Install composer dependencies
23-
uses: ramsey/composer-install@v1
23+
uses: ramsey/composer-install@v2
2424

2525
- name: Run PHPStan
2626
run: ./vendor/bin/phpstan --error-format=github

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# A simple, drop-in drafts/revisions system for Laravel models
66

77
[![Latest Version on Packagist](https://img.shields.io/packagist/v/oddvalue/laravel-drafts.svg?style=flat-square)](https://packagist.org/packages/oddvalue/laravel-drafts)
8-
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/oddvalue/laravel-drafts/run-tests?label=tests)](https://github.com/oddvalue/laravel-drafts/actions?query=workflow%3Arun-tests+branch%3Amain)
9-
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/oddvalue/laravel-drafts/Check%20&%20fix%20styling?label=code%20style)](https://github.com/oddvalue/laravel-drafts/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
8+
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/oddvalue/laravel-drafts/run-tests.yml?label=tests&style=flat-square)](https://github.com/oddvalue/laravel-drafts/actions?query=workflow%3Arun-tests+branch%3Amain)
9+
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/oddvalue/laravel-drafts/php-cs-fixer.yml?label=code%20style&style=flat-square)](https://github.com/oddvalue/laravel-drafts/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
1010
[![Total Downloads](https://img.shields.io/packagist/dt/oddvalue/laravel-drafts.svg?style=flat-square)](https://packagist.org/packages/oddvalue/laravel-drafts)
1111

1212
* [Installation](#installation)
@@ -39,7 +39,7 @@ composer require oddvalue/laravel-drafts
3939
You can publish the config file with:
4040

4141
```bash
42-
php artisan vendor:publish --tag="laravel-drafts-config"
42+
php artisan vendor:publish --tag="drafts-config"
4343
```
4444

4545
This is the contents of the published config file:

SECURITY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Security Policy
2+
3+
## Reporting a Vulnerability
4+
5+
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

database/migrations/create_drafts_table.php.stub

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Concerns/HasDrafts.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
use Illuminate\Support\Str;
1515
use Oddvalue\LaravelDrafts\Facades\LaravelDrafts;
1616

17+
/**
18+
* @method void Current(Builder $query)
19+
* @method void WithoutCurrent(Builder $query)
20+
* @method void ExcludeRevision(Builder $query, int | Model $exclude)
21+
*/
1722
trait HasDrafts
1823
{
1924
use Publishes;
@@ -87,7 +92,7 @@ protected function newRevision(): void
8792
return;
8893
}
8994

90-
$revision = $this->fresh()->replicate();
95+
$revision = $this->fresh()?->replicate();
9196

9297
static::saved(function () use ($revision) {
9398
$revision->created_at = $this->created_at;
@@ -119,7 +124,7 @@ public function generateUuid(): void
119124

120125
public function setCurrent(): void
121126
{
122-
$oldCurrent = $this->revisions()->withDrafts()->current()->withoutSelf()->first();
127+
$oldCurrent = $this->revisions()->withDrafts()->current()->excludeRevision($this)->first();
123128

124129
static::saved(function () use ($oldCurrent) {
125130
if ($oldCurrent) {
@@ -134,7 +139,7 @@ public function setCurrent(): void
134139

135140
public function setLive(): void
136141
{
137-
$published = $this->revisions()->withoutSelf()->published()->first();
142+
$published = $this->revisions()->excludeRevision($this)->published()->first();
138143

139144
if (! $published) {
140145
$this->{$this->getPublishedAtColumn()} ??= now();
@@ -296,7 +301,7 @@ public function setPublisher(): static
296301

297302
public function pruneRevisions(): void
298303
{
299-
$this->withoutEvents(function () {
304+
self::withoutEvents(function () {
300305
$revisionsToKeep = $this->revisions()
301306
->orderByDesc('updated_at')
302307
->onlyDrafts()
@@ -400,7 +405,15 @@ public function scopeWithoutCurrent(Builder $query): void
400405
$query->where($this->getIsCurrentColumn(), false);
401406
}
402407

403-
public function scopeWithoutSelf(Builder $query)
408+
public function scopeExcludeRevision(Builder $query, int | Model $exclude): void
409+
{
410+
$query->where($this->getKeyName(), '!=', is_int($exclude) ? $exclude : $exclude->getKey());
411+
}
412+
413+
/**
414+
* @deprecated This doesn't actually work, will be removed in next version
415+
*/
416+
public function scopeWithoutSelf(Builder $query): void
404417
{
405418
$query->where('id', '!=', $this->id);
406419
}

tests/DraftsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\DB;
34
use Oddvalue\LaravelDrafts\Tests\Post;
45

56
use function Spatie\PestPluginTestTime\testTime;
@@ -124,3 +125,13 @@
124125

125126
expect(Post::find($originalId)->title)->toBe('Qux');
126127
});
128+
129+
it('can get all revisions excluding self', function (): void {
130+
$post = Post::factory()->create(['title' => 'Foo']);
131+
$post->updateAsDraft(['title' => 'Bar']);
132+
$post->updateAsDraft(['title' => 'Baz']);
133+
$post->updateAsDraft(['title' => 'Qux']);
134+
135+
expect($post->revisions()->excludeRevision($post)->pluck('id'))
136+
->not()->toContain($post->id);
137+
});

tests/TestCase.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ protected function getPackageProviders($app)
3535
public function getEnvironmentSetUp($app)
3636
{
3737
config()->set('database.default', 'testing');
38-
39-
/*
40-
$migration = include __DIR__.'/../database/migrations/create_laravel-drafts_table.php.stub';
41-
$migration->up();
42-
*/
4338
}
4439

4540
/**

0 commit comments

Comments
 (0)