From 39656e99d41b28fa08fc634433708e1714a968fe Mon Sep 17 00:00:00 2001 From: Nick Rogers Date: Thu, 5 Sep 2024 09:49:33 +0930 Subject: [PATCH 1/8] feat: taxonomy terms can be reordered feat: show terms count for taxonomies wip: taxonomies should be reordered, but aren't fix: reinstated taxonomies list in navigation fix: taxonomy navigation icons are unique fix: nav isActiveWhen for taxonomies works fix: user-group icon for authors fix: hamburger icon for menu --- ...4_09_03_000000_add_order_to_taxonomies.php | 27 +++++++ ..._03_000000_add_order_to_taxonomy_terms.php | 27 +++++++ src/Filament/Resources/AuthorResource.php | 16 ++--- src/Filament/Resources/MenuResource.php | 6 +- src/Filament/Resources/TaxonomyResource.php | 71 ++++++++++++------- .../Resources/TaxonomyTermResource.php | 17 +++-- src/Models/Taxonomy.php | 24 ++++++- src/Models/TaxonomyTerm.php | 12 ++++ tests/Filament/TaxonomyResourceTest.php | 54 +++++++++++--- tests/Filament/TaxonomyTermResourceTest.php | 32 +++++++++ 10 files changed, 231 insertions(+), 55 deletions(-) create mode 100644 database/migrations/2024_09_03_000000_add_order_to_taxonomies.php create mode 100644 database/migrations/2024_09_03_000000_add_order_to_taxonomy_terms.php diff --git a/database/migrations/2024_09_03_000000_add_order_to_taxonomies.php b/database/migrations/2024_09_03_000000_add_order_to_taxonomies.php new file mode 100644 index 00000000..77c32737 --- /dev/null +++ b/database/migrations/2024_09_03_000000_add_order_to_taxonomies.php @@ -0,0 +1,27 @@ +integer('order')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('taxonomies', function (Blueprint $table) { + $table->dropColumn('order'); + }); + } +}; diff --git a/database/migrations/2024_09_03_000000_add_order_to_taxonomy_terms.php b/database/migrations/2024_09_03_000000_add_order_to_taxonomy_terms.php new file mode 100644 index 00000000..9ed509f5 --- /dev/null +++ b/database/migrations/2024_09_03_000000_add_order_to_taxonomy_terms.php @@ -0,0 +1,27 @@ +integer('order')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('taxonomy_terms', function (Blueprint $table) { + $table->dropColumn('order'); + }); + } +}; diff --git a/src/Filament/Resources/AuthorResource.php b/src/Filament/Resources/AuthorResource.php index 9c2b773c..5c7d13b1 100644 --- a/src/Filament/Resources/AuthorResource.php +++ b/src/Filament/Resources/AuthorResource.php @@ -21,7 +21,7 @@ class AuthorResource extends AbstractResource protected static ?string $model = Author::class; - protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; + protected static ?string $navigationIcon = 'heroicon-o-user-group'; public static function form(Form $form): Form { @@ -34,12 +34,12 @@ public static function getFormFields() Fieldset::make() ->schema([ TextInput::make('first_name') - ->label(fn (Get $get) => $get('is_individual') ? 'First Name' : 'Organization Name') - ->required() - ->autofocus(), + ->label(fn(Get $get) => $get('is_individual') ? 'First Name' : 'Organization Name') + ->required() + ->autofocus(), TextInput::make('last_name') ->label('Last Name') - ->visible(fn (Get $get) => $get('is_individual') ? true : false), + ->visible(fn(Get $get) => $get('is_individual') ? true : false), ]) ->columns(2), Toggle::make('is_individual') @@ -56,11 +56,9 @@ public static function table(Table $table): Table ->columns([ TextColumn::make('display_name')->sortable(), IconColumn::make('is_individual')->label('Category') - ->icon(fn (bool $state) => $state ? 'heroicon-m-user' : 'heroicon-m-user-group'), - ]) - ->filters([ - + ->icon(fn(bool $state) => $state ? 'heroicon-m-user' : 'heroicon-m-user-group'), ]) + ->filters([]) ->actions([ Tables\Actions\EditAction::make(), ]) diff --git a/src/Filament/Resources/MenuResource.php b/src/Filament/Resources/MenuResource.php index 2dfa6294..db857f67 100644 --- a/src/Filament/Resources/MenuResource.php +++ b/src/Filament/Resources/MenuResource.php @@ -18,7 +18,7 @@ class MenuResource extends AbstractResource protected static ?string $model = Menu::class; - protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; + protected static ?string $navigationIcon = 'heroicon-o-bars-3'; protected static ?string $navigationGroup = 'Content'; public static function form(Form $form): Form @@ -45,9 +45,7 @@ public static function table(Table $table): Table TextColumn::make('note')->label('Note'), TextColumn::make('items.count')->label('Items'), ]) - ->filters([ - - ]) + ->filters([]) ->actions([ Tables\Actions\EditAction::make(), ]) diff --git a/src/Filament/Resources/TaxonomyResource.php b/src/Filament/Resources/TaxonomyResource.php index 6e0fc819..2fa2f0a2 100644 --- a/src/Filament/Resources/TaxonomyResource.php +++ b/src/Filament/Resources/TaxonomyResource.php @@ -7,6 +7,7 @@ use Filament\Navigation\NavigationItem; use Filament\Tables; use Filament\Tables\Table; +use Illuminate\Support\Str; use Portable\FilaCms\Facades\FilaCms; use Portable\FilaCms\Filament\Resources\TaxonomyResource\Pages; use Portable\FilaCms\Filament\Resources\TaxonomyResource\RelationManagers; @@ -23,37 +24,50 @@ class TaxonomyResource extends AbstractResource protected static ?string $navigationGroup = 'Taxonomies'; - /** - * @return array - */ + // /** + // * @return array + // */ public static function getNavigationItems(): array { $navItems = []; + // Include default index + $navItems[] = + NavigationItem::make(static::getNavigationLabel()) + ->group(static::getNavigationGroup()) + ->parentItem(static::getNavigationParentItem()) + ->icon(static::getNavigationIcon()) + ->activeIcon(static::getActiveNavigationIcon()) + ->isActiveWhen(fn() => request()->routeIs(static::getRouteBaseName() . '.index')) + ->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor()) + ->badgeTooltip(static::getNavigationBadgeTooltip()) + ->sort(static::getNavigationSort()) + ->url(route(static::getRouteBaseName() . '.index')); + + // Include all taxonomies foreach (Taxonomy::all() as $taxonomy) { $navItems[] = NavigationItem::make($taxonomy->name) - ->group(static::getNavigationGroup()) - ->parentItem(static::getNavigationParentItem()) - ->icon(static::getNavigationIcon()) - ->activeIcon(static::getActiveNavigationIcon()) - ->isActiveWhen(fn () => request()->routeIs(route(static::getRouteBaseName() . '.edit', $taxonomy))) - ->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor()) - ->badgeTooltip(static::getNavigationBadgeTooltip()) - ->sort(static::getNavigationSort()) - ->url(route(static::getRouteBaseName() . '.edit', $taxonomy)); - } - - $navItems[] = - NavigationItem::make('Create') ->group(static::getNavigationGroup()) ->parentItem(static::getNavigationParentItem()) - ->icon(static::getNavigationIcon()) - ->activeIcon(static::getActiveNavigationIcon()) - ->isActiveWhen(fn () => request()->routeIs(static::getRouteBaseName() . '.create')) + ->icon('heroicon-o-square-2-stack') + ->isActiveWhen(fn() => request()->routeIs(static::getRouteBaseName() . '.edit') && request()->route('record') == $taxonomy->id) ->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor()) ->badgeTooltip(static::getNavigationBadgeTooltip()) ->sort(static::getNavigationSort()) - ->url(route(static::getRouteBaseName() . '.create')); + ->url(route(static::getRouteBaseName() . '.edit', $taxonomy)); + } + + // Include direct link to create + $navItems[] = + NavigationItem::make('New' . ' ' . Str::singular(static::getNavigationLabel())) + ->group(static::getNavigationGroup()) + ->parentItem(static::getNavigationParentItem()) + ->icon('heroicon-o-plus-circle') + ->isActiveWhen(fn() => request()->routeIs(static::getRouteBaseName() . '.create')) + ->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor()) + ->badgeTooltip(static::getNavigationBadgeTooltip()) + ->sort(static::getNavigationSort()) + ->url(route(static::getRouteBaseName() . '.create')); return $navItems; } @@ -91,10 +105,17 @@ public static function table(Table $table): Table ->label('Created') ->dateTime() ->sortable(), + Tables\Columns\TextColumn::make('terms') + ->sortable() + ->badge() + ->color(fn(string $state): string => $state > 0 ? 'primary' : 'gray') + ->state(fn(Taxonomy $record): float => $record->terms->count()), ]) - ->filters([ - // - ]) + ->reorderRecordsTriggerAction( + fn(Tables\Actions\Action $action, bool $isReordering) => $action + ->button() + ->label($isReordering ? 'Disable reordering' : 'Enable reordering'), + ) ->actions([ Tables\Actions\EditAction::make(), ]) @@ -102,7 +123,9 @@ public static function table(Table $table): Table Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), - ]); + ]) + ->defaultSort('order') + ->reorderable('order', auth()->user()->can('manage taxonomies')); } public static function getRelations(): array diff --git a/src/Filament/Resources/TaxonomyTermResource.php b/src/Filament/Resources/TaxonomyTermResource.php index e89c457e..efd0ae40 100644 --- a/src/Filament/Resources/TaxonomyTermResource.php +++ b/src/Filament/Resources/TaxonomyTermResource.php @@ -112,16 +112,23 @@ public static function table(Table $table): Table if ($hasTermWithContent) { Notification::make() - ->danger() - ->title('Unable to delete terms') - ->body('One or more terms selected is currently in use') - ->send(); + ->danger() + ->title('Unable to delete terms') + ->body('One or more terms selected is currently in use') + ->send(); $action->cancel(); } }), ]), - ]); + ]) + ->reorderRecordsTriggerAction( + fn(Tables\Actions\Action $action, bool $isReordering) => $action + ->button() + ->label($isReordering ? 'Finish reordering' : 'Reorder terms'), + ) + ->defaultSort('order') + ->reorderable('order', auth()->user()->can('manage taxonomies')); } public static function getRelations(): array diff --git a/src/Models/Taxonomy.php b/src/Models/Taxonomy.php index 14ee7741..297447e2 100644 --- a/src/Models/Taxonomy.php +++ b/src/Models/Taxonomy.php @@ -3,6 +3,7 @@ namespace Portable\FilaCms\Models; use Dyrynda\Database\Support\CascadeSoftDeletes; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -22,11 +23,13 @@ class Taxonomy extends Model protected $versionable = [ 'name', 'taxonomy_resources', + 'order', ]; protected $fillable = [ - 'name', - 'code' + 'name', + 'code', + 'order', ]; protected $appends = [ @@ -37,7 +40,7 @@ class Taxonomy extends Model public function terms() { - return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id'); + return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')->orderBy('order'); } public function resources() @@ -51,4 +54,19 @@ public function taxonomyResources(): Attribute return $this->resources->pluck('resource_class'); }); } + + public function newQuery(): Builder + { + return parent::newQuery()->orderBy('order'); + } + + public static function booted(): void + { + static::created(function (Taxonomy $item) { + // auto-add order with end of list + $count = Taxonomy::max('order'); + $item->order = $count + 1; + $item->save(); + }); + } } diff --git a/src/Models/TaxonomyTerm.php b/src/Models/TaxonomyTerm.php index 01340cc4..9775d508 100644 --- a/src/Models/TaxonomyTerm.php +++ b/src/Models/TaxonomyTerm.php @@ -18,12 +18,14 @@ class TaxonomyTerm extends Model protected $versionable = [ 'name', + 'order', ]; protected $fillable = [ 'name', 'taxonomy_id', 'parent_id', + 'order', ]; public function taxonomy() @@ -40,4 +42,14 @@ public function taxonomyables() { return $this->hasMany(Taxonomyable::class); } + + public static function booted(): void + { + static::created(function (TaxonomyTerm $item) { + // auto-add order with end of list + $count = TaxonomyTerm::where('taxonomy_id', $item->taxonomy_id)->max('order'); + $item->order = $count + 1; + $item->save(); + }); + } } diff --git a/tests/Filament/TaxonomyResourceTest.php b/tests/Filament/TaxonomyResourceTest.php index ac4ba467..029d51b2 100644 --- a/tests/Filament/TaxonomyResourceTest.php +++ b/tests/Filament/TaxonomyResourceTest.php @@ -108,14 +108,14 @@ public function test_can_save_form(): void Livewire::test(TargetResource\Pages\EditTaxonomy::class, [ 'record' => $data->getRoutekey(), ]) - ->fillForm([ - 'code' => fake()->regexify('[A-Z]{3}'), - 'name' => $new->name, - 'taxonomy_resources' => array_keys(FilaCms::getContentModels()) - ]) - ->call('save') - ->dumpSession() - ->assertHasNoFormErrors(); + ->fillForm([ + 'code' => fake()->regexify('[A-Z]{3}'), + 'name' => $new->name, + 'taxonomy_resources' => array_keys(FilaCms::getContentModels()) + ]) + ->call('save') + ->dumpSession() + ->assertHasNoFormErrors(); $data->refresh(); $this->assertEquals($data->name, $new->name); @@ -135,7 +135,7 @@ public function test_cant_delete_with_terms_in_use() $livewireResponse = Livewire::test(TargetResource\Pages\EditTaxonomy::class, [ 'record' => $taxonomy->getRouteKey() - ]) + ]) ->call('mountAction', 'delete') ->call('callMountedAction'); @@ -157,7 +157,7 @@ public function test_can_delete_without_terms_in_use() $livewireResponse = Livewire::test(TargetResource\Pages\EditTaxonomy::class, [ 'record' => $taxonomy->getRouteKey() - ]) + ]) ->call('mountAction', 'delete') ->call('callMountedAction'); @@ -171,6 +171,40 @@ public function test_can_delete_without_terms_in_use() $this->assertNull($term); } + public function test_can_reorder_taxonomies() + { + + // Step 1: Create some Taxonomies + $taxonomy1 = Taxonomy::factory()->create(); + $taxonomy2 = Taxonomy::factory()->create(); + $taxonomy3 = Taxonomy::factory()->create(); + + // Assert initial order + expect($taxonomy1->fresh()->order)->toBe(1); + expect($taxonomy2->fresh()->order)->toBe(2); + expect($taxonomy3->fresh()->order)->toBe(3); + + // Step 2: Reorder the terms + // TODO: This doesn't appear to work + Livewire::test( + TargetResource\Pages\ListTaxonomies::class + )->call('reorderTable', ['3', '2', '1']); + + // TODO: this mimics what the above function should do + $taxonomy1->update(['order' => 3]); + $taxonomy2->update(['order' => 2]); + $taxonomy3->update(['order' => 1]); + + // Step 3: Check the default order + $taxonomies = Taxonomy::all(); + + expect($taxonomies->pluck('id')->toArray())->toBe([ + $taxonomy3->id, // Should now be first + $taxonomy2->id, // Should now be second + $taxonomy1->id, // Should now be third + ]); + } + public function generateModel(): TargetModel { return TargetModel::create([ diff --git a/tests/Filament/TaxonomyTermResourceTest.php b/tests/Filament/TaxonomyTermResourceTest.php index db4ea47b..dc710c09 100644 --- a/tests/Filament/TaxonomyTermResourceTest.php +++ b/tests/Filament/TaxonomyTermResourceTest.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Livewire\Livewire; +use Mockery; use Portable\FilaCms\Filament\Resources\TaxonomyResource\Pages\EditTaxonomy; use Portable\FilaCms\Filament\Resources\TaxonomyResource\RelationManagers\TermsRelationManager; use Portable\FilaCms\Models\Page; @@ -74,4 +75,35 @@ public function test_can_delete_terms_not_in_use() $this->assertSoftDeleted('taxonomy_terms', ['id' => $term->id]); } + + public function test_can_reorder_taxonomy_terms() + { + + $taxonomy = TaxonomyFactory::new()->create(); + + // Step 1: Create some TaxonomyTerms + $term1 = TaxonomyTerm::factory()->create(['taxonomy_id' => $taxonomy->id]); + $term2 = TaxonomyTerm::factory()->create(['taxonomy_id' => $taxonomy->id]); + $term3 = TaxonomyTerm::factory()->create(['taxonomy_id' => $taxonomy->id]); + + // Assert initial order + expect($term1->fresh()->order)->toBe(1); + expect($term2->fresh()->order)->toBe(2); + expect($term3->fresh()->order)->toBe(3); + + // Step 2: Reorder the terms + Livewire::test(TermsRelationManager::class, [ + 'ownerRecord' => $taxonomy, + 'pageClass' => EditTaxonomy::class + ])->call('reorderTable', ['3', '2', '1']); + + // Step 3: Check the default order + $terms = $taxonomy->terms()->get(); + + expect($terms->pluck('id')->toArray())->toBe([ + $term3->id, // Should now be first + $term2->id, // Should now be second + $term1->id, // Should now be third + ]); + } } From f91203c23375c553f332b97a1f06b17b1fe23ec5 Mon Sep 17 00:00:00 2001 From: Jeremy Layson Date: Fri, 4 Oct 2024 16:22:02 +0800 Subject: [PATCH 2/8] Temporarily remove order --- src/Models/Taxonomy.php | 18 ++++++++---------- src/Models/TaxonomyTerm.php | 14 ++++++-------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Models/Taxonomy.php b/src/Models/Taxonomy.php index 297447e2..d9124ec5 100644 --- a/src/Models/Taxonomy.php +++ b/src/Models/Taxonomy.php @@ -23,13 +23,11 @@ class Taxonomy extends Model protected $versionable = [ 'name', 'taxonomy_resources', - 'order', ]; protected $fillable = [ 'name', 'code', - 'order', ]; protected $appends = [ @@ -40,7 +38,7 @@ class Taxonomy extends Model public function terms() { - return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')->orderBy('order'); + return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id'); } public function resources() @@ -57,16 +55,16 @@ public function taxonomyResources(): Attribute public function newQuery(): Builder { - return parent::newQuery()->orderBy('order'); + return parent::newQuery(); } public static function booted(): void { - static::created(function (Taxonomy $item) { - // auto-add order with end of list - $count = Taxonomy::max('order'); - $item->order = $count + 1; - $item->save(); - }); + // static::created(function (Taxonomy $item) { + // // auto-add order with end of list + // $count = Taxonomy::max('order'); + // $item->order = $count + 1; + // $item->save(); + // }); } } diff --git a/src/Models/TaxonomyTerm.php b/src/Models/TaxonomyTerm.php index 9775d508..794fc6b3 100644 --- a/src/Models/TaxonomyTerm.php +++ b/src/Models/TaxonomyTerm.php @@ -18,14 +18,12 @@ class TaxonomyTerm extends Model protected $versionable = [ 'name', - 'order', ]; protected $fillable = [ 'name', 'taxonomy_id', 'parent_id', - 'order', ]; public function taxonomy() @@ -45,11 +43,11 @@ public function taxonomyables() public static function booted(): void { - static::created(function (TaxonomyTerm $item) { - // auto-add order with end of list - $count = TaxonomyTerm::where('taxonomy_id', $item->taxonomy_id)->max('order'); - $item->order = $count + 1; - $item->save(); - }); + // static::created(function (TaxonomyTerm $item) { + // // auto-add order with end of list + // $count = TaxonomyTerm::where('taxonomy_id', $item->taxonomy_id)->max('order'); + // $item->order = $count + 1; + // $item->save(); + // }); } } From 9ddf88f16ac102093dafdc713af5d3312dfd6f39 Mon Sep 17 00:00:00 2001 From: Jeremy Layson Date: Fri, 4 Oct 2024 16:25:16 +0800 Subject: [PATCH 3/8] Revert order --- src/Models/Taxonomy.php | 18 ++++++++++-------- src/Models/TaxonomyTerm.php | 14 ++++++++------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Models/Taxonomy.php b/src/Models/Taxonomy.php index d9124ec5..297447e2 100644 --- a/src/Models/Taxonomy.php +++ b/src/Models/Taxonomy.php @@ -23,11 +23,13 @@ class Taxonomy extends Model protected $versionable = [ 'name', 'taxonomy_resources', + 'order', ]; protected $fillable = [ 'name', 'code', + 'order', ]; protected $appends = [ @@ -38,7 +40,7 @@ class Taxonomy extends Model public function terms() { - return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id'); + return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')->orderBy('order'); } public function resources() @@ -55,16 +57,16 @@ public function taxonomyResources(): Attribute public function newQuery(): Builder { - return parent::newQuery(); + return parent::newQuery()->orderBy('order'); } public static function booted(): void { - // static::created(function (Taxonomy $item) { - // // auto-add order with end of list - // $count = Taxonomy::max('order'); - // $item->order = $count + 1; - // $item->save(); - // }); + static::created(function (Taxonomy $item) { + // auto-add order with end of list + $count = Taxonomy::max('order'); + $item->order = $count + 1; + $item->save(); + }); } } diff --git a/src/Models/TaxonomyTerm.php b/src/Models/TaxonomyTerm.php index 794fc6b3..9775d508 100644 --- a/src/Models/TaxonomyTerm.php +++ b/src/Models/TaxonomyTerm.php @@ -18,12 +18,14 @@ class TaxonomyTerm extends Model protected $versionable = [ 'name', + 'order', ]; protected $fillable = [ 'name', 'taxonomy_id', 'parent_id', + 'order', ]; public function taxonomy() @@ -43,11 +45,11 @@ public function taxonomyables() public static function booted(): void { - // static::created(function (TaxonomyTerm $item) { - // // auto-add order with end of list - // $count = TaxonomyTerm::where('taxonomy_id', $item->taxonomy_id)->max('order'); - // $item->order = $count + 1; - // $item->save(); - // }); + static::created(function (TaxonomyTerm $item) { + // auto-add order with end of list + $count = TaxonomyTerm::where('taxonomy_id', $item->taxonomy_id)->max('order'); + $item->order = $count + 1; + $item->save(); + }); } } From 43eacedca3887a26f4062fff2a08362b0b95ef3d Mon Sep 17 00:00:00 2001 From: Jeremy Layson Date: Tue, 15 Oct 2024 21:37:30 +0800 Subject: [PATCH 4/8] Fix issue with reordering --- src/Filament/Resources/TaxonomyResource.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Filament/Resources/TaxonomyResource.php b/src/Filament/Resources/TaxonomyResource.php index 2fa2f0a2..d36cb6b4 100644 --- a/src/Filament/Resources/TaxonomyResource.php +++ b/src/Filament/Resources/TaxonomyResource.php @@ -125,7 +125,8 @@ public static function table(Table $table): Table ]), ]) ->defaultSort('order') - ->reorderable('order', auth()->user()->can('manage taxonomies')); + ->authorizeReorder(auth()->user()->can('manage taxonomies')) + ->reorderable('order'); } public static function getRelations(): array From 58f63f58fd2bad6a94ae1645ba63d41d90ff0599 Mon Sep 17 00:00:00 2001 From: Jeremy Layson Date: Tue, 15 Oct 2024 23:10:56 +0800 Subject: [PATCH 5/8] Revert Author Resource --- src/Filament/Resources/AuthorResource.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Filament/Resources/AuthorResource.php b/src/Filament/Resources/AuthorResource.php index 5c7d13b1..9c2b773c 100644 --- a/src/Filament/Resources/AuthorResource.php +++ b/src/Filament/Resources/AuthorResource.php @@ -21,7 +21,7 @@ class AuthorResource extends AbstractResource protected static ?string $model = Author::class; - protected static ?string $navigationIcon = 'heroicon-o-user-group'; + protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; public static function form(Form $form): Form { @@ -34,12 +34,12 @@ public static function getFormFields() Fieldset::make() ->schema([ TextInput::make('first_name') - ->label(fn(Get $get) => $get('is_individual') ? 'First Name' : 'Organization Name') - ->required() - ->autofocus(), + ->label(fn (Get $get) => $get('is_individual') ? 'First Name' : 'Organization Name') + ->required() + ->autofocus(), TextInput::make('last_name') ->label('Last Name') - ->visible(fn(Get $get) => $get('is_individual') ? true : false), + ->visible(fn (Get $get) => $get('is_individual') ? true : false), ]) ->columns(2), Toggle::make('is_individual') @@ -56,9 +56,11 @@ public static function table(Table $table): Table ->columns([ TextColumn::make('display_name')->sortable(), IconColumn::make('is_individual')->label('Category') - ->icon(fn(bool $state) => $state ? 'heroicon-m-user' : 'heroicon-m-user-group'), + ->icon(fn (bool $state) => $state ? 'heroicon-m-user' : 'heroicon-m-user-group'), + ]) + ->filters([ + ]) - ->filters([]) ->actions([ Tables\Actions\EditAction::make(), ]) From af635e868d0805bcbb280342ac1bbecf28f79aab Mon Sep 17 00:00:00 2001 From: Jeremy Layson Date: Tue, 15 Oct 2024 23:35:37 +0800 Subject: [PATCH 6/8] Backward compatibility with missing order column --- src/Models/Taxonomy.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Models/Taxonomy.php b/src/Models/Taxonomy.php index 297447e2..7937e25b 100644 --- a/src/Models/Taxonomy.php +++ b/src/Models/Taxonomy.php @@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Overtrue\LaravelVersionable\Versionable; use Overtrue\LaravelVersionable\VersionStrategy; +use Illuminate\Support\Facades\Schema; class Taxonomy extends Model { @@ -40,7 +41,10 @@ class Taxonomy extends Model public function terms() { - return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')->orderBy('order'); + if (Schema::hasColumn('taxonomies', 'order')) { + return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')->orderBy('order'); + } + return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id'); } public function resources() @@ -57,16 +61,21 @@ public function taxonomyResources(): Attribute public function newQuery(): Builder { - return parent::newQuery()->orderBy('order'); + if (Schema::hasColumn('taxonomies', 'order')) { + return parent::newQuery()->orderBy('order'); + } + return parent::newQuery(); } public static function booted(): void { static::created(function (Taxonomy $item) { - // auto-add order with end of list - $count = Taxonomy::max('order'); - $item->order = $count + 1; - $item->save(); + if (Schema::hasColumn('taxonomies', 'order')) { + // auto-add order with end of list + $count = Taxonomy::max('order'); + $item->order = $count + 1; + $item->save(); + } }); } } From 21befdef9978bf8a9e0cea40d8924e6b489fad0a Mon Sep 17 00:00:00 2001 From: Jeremy Layson Date: Wed, 16 Oct 2024 00:06:34 +0800 Subject: [PATCH 7/8] Lint --- .../2024_09_03_000000_add_order_to_taxonomies.php | 2 +- ...2024_09_03_000000_add_order_to_taxonomy_terms.php | 2 +- src/Filament/Resources/TaxonomyResource.php | 12 ++++++------ src/Filament/Resources/TaxonomyTermResource.php | 2 +- tests/Filament/TaxonomyTermResourceTest.php | 1 - 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/database/migrations/2024_09_03_000000_add_order_to_taxonomies.php b/database/migrations/2024_09_03_000000_add_order_to_taxonomies.php index 77c32737..d2233f79 100644 --- a/database/migrations/2024_09_03_000000_add_order_to_taxonomies.php +++ b/database/migrations/2024_09_03_000000_add_order_to_taxonomies.php @@ -4,7 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class() extends Migration { +return new class () extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2024_09_03_000000_add_order_to_taxonomy_terms.php b/database/migrations/2024_09_03_000000_add_order_to_taxonomy_terms.php index 9ed509f5..8827f348 100644 --- a/database/migrations/2024_09_03_000000_add_order_to_taxonomy_terms.php +++ b/database/migrations/2024_09_03_000000_add_order_to_taxonomy_terms.php @@ -4,7 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class() extends Migration { +return new class () extends Migration { /** * Run the migrations. */ diff --git a/src/Filament/Resources/TaxonomyResource.php b/src/Filament/Resources/TaxonomyResource.php index d36cb6b4..7ce40a64 100644 --- a/src/Filament/Resources/TaxonomyResource.php +++ b/src/Filament/Resources/TaxonomyResource.php @@ -37,7 +37,7 @@ public static function getNavigationItems(): array ->parentItem(static::getNavigationParentItem()) ->icon(static::getNavigationIcon()) ->activeIcon(static::getActiveNavigationIcon()) - ->isActiveWhen(fn() => request()->routeIs(static::getRouteBaseName() . '.index')) + ->isActiveWhen(fn () => request()->routeIs(static::getRouteBaseName() . '.index')) ->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor()) ->badgeTooltip(static::getNavigationBadgeTooltip()) ->sort(static::getNavigationSort()) @@ -50,7 +50,7 @@ public static function getNavigationItems(): array ->group(static::getNavigationGroup()) ->parentItem(static::getNavigationParentItem()) ->icon('heroicon-o-square-2-stack') - ->isActiveWhen(fn() => request()->routeIs(static::getRouteBaseName() . '.edit') && request()->route('record') == $taxonomy->id) + ->isActiveWhen(fn () => request()->routeIs(static::getRouteBaseName() . '.edit') && request()->route('record') == $taxonomy->id) ->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor()) ->badgeTooltip(static::getNavigationBadgeTooltip()) ->sort(static::getNavigationSort()) @@ -63,7 +63,7 @@ public static function getNavigationItems(): array ->group(static::getNavigationGroup()) ->parentItem(static::getNavigationParentItem()) ->icon('heroicon-o-plus-circle') - ->isActiveWhen(fn() => request()->routeIs(static::getRouteBaseName() . '.create')) + ->isActiveWhen(fn () => request()->routeIs(static::getRouteBaseName() . '.create')) ->badge(static::getNavigationBadge(), color: static::getNavigationBadgeColor()) ->badgeTooltip(static::getNavigationBadgeTooltip()) ->sort(static::getNavigationSort()) @@ -108,11 +108,11 @@ public static function table(Table $table): Table Tables\Columns\TextColumn::make('terms') ->sortable() ->badge() - ->color(fn(string $state): string => $state > 0 ? 'primary' : 'gray') - ->state(fn(Taxonomy $record): float => $record->terms->count()), + ->color(fn (string $state): string => $state > 0 ? 'primary' : 'gray') + ->state(fn (Taxonomy $record): float => $record->terms->count()), ]) ->reorderRecordsTriggerAction( - fn(Tables\Actions\Action $action, bool $isReordering) => $action + fn (Tables\Actions\Action $action, bool $isReordering) => $action ->button() ->label($isReordering ? 'Disable reordering' : 'Enable reordering'), ) diff --git a/src/Filament/Resources/TaxonomyTermResource.php b/src/Filament/Resources/TaxonomyTermResource.php index efd0ae40..e6253e1f 100644 --- a/src/Filament/Resources/TaxonomyTermResource.php +++ b/src/Filament/Resources/TaxonomyTermResource.php @@ -123,7 +123,7 @@ public static function table(Table $table): Table ]), ]) ->reorderRecordsTriggerAction( - fn(Tables\Actions\Action $action, bool $isReordering) => $action + fn (Tables\Actions\Action $action, bool $isReordering) => $action ->button() ->label($isReordering ? 'Finish reordering' : 'Reorder terms'), ) diff --git a/tests/Filament/TaxonomyTermResourceTest.php b/tests/Filament/TaxonomyTermResourceTest.php index dc710c09..d978b164 100644 --- a/tests/Filament/TaxonomyTermResourceTest.php +++ b/tests/Filament/TaxonomyTermResourceTest.php @@ -6,7 +6,6 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Livewire\Livewire; -use Mockery; use Portable\FilaCms\Filament\Resources\TaxonomyResource\Pages\EditTaxonomy; use Portable\FilaCms\Filament\Resources\TaxonomyResource\RelationManagers\TermsRelationManager; use Portable\FilaCms\Models\Page; From deeddc1fe9427822757adf0ab3ed939b4dc74d45 Mon Sep 17 00:00:00 2001 From: jeremy-portable Date: Wed, 6 Nov 2024 13:15:29 +0800 Subject: [PATCH 8/8] Fix testcase to remove todo and 'hack' --- tests/Filament/TaxonomyResourceTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/Filament/TaxonomyResourceTest.php b/tests/Filament/TaxonomyResourceTest.php index 029d51b2..47bbf572 100644 --- a/tests/Filament/TaxonomyResourceTest.php +++ b/tests/Filament/TaxonomyResourceTest.php @@ -185,16 +185,10 @@ public function test_can_reorder_taxonomies() expect($taxonomy3->fresh()->order)->toBe(3); // Step 2: Reorder the terms - // TODO: This doesn't appear to work Livewire::test( TargetResource\Pages\ListTaxonomies::class )->call('reorderTable', ['3', '2', '1']); - // TODO: this mimics what the above function should do - $taxonomy1->update(['order' => 3]); - $taxonomy2->update(['order' => 2]); - $taxonomy3->update(['order' => 1]); - // Step 3: Check the default order $taxonomies = Taxonomy::all();