Skip to content

Commit f8ce8c6

Browse files
committed
refactoring the TableCell Url generator
1 parent 26b510f commit f8ce8c6

File tree

7 files changed

+46
-32
lines changed

7 files changed

+46
-32
lines changed

src/Contracts/Translatable.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Casts\Attribute;
66
use Javaabu\Translatable\Exceptions\LanguageNotAllowedException;
7+
use Javaabu\Translatable\Models\Language;
78

89
interface Translatable
910
{
@@ -114,9 +115,9 @@ public function deleteTranslation(string $locale): void;
114115
*/
115116
public function localeDirection(): Attribute;
116117

117-
public function getRouteName(): ?string;
118+
public function languageSwitcherRouteName(): ?string;
118119

119-
public function getRouteParams(): array;
120+
public function url(string $action = 'show', Language|string|null $locale = null, ?string $portal = null): string;
120121

121-
public function languageSwitcherRouteName(): ?string;
122+
public function routeKeyForPortal(?string $portal): string;
122123
}

src/Traits/IsTranslatable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
trait IsTranslatable
1616
{
17+
use TranslatedUrlGenerator;
18+
1719
private bool $skipTranslation = false;
1820

1921
public function getNonTranslatablePivots(): array
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Javaabu\Translatable\Traits;
4+
5+
use Javaabu\Translatable\Models\Language;
6+
use Request;
7+
8+
trait TranslatedUrlGenerator
9+
{
10+
public function url(string $action = 'show', Language|string|null $locale = null, ?string $portal = null): string
11+
{
12+
if ( ! $portal) {
13+
$portal = Request::portal();
14+
}
15+
16+
// Figure out route name based on namespace and action
17+
$model_route_name = str(class_basename($this))->plural()->snake('-')->toString();
18+
19+
$route_name = $portal ? "{$portal}.{$model_route_name}.{$action}" : "{$model_route_name}.{$action}";
20+
21+
$params = [];
22+
23+
if ( ! in_array($action, ['index', 'store', 'create', 'trash'])) {
24+
$params[] = $this->routeKeyForPortal($portal);
25+
}
26+
27+
return translate_route(name: $route_name, parameters: $params, locale: $locale);
28+
}
29+
30+
public function routeKeyForPortal(?string $portal): string
31+
{
32+
return (string) $this->id;
33+
}
34+
}

src/TranslatableServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function boot(): void
6565
return redirect()->to($url, $status, $headers);
6666
});
6767

68-
Request::macro('portal', function (): string {
68+
Request::macro('portal', function (): ?string {
6969
$req = request();
7070
$adminDomain = config('app.admin_domain'); // e.g. 'admin.ncs.test' or null
7171
$adminPrefix = config('app.admin_prefix', 'admin'); // e.g. 'admin'
@@ -81,7 +81,7 @@ public function boot(): void
8181
}
8282

8383
// pattern: ncs.test/{locale}/{portal}
84-
return $req->segment(2) ?? 'web';
84+
return $req->segment(2) ?? null;
8585
});
8686

8787
Request::macro('isPortal', function (string $portal): bool {

src/Views/Components/TableCells.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,20 @@ class TableCells extends Component
1515

1616
public function __construct(
1717
public Translatable $model,
18-
public string $route_name = '',
19-
public array $route_params = [],
20-
public string $create_url = '',
2118
) {
2219
$this->languages = Languages::allExceptCurrent();
2320
}
2421

2522
public function getUrl(string $action, string $locale): string
2623
{
27-
$params = filled($this->route_params) ? $this->route_params : $this->model->getRouteParams();
28-
29-
// A big assumption here is that the last parameter in the route params is the model ID.
30-
// If the action is 'create', we don't need the last parameter (usually the ID)
31-
if ($action === 'create') {
32-
$params = array_slice($params, 0, -1); // Remove the last parameter if it exists
33-
}
24+
$url = $this->model->url($action, $locale);
3425

3526
// If the model is we need to add "lang_parent" to the params
3627
if ($this->isModelDbTranslatable()) {
37-
$params['lang_parent'] = $this->model->id;
28+
$url = add_query_arg('lang_parent', $this->model->id, $url);
3829
}
3930

40-
return translate_route(
41-
filled($this->route_name) ? $this->route_name : $this->model->getRouteName() . '.' . $action,
42-
$params,
43-
locale: $locale,
44-
);
31+
return $url;
4532
}
4633

4734
public function isModelDbTranslatable(): bool

tests/TestSupport/Models/Article.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,4 @@ public function getNonTranslatablePivots(): array
4747
'author_id',
4848
];
4949
}
50-
51-
public function getRouteName(): string
52-
{
53-
return 'articles';
54-
}
55-
56-
public function getRouteParams(): array
57-
{
58-
return ['article' => $this->id];
59-
}
6050
}

tests/Unit/TableCellTranslatedUrlGenerationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function it_can_generate_show_url_for_json_translatable_record(): void
6666
]);
6767

6868
Languages::setCurrentLocale('en');
69-
$tableCellComponent = new TableCells($article, create_url: 'articles/create');
69+
$tableCellComponent = new TableCells($article);
7070

7171
$en_url = $tableCellComponent->getUrl('show', 'en');
7272
$this->assertTrue(str($en_url)->contains('/en/articles/' . $article->id), 'English URL does not contain expected path.');

0 commit comments

Comments
 (0)