Skip to content

Commit

Permalink
Merge pull request #227 from masterix21/feature-skip-generates
Browse files Browse the repository at this point in the history
Add ability to skip the slug generation by a condition
  • Loading branch information
freekmurze authored Mar 28, 2022
2 parents 9b11a8b + 2cdb39f commit e3b102e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
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

0 comments on commit e3b102e

Please sign in to comment.