Optimierung 5.3 (Teil 2): Category::localizedName()
Name aus der locale-gefiltert geladenen Uebersetzung mit #id-Fallback als Model-Methode. Ersetzt die identische Inline-Logik an 4 Stellen (Footer-Codes und Kategorien jeweils create/edit). Test: CategoryLocalizedNameTest. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
178adb00e2
commit
abe9a0aa69
6 changed files with 32 additions and 4 deletions
|
|
@ -46,6 +46,16 @@ class Category extends Model
|
||||||
return $this->hasMany(CategoryTranslation::class);
|
return $this->hasMany(CategoryTranslation::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name aus der bereits (locale-gefiltert) geladenen Übersetzung, mit
|
||||||
|
* `#id`-Fallback, falls keine Übersetzung vorliegt. Erwartet eine
|
||||||
|
* eager-geladene `translations`-Relation der gewünschten Sprache.
|
||||||
|
*/
|
||||||
|
public function localizedName(): string
|
||||||
|
{
|
||||||
|
return $this->translations->first()?->name ?? '#'.$this->id;
|
||||||
|
}
|
||||||
|
|
||||||
public function pressReleases(): HasMany
|
public function pressReleases(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(PressRelease::class);
|
return $this->hasMany(PressRelease::class);
|
||||||
|
|
|
||||||
|
|
@ -219,7 +219,7 @@ new #[Layout('components.layouts.app'), Title('Kategorie anlegen')] class extend
|
||||||
<option value="">{{ __('— Keine —') }}</option>
|
<option value="">{{ __('— Keine —') }}</option>
|
||||||
@foreach ($parentOptions as $parent)
|
@foreach ($parentOptions as $parent)
|
||||||
<option value="{{ $parent->id }}">
|
<option value="{{ $parent->id }}">
|
||||||
{{ $parent->translations->first()?->name ?? '#'.$parent->id }}
|
{{ $parent->localizedName() }}
|
||||||
</option>
|
</option>
|
||||||
@endforeach
|
@endforeach
|
||||||
</flux:select>
|
</flux:select>
|
||||||
|
|
|
||||||
|
|
@ -270,7 +270,7 @@ new #[Layout('components.layouts.app'), Title('Kategorie bearbeiten')] class ext
|
||||||
<option value="">{{ __('— Keine —') }}</option>
|
<option value="">{{ __('— Keine —') }}</option>
|
||||||
@foreach ($parentOptions as $parent)
|
@foreach ($parentOptions as $parent)
|
||||||
<option value="{{ $parent->id }}">
|
<option value="{{ $parent->id }}">
|
||||||
{{ $parent->translations->first()?->name ?? '#'.$parent->id }}
|
{{ $parent->localizedName() }}
|
||||||
</option>
|
</option>
|
||||||
@endforeach
|
@endforeach
|
||||||
</flux:select>
|
</flux:select>
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ new #[Layout('components.layouts.app'), Title('Footer-Code anlegen')] class exte
|
||||||
->get()
|
->get()
|
||||||
->map(fn (Category $cat) => [
|
->map(fn (Category $cat) => [
|
||||||
'id' => $cat->id,
|
'id' => $cat->id,
|
||||||
'name' => $cat->translations->first()?->name ?? '#'.$cat->id,
|
'name' => $cat->localizedName(),
|
||||||
'portal' => $cat->portal->value,
|
'portal' => $cat->portal->value,
|
||||||
]),
|
]),
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -99,7 +99,7 @@ new #[Layout('components.layouts.app'), Title('Footer-Code bearbeiten')] class e
|
||||||
->get()
|
->get()
|
||||||
->map(fn (Category $cat) => [
|
->map(fn (Category $cat) => [
|
||||||
'id' => $cat->id,
|
'id' => $cat->id,
|
||||||
'name' => $cat->translations->first()?->name ?? '#'.$cat->id,
|
'name' => $cat->localizedName(),
|
||||||
'portal' => $cat->portal->value,
|
'portal' => $cat->portal->value,
|
||||||
]),
|
]),
|
||||||
];
|
];
|
||||||
|
|
|
||||||
18
tests/Feature/CategoryLocalizedNameTest.php
Normal file
18
tests/Feature/CategoryLocalizedNameTest.php
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Category;
|
||||||
|
|
||||||
|
test('localizedName returns the loaded translation name', function () {
|
||||||
|
$category = Category::factory()->withTranslations()->create();
|
||||||
|
$category->load(['translations' => fn ($query) => $query->where('locale', 'de')]);
|
||||||
|
|
||||||
|
$expected = $category->translations->first()->name;
|
||||||
|
|
||||||
|
expect($category->localizedName())->toBe($expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('localizedName falls back to the id when no translation is loaded', function () {
|
||||||
|
$category = Category::factory()->create();
|
||||||
|
|
||||||
|
expect($category->localizedName())->toBe('#'.$category->id);
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue