Skip to content

Commit

Permalink
simlify slug field
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jan 24, 2024
1 parent 95d2707 commit 449060f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
2 changes: 1 addition & 1 deletion resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{{-- Content --}}
@section('content')
<div class="l-row l-row--column:sm:2 l-row--column:lg:3">
<div class="l-row">
@foreach($widgets as $widget)
@include($widget['template'], $widget)
@endforeach
Expand Down
8 changes: 5 additions & 3 deletions resources/views/widgets/welcome.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div class="card">
<h2 class="card__title">{{ __('Welcome') }}</h2>
<div class="card__inner">
<div class="app-card">
<div class="app-card__header">
<h2 class="app-card__title">{{ __('Welcome') }}</h2>
</div>
<div class="app-card__body">
<p>{{ __('This is the Root Dashboard.') }}</p>
</div>
</div>
36 changes: 23 additions & 13 deletions src/Fields/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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}
*/
Expand Down Expand Up @@ -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();
}

Expand All @@ -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.
*/
Expand Down Expand Up @@ -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);
Expand Down
50 changes: 50 additions & 0 deletions tests/Fields/SlugTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Cone\Root\Tests\Fields;

use Cone\Root\Fields\Slug;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

class SlugTest extends TestCase
{
protected Slug $field;

public function setUp(): void
{
parent::setUp();

$this->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' => '[email protected]',
'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);
}
}

0 comments on commit 449060f

Please sign in to comment.