Skip to content

Commit 40cc2fe

Browse files
author
ankitcodes4u
committed
refactor: implement feedback and clean up tests
1 parent d7de82a commit 40cc2fe

30 files changed

Lines changed: 521 additions & 1334 deletions

File tree

config/eclipse-cms.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
|--------------------------------------------------------------------------
99
*/
1010
'tenancy' => [
11-
'enabled' => true,
12-
'model' => 'Eclipse\\Core\\Models\\Site',
13-
'foreign_key' => 'site_id',
11+
'enabled' => false,
12+
'model' => null,
13+
'foreign_key' => null,
1414
],
1515
];
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Eclipse\Cms\Factories;
44

55
use Eclipse\Cms\Enums\PageStatus;
6-
use Eclipse\Cms\Enums\SectionType;
76
use Eclipse\Cms\Models\Page;
87
use Eclipse\Cms\Models\Section;
98
use Illuminate\Database\Eloquent\Factories\Factory;
@@ -45,13 +44,21 @@ public function definition(): array
4544
],
4645
'code' => $this->faker->unique()->word(),
4746
'status' => $this->faker->randomElement([PageStatus::Draft, PageStatus::Published]),
48-
'type' => SectionType::Pages,
47+
'type' => 'page',
4948
'created_at' => Carbon::now(),
5049
'updated_at' => Carbon::now(),
51-
'section_id' => Section::factory(),
5250
];
5351
}
5452

53+
public function configure()
54+
{
55+
return $this->afterMaking(function (Page $page) {
56+
if (! $page->section_id) {
57+
$page->section_id = Section::factory()->create()->id;
58+
}
59+
});
60+
}
61+
5562
public function forSection($section): static
5663
{
5764
return $this->state([
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,29 @@ public function definition(): array
3131
public function configure()
3232
{
3333
return $this->afterMaking(function (Section $section) {
34-
$foreignKey = config('eclipse-cms.tenancy.foreign_key');
35-
$currentValue = $section->getAttribute($foreignKey);
36-
37-
if (config('eclipse-cms.tenancy.enabled') &&
38-
(! $currentValue || $currentValue === null)) {
39-
$class = config('eclipse-cms.tenancy.model');
40-
$newValue = $class::inRandomOrder()->first()?->id ?? $class::factory()->create()->id;
41-
$section->setAttribute($foreignKey, $newValue);
34+
if (config('eclipse-cms.tenancy.enabled')) {
35+
$foreignKey = config('eclipse-cms.tenancy.foreign_key');
36+
$currentValue = $section->getAttribute($foreignKey);
37+
38+
if (! $currentValue || $currentValue === null) {
39+
$class = config('eclipse-cms.tenancy.model');
40+
if (class_exists($class)) {
41+
$newValue = $class::inRandomOrder()->first()?->id ?? $class::factory()->create()->id;
42+
$section->setAttribute($foreignKey, $newValue);
43+
}
44+
}
4245
}
4346
});
4447
}
4548

4649
public function forSite($site): static
4750
{
48-
return $this->state([
49-
config('eclipse-cms.tenancy.foreign_key') => $site->id,
50-
]);
51+
if (config('eclipse-cms.tenancy.enabled')) {
52+
return $this->state([
53+
config('eclipse-cms.tenancy.foreign_key') => $site->id,
54+
]);
55+
}
56+
57+
return $this;
5158
}
5259
}

database/migrations/2025_07_22_082135_create_sections_table.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ public function up(): void
1414

1515
if (config('eclipse-cms.tenancy.enabled')) {
1616
$tenantClass = config('eclipse-cms.tenancy.model');
17-
if (class_exists($tenantClass)) {
18-
$tenant = new $tenantClass;
19-
$table->foreignId(config('eclipse-cms.tenancy.foreign_key'))
20-
->constrained($tenant->getTable(), $tenant->getKeyName())
21-
->cascadeOnUpdate()
22-
->cascadeOnDelete();
23-
} else {
24-
$table->unsignedBigInteger(config('eclipse-cms.tenancy.foreign_key'))->nullable();
25-
}
17+
/** @var \Illuminate\Database\Eloquent\Model $tenant */
18+
$tenant = new $tenantClass;
19+
$table->foreignId(config('eclipse-cms.tenancy.foreign_key'))
20+
->constrained($tenant->getTable(), $tenant->getKeyName())
21+
->cascadeOnUpdate()
22+
->cascadeOnDelete();
2623
}
2724

2825
$table->string('name');

database/seeders/CmsSeeder.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,21 @@
44

55
use Eclipse\Cms\Models\Page;
66
use Eclipse\Cms\Models\Section;
7-
use Eclipse\Core\Models\Site;
87
use Illuminate\Database\Seeder;
98

109
class CmsSeeder extends Seeder
1110
{
1211
public function run(): void
1312
{
14-
$sites = Site::all();
13+
$sections = Section::factory()
14+
->count(3)
15+
->create();
1516

16-
if ($sites->isEmpty()) {
17-
$sites = collect([Site::factory()->create()]);
18-
}
19-
20-
foreach ($sites as $site) {
21-
$sections = Section::factory()
17+
$sections->each(function (Section $section): void {
18+
Page::factory()
2219
->count(3)
23-
->forSite($site)
24-
->create([
25-
'name' => [
26-
'en' => 'Information',
27-
'sl' => 'Informacije',
28-
],
29-
]);
30-
31-
$sections->each(function (Section $section) {
32-
Page::factory()
33-
->count(3)
34-
->forSection($section)
35-
->create();
36-
});
37-
}
20+
->forSection($section)
21+
->create();
22+
});
3823
}
3924
}

src/Filament/Resources/PageResource.php renamed to src/Admin/Filament/Resources/PageResource.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Eclipse\Cms\Filament\Resources;
3+
namespace Eclipse\Cms\Admin\Filament\Resources;
44

5+
use Eclipse\Cms\Admin\Filament\Resources\PageResource\Pages;
56
use Eclipse\Cms\Enums\PageStatus;
6-
use Eclipse\Cms\Filament\Resources\PageResource\Pages;
77
use Eclipse\Cms\Models\Page;
88
use Filament\Forms\Components\Hidden;
99
use Filament\Forms\Components\Placeholder;
@@ -61,15 +61,17 @@ public static function form(Form $form): Form
6161
->live(onBlur: true)
6262
->afterStateUpdated(function (string $operation, $state, $set) {
6363
if ($operation === 'create' && $state) {
64-
$set('sef_key', Str::slug($state));
64+
$slug = is_array($state) ? ($state['en'] ?? '') : $state;
65+
if ($slug) {
66+
$set('sef_key', Str::slug($slug));
67+
}
6568
}
6669
})
6770
->columnSpan(2),
6871

6972
Select::make('section_id')
7073
->label('Section')
7174
->relationship('section', 'name')
72-
->required()
7375
->searchable()
7476
->preload()
7577
->native(false)

src/Filament/Resources/PageResource/Pages/CreatePage.php renamed to src/Admin/Filament/Resources/PageResource/Pages/CreatePage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Eclipse\Cms\Filament\Resources\PageResource\Pages;
3+
namespace Eclipse\Cms\Admin\Filament\Resources\PageResource\Pages;
44

5-
use Eclipse\Cms\Filament\Resources\PageResource;
5+
use Eclipse\Cms\Admin\Filament\Resources\PageResource;
66
use Filament\Actions;
77
use Filament\Resources\Pages\CreateRecord;
88

src/Filament/Resources/PageResource/Pages/EditPage.php renamed to src/Admin/Filament/Resources/PageResource/Pages/EditPage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Eclipse\Cms\Filament\Resources\PageResource\Pages;
3+
namespace Eclipse\Cms\Admin\Filament\Resources\PageResource\Pages;
44

5-
use Eclipse\Cms\Filament\Resources\PageResource;
5+
use Eclipse\Cms\Admin\Filament\Resources\PageResource;
66
use Filament\Actions;
77
use Filament\Resources\Pages\EditRecord;
88

src/Filament/Resources/PageResource/Pages/ListPages.php renamed to src/Admin/Filament/Resources/PageResource/Pages/ListPages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Eclipse\Cms\Filament\Resources\PageResource\Pages;
3+
namespace Eclipse\Cms\Admin\Filament\Resources\PageResource\Pages;
44

5-
use Eclipse\Cms\Filament\Resources\PageResource;
5+
use Eclipse\Cms\Admin\Filament\Resources\PageResource;
66
use Eclipse\Common\Foundation\Pages\HasScoutSearch;
77
use Filament\Actions\CreateAction;
88
use Filament\Resources\Pages\ListRecords;

src/Filament/Resources/SectionResource.php renamed to src/Admin/Filament/Resources/SectionResource.php

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace Eclipse\Cms\Filament\Resources;
3+
namespace Eclipse\Cms\Admin\Filament\Resources;
44

5+
use Eclipse\Cms\Admin\Filament\Resources\SectionResource\Pages;
6+
use Eclipse\Cms\Admin\Filament\Resources\SectionResource\RelationManagers;
57
use Eclipse\Cms\Enums\SectionType;
6-
use Eclipse\Cms\Filament\Resources\SectionResource\Pages;
7-
use Eclipse\Cms\Filament\Resources\SectionResource\RelationManagers;
88
use Eclipse\Cms\Models\Section;
99
use Filament\Forms\Components\Placeholder;
1010
use Filament\Forms\Components\Section as FormSection;
@@ -22,10 +22,10 @@
2222
use Filament\Tables\Actions\RestoreAction;
2323
use Filament\Tables\Actions\RestoreBulkAction;
2424
use Filament\Tables\Columns\TextColumn;
25+
use Filament\Tables\Filters\SelectFilter;
2526
use Filament\Tables\Filters\TrashedFilter;
2627
use Filament\Tables\Table;
2728
use Illuminate\Database\Eloquent\Builder;
28-
use Illuminate\Database\Eloquent\Model;
2929
use Illuminate\Database\Eloquent\SoftDeletingScope;
3030

3131
class SectionResource extends Resource
@@ -36,19 +36,19 @@ class SectionResource extends Resource
3636

3737
protected static ?string $slug = 'cms/sections';
3838

39-
protected static ?string $navigationGroup = 'CMS';
39+
protected static ?string $navigationIcon = 'heroicon-o-folder';
4040

41-
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
41+
protected static ?string $navigationGroup = 'CMS';
4242

43-
protected static ?string $recordTitleAttribute = 'name';
43+
protected static bool $shouldRegisterNavigation = true;
4444

4545
protected static ?string $navigationLabel = 'Sections';
4646

4747
public static function form(Form $form): Form
4848
{
4949
return $form
5050
->schema([
51-
FormSection::make()
51+
FormSection::make('Basic Information')
5252
->schema([
5353
TextInput::make('name')
5454
->label('Section Name')
@@ -79,7 +79,7 @@ public static function form(Form $form): Form
7979

8080
Placeholder::make('pages_count')
8181
->label('Total Pages')
82-
->content(fn (?Section $record): string => $record ? $record->pages()->count().' pages' : '-'),
82+
->content(fn (?Section $record): string => $record?->pages()->count().' pages' ?? '-'),
8383
])
8484
->columns(3)
8585
->compact()
@@ -105,8 +105,8 @@ public static function table(Table $table): Table
105105
TextColumn::make('pages_count')
106106
->label('Pages')
107107
->counts('pages')
108-
->sortable()
109-
->alignCenter(),
108+
->badge()
109+
->color('gray'),
110110

111111
TextColumn::make('created_at')
112112
->label('Created')
@@ -121,6 +121,9 @@ public static function table(Table $table): Table
121121
->toggleable(),
122122
])
123123
->filters([
124+
SelectFilter::make('type')
125+
->options(SectionType::class),
126+
124127
TrashedFilter::make(),
125128
])
126129
->actions([
@@ -138,29 +141,20 @@ public static function table(Table $table): Table
138141
]);
139142
}
140143

141-
public static function getPages(): array
142-
{
143-
return [
144-
'index' => Pages\ListSections::route('/'),
145-
'create' => Pages\CreateSection::route('/create'),
146-
'edit' => Pages\EditSection::route('/{record}/edit'),
147-
];
148-
}
149-
150144
public static function getRelations(): array
151145
{
152146
return [
153147
RelationManagers\PagesRelationManager::class,
154148
];
155149
}
156150

157-
public static function getRelatedUrl(string $relation, Model $record): string
151+
public static function getPages(): array
158152
{
159-
if ($relation === 'pages') {
160-
return PageResource::getUrl('index', ['tableFilters' => ['section' => ['value' => $record->id]]]);
161-
}
162-
163-
return parent::getRelatedUrl($relation, $record);
153+
return [
154+
'index' => Pages\ListSections::route('/'),
155+
'create' => Pages\CreateSection::route('/create'),
156+
'edit' => Pages\EditSection::route('/{record}/edit'),
157+
];
164158
}
165159

166160
public static function getEloquentQuery(): Builder

0 commit comments

Comments
 (0)