Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to skip the slug generation by a condition #227

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ $model->slug = 'my-custom-url';
$model->save(); //slug is now "my-custom-url";
```

## Prevents slugs from being generated on some conditions

If you don't want to create the slug when the model has a state, you can use the `skipGenerateWhen` function.

```php
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('name')
->saveSlugsTo('slug')
->skipGenerateWhen(fn () => $this->state === 'draft');
}
```

### Prevent slugs from being generated on creation

If you don't want to create the slug when the model is initially created you can set use the `doNotGenerateSlugsOnCreate()` function.
Expand Down
8 changes: 8 additions & 0 deletions src/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ protected function generateSlugOnCreate(): void
{
$this->slugOptions = $this->getSlugOptions();

if ($this->slugOptions->skipGenerate) {
return;
}

if (! $this->slugOptions->generateSlugsOnCreate) {
return;
}
Expand All @@ -44,6 +48,10 @@ protected function generateSlugOnUpdate(): void
{
$this->slugOptions = $this->getSlugOptions();

if ($this->slugOptions->skipGenerate) {
return;
}

if (! $this->slugOptions->generateSlugsOnUpdate) {
return;
}
Expand Down
9 changes: 9 additions & 0 deletions src/SlugOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class SlugOptions

public int $maximumLength = 250;

public bool $skipGenerate = false;

public bool $generateSlugsOnCreate = true;

public bool $generateSlugsOnUpdate = true;
Expand Down Expand Up @@ -74,6 +76,13 @@ public function slugsShouldBeNoLongerThan(int $maximumLength): self
return $this;
}

public function skipGenerateWhen(callable $callable): self
{
$this->skipGenerate = $callable() === true;

return $this;
}

public function doNotGenerateSlugsOnCreate(): self
{
$this->generateSlugsOnCreate = false;
Expand Down
28 changes: 26 additions & 2 deletions tests/HasSlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,32 @@ public function getSlugOptions(): SlugOptions
expect($model->url)->toEqual('this-is-an-other-1');
});

it('has a method that prevents a slug being generated on condition', function () {
$model = new class () extends TestModel {
public function getSlugOptions(): SlugOptions
{
return parent::getSlugOptions()
->skipGenerateWhen(fn () => $this->name === 'draft');
}
};

$model->name = 'draft';
$model->save();

expect($model->url)->toBeNull();

$model->other_field = 'Spatie';
$model->save();

expect($model->url)->toBeNull();

$model->name = 'this is not a draft';
$model->save();

expect($model->url)->toEqual('this-is-not-a-draft');
});

it('has an method that prevents a slug being generated on creation', function () {
it('has a method that prevents a slug being generated on creation', function () {
$model = new class () extends TestModel {
public function getSlugOptions(): SlugOptions
{
Expand All @@ -171,7 +195,7 @@ public function getSlugOptions(): SlugOptions
expect($model->url)->toBeNull();
});

it('has an method that prevents a slug being generated on update', function () {
it('has a method that prevents a slug being generated on update', function () {
$model = new class () extends TestModel {
public function getSlugOptions(): SlugOptions
{
Expand Down