Skip to content

Commit

Permalink
Remove breaking change by introducing $slugDeps and $slugFields with …
Browse files Browse the repository at this point in the history
…fallback to $slugAttributes
  • Loading branch information
Tofandel committed Nov 20, 2024
1 parent 0179d11 commit c0a4396
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class Page extends Model
'description',
];
public $slugAttributes = [
protected $slugFields = [
'title',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Article extends Model implements LocalizedUrlRoutable
'description',
];

public $slugAttributes = [
protected $slugFields = [
'title',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Article extends Model implements Sortable

// #endregion fillable

public $slugAttributes = [
private $slugFields = [
'title',
];

Expand Down
2 changes: 1 addition & 1 deletion examples/basic-page-builder/app/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Page extends Model
'description',
];

public $slugAttributes = [
protected $slugFields = [
'title',
];
}
2 changes: 1 addition & 1 deletion examples/portfolio/app/Models/Partner.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Partner extends Model
'description',
];

public $slugAttributes = [
protected $slugFields = [
'title',
];

Expand Down
2 changes: 1 addition & 1 deletion examples/portfolio/app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Project extends Model
'description',
];

public $slugAttributes = [
protected $slugFields = [
'title',
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Post extends Model implements Sortable

public $translatedAttributes = ['title', 'description'];

public $slugAttributes = ['title'];
public $slugFields = ['title'];

public $mediasParams = [
'cover' => [
Expand Down
2 changes: 1 addition & 1 deletion examples/tests-modules/app/Models/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Author extends Model implements Sortable
public $translatedAttributes = ['name', 'description', 'bio'];

// uncomment and modify this as needed if you use the HasSlug trait
public $slugAttributes = ['name'];
protected $slugFields = ['name'];

// add checkbox fields names here (published toggle is itself a checkbox)
public $checkboxes = ['published'];
Expand Down
2 changes: 1 addition & 1 deletion examples/tests-modules/app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Category extends Model
public $translatedAttributes = ['title'];

// uncomment and modify this as needed if you use the HasSlug trait
public $slugAttributes = ['title'];
protected $slugFields = ['title'];

// add checkbox fields names here (published toggle is itself a checkbox)
public $checkboxes = ['published'];
Expand Down
2 changes: 1 addition & 1 deletion examples/tests-singleton/app/Models/ContactPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ContactPage extends Model
'description',
];

public $slugAttributes = [
protected $slugFields = [
'title',
];

Expand Down
2 changes: 1 addition & 1 deletion examples/tests-subdomain-routing/app/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Page extends Model implements Sortable
'description',
];

public $slugAttributes = [
public $slugFields = [
'title',
];

Expand Down
3 changes: 2 additions & 1 deletion src/Commands/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class {{modelClassName}} extends Model {{modelImplements}}
'description',
];
{{/hasTranslation}}{{hasSlug}}
public $slugAttributes = [
protected $slugFields = [
'title',
];
protected $slugDeps = [];
{{/hasSlug}}
}
26 changes: 18 additions & 8 deletions src/Models/Behaviors/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ public function updateOrNewSlug(array $slugParams): void
// The relation was loaded before the old slug even existed, unload it and let it lazy reload as needed
$this->unsetRelation('slugs');
}
var_dump('updated old', $this->slugs);
}
if ($isNowActive) {
$this->disableLocaleSlugs($oldMatchingSlug->locale, $oldMatchingSlug->getKey());
Expand Down Expand Up @@ -257,7 +256,6 @@ public function disableLocaleSlugs(string|array $locale = null, int|array $excep
$slug->active = false;
$slug->syncOriginalAttribute('active');
});
var_dump('disabled', $this->slugs);
}
}

Expand Down Expand Up @@ -360,13 +358,25 @@ public function getSlugAttribute(): string
return $this->getSlug();
}

public function getSlugDeps(): array
{
return $this->slugDeps ?? array_slice($this->slugAttributes ?? [], 1);
}
public function getSlugFields(): array
{
if (!isset($this->slugFields) && isset($this->slugAttributes)) {
trigger_deprecation('area17/twill', '3.5', 'The slugAttributes property has been deprecated instead define slug fields with the slugFields property and additional columns with the slugDeps property');
}
return $this->slugFields ?? array_slice($this->slugAttributes ?? [], 0, 1);
}

public function getSlugParams(?string $locale = null, bool $skipUnchanged = false): ?array
{
$translatedAttributes = $this->getTranslatedAttributes();
$slugParams = [];
$attributes = $this->slugAttributes ?? [];
$deps = $this->slugDependencies ?? [];
if (empty($attributes)) {
$deps = $this->getSlugDeps();
$fields = $this->getSlugFields();
if (empty($fields)) {
return null;
}
foreach (getLocales() as $appLocale) {
Expand Down Expand Up @@ -395,9 +405,9 @@ public function getSlugParams(?string $locale = null, bool $skipUnchanged = fals
return $this->$attribute;
};

$slugAttributes = array_filter(array_map($getAttributeValue, $attributes));
$slugFields = array_filter(array_map($getAttributeValue, $fields));

if (empty($slugAttributes)) {
if (empty($slugFields)) {
// Skip empty slugs
continue;
}
Expand All @@ -408,7 +418,7 @@ public function getSlugParams(?string $locale = null, bool $skipUnchanged = fals
}
$slugParam = [
'active' => $translation?->active ?? true,
'slug' => Arr::join($slugAttributes, '-'),
'slug' => Arr::join($slugFields, '-'),
'locale' => $appLocale,
] + $slugDependencies;

Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/Behaviors/HandleSlugs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait HandleSlugs
{
public function beforeSaveHandleSlugs(TwillModelContract $object, array $fields): void
{
if (property_exists($this->model, 'slugAttributes')) {
if (method_exists($this->model, 'getSlugFields')) {
$object->twillSlugData = [];
$submittedLanguages = Collection::make($fields['languages'] ?? []);

Expand Down
34 changes: 34 additions & 0 deletions tests/integration/Anonymous/AnonymousModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class AnonymousModule
/** @var string[] */
private array $slugAttributes = [];

/** @var string[] */
private array $slugFields = [];

/** @var string[] */
private array $slugDeps = [];

protected function __construct(public string $namePlural, public Application $app)
{
$this->classPrinter = new PsrPrinter();
Expand Down Expand Up @@ -203,6 +209,22 @@ public function withSlugAttributes(array $fields): self
return $this;
}


public function withSlugFields(array $fields): self
{
$this->slugFields = $fields;

return $this;
}

public function withSlugDeps(array $fields): self
{
$this->slugDeps = $fields;

return $this;
}


/**
* Boots the anonymous module and returns the model class.
*/
Expand Down Expand Up @@ -626,6 +648,18 @@ private function getModelClass(string $classNameWithNamespace): PhpNamespace
$this->slugAttributes
);
}
if ($this->slugDeps !== []) {
$class->addProperty(
'slugDeps',
$this->slugDeps
);
}
if ($this->slugFields !== []) {
$class->addProperty(
'slugFields',
$this->slugFields
);
}

foreach ($this->belongsToMany as $name => $target) {
$method = $class->addMethod($name);
Expand Down
6 changes: 1 addition & 5 deletions tests/integration/SlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
use A17\Twill\Models\Behaviors\HasSlug;
use A17\Twill\Models\Model;
use A17\Twill\Tests\Integration\Anonymous\AnonymousModule;
use App\Models\Author;
use App\Repositories\AuthorRepository;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

class SlugTest extends TestCase
{
Expand Down Expand Up @@ -36,7 +32,7 @@ public function testMultipleSlugAttributes()
'first_name' => [],
'last_name' => [],
])
->withSlugAttributes([
->withSlugFields([
'last_name', 'first_name'
])
->boot();
Expand Down

0 comments on commit c0a4396

Please sign in to comment.