From 449060f7e26d60e02dcd80b2fec7f92a150d102d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Wed, 24 Jan 2024 10:57:07 +0100 Subject: [PATCH] simlify slug field --- resources/views/dashboard.blade.php | 2 +- resources/views/widgets/welcome.blade.php | 8 ++-- src/Fields/Slug.php | 36 ++++++++++------ tests/Fields/SlugTest.php | 50 +++++++++++++++++++++++ 4 files changed, 79 insertions(+), 17 deletions(-) create mode 100644 tests/Fields/SlugTest.php diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index dfd6f0fa8..6df83d943 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -5,7 +5,7 @@ {{-- Content --}} @section('content') -
+
@foreach($widgets as $widget) @include($widget['template'], $widget) @endforeach diff --git a/resources/views/widgets/welcome.blade.php b/resources/views/widgets/welcome.blade.php index a75ff3543..791361cd7 100644 --- a/resources/views/widgets/welcome.blade.php +++ b/resources/views/widgets/welcome.blade.php @@ -1,6 +1,8 @@ -
-

{{ __('Welcome') }}

-
+
+
+

{{ __('Welcome') }}

+
+

{{ __('This is the Root Dashboard.') }}

diff --git a/src/Fields/Slug.php b/src/Fields/Slug.php index 53b9fecef..8d8abf0f4 100644 --- a/src/Fields/Slug.php +++ b/src/Fields/Slug.php @@ -45,12 +45,11 @@ class Slug extends Text /** * Create a new field instance. */ - public function __construct(?string $label = null, Closure|string|null $modelAttribute = null) + public function __construct(?string $label = null, Closure|string $modelAttribute = 'slug') { - parent::__construct($label ?: __('Slug'), $modelAttribute ?: 'slug'); + parent::__construct($label ?: __('Slug'), $modelAttribute); $this->readonly(); - $this->unique(); } @@ -64,6 +63,14 @@ public function nullable(bool $value = true): static return $this; } + /** + * Determine if the field is nullable. + */ + public function isNullable(): bool + { + return $this->nullable; + } + /** * {@inheritdoc} */ @@ -91,7 +98,7 @@ public function getValueForHydrate(Request $request): mixed { $value = parent::getValueForHydrate($request); - if (! $this->nullable && empty($value)) { + if (! $this->isNullable() && empty($value)) { $value = Str::random(); } @@ -108,6 +115,16 @@ public function from(array|string $attributes): static return $this; } + /** + * Set the "separator" property. + */ + public function separator(string $value): static + { + $this->separator = $value; + + return $this; + } + /** * Set the "unique" property. */ @@ -157,16 +174,9 @@ protected function generate(Request $request, Model $model): string ->newQuery() ->when( in_array(SoftDeletes::class, class_uses_recursive($model)), - static function (Builder $query): Builder { - return $query->withTrashed(); - } + static fn (Builder $query): Builder => $query->withTrashed() ) - ->whereRaw(sprintf( - "`%s` regexp '^%s(%s[\\\\d]+)?$'", - $this->modelAttribute, - preg_quote($value), - preg_quote($this->separator) - )) + ->where($this->modelAttribute, 'like', $value.'%') ->orderByDesc($this->modelAttribute) ->limit(1) ->value($this->modelAttribute); diff --git a/tests/Fields/SlugTest.php b/tests/Fields/SlugTest.php new file mode 100644 index 000000000..5f6d62c03 --- /dev/null +++ b/tests/Fields/SlugTest.php @@ -0,0 +1,50 @@ +field = new Slug('Slug', 'name'); + } + + public function test_a_slug_field_has_slug_template(): void + { + $this->assertSame('root::fields.slug', $this->field->getTemplate()); + } + + public function test_a_slug_field_can_be_nullable(): void + { + $this->assertFalse($this->field->isNullable()); + $this->field->nullable(); + $this->assertTrue($this->field->isNullable()); + } + + public function test_a_slug_field_generates_value_from_model_attributes(): void + { + $user = new User([ + 'email' => 'test@foo.com', + 'name' => 'Test', + 'password' => 'secret', + ]); + + $this->field->from('email')->unique()->separator('_'); + + $this->field->persist( + $this->app['request'], $user, $this->field->getValueForHydrate($this->app['request']) + ); + + $user->save(); + + $this->assertSame('test_at_foocom', $user->name); + } +}