Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ public function panel(Panel $panel): Panel
}
```

## Excluding Soft Deleted Records

If your models use soft deletes, you can exclude trashed records from the count with the `withoutTrashed` method on the plugin.

```php
use Awcodes\Overlook\OverlookPlugin;

public function panel(Panel $panel): Panel
{
return $panel
->plugins([
OverlookPlugin::make()
->withoutTrashed(),
]);
}
```

## Sorting the Items

By default, the items will be sorted in the order they are registered with Filament or as provided in the `includes` method. You can change this to sort them alphabetically with the `alphabetical` method on the plugin.
Expand Down
14 changes: 14 additions & 0 deletions src/OverlookPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class OverlookPlugin implements Plugin

protected array|Closure|null $icons = null;

protected bool|Closure|null $withoutTrashed = null;

public static function make(): self
{
return app(self::class);
Expand Down Expand Up @@ -143,4 +145,16 @@ public function getIcons(): array
{
return $this->evaluate($this->icons) ?? [];
}

public function withoutTrashed(bool|Closure|null $condition = true): static
{
$this->withoutTrashed = $condition;

return $this;
}

public function shouldExcludeTrashed(): bool
{
return $this->evaluate($this->withoutTrashed) ?? false;
}
}
7 changes: 6 additions & 1 deletion src/Widgets/OverlookWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Awcodes\Overlook\OverlookPlugin;
use Exception;
use Filament\Widgets\Widget;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Number;

class OverlookWidget extends Widget
Expand Down Expand Up @@ -71,14 +72,18 @@ public function getData(): array
? $includes
: filament()->getCurrentOrDefaultPanel()->getResources();

return collect($rawResources)->filter(fn ($resource): bool => ! in_array($resource, $excludes))->transform(function ($resource) use ($icons): ?array {
return collect($rawResources)->filter(fn ($resource): bool => ! in_array($resource, $excludes))->transform(function ($resource) use ($plugin, $icons): ?array {

$customIcon = array_search($resource, $icons);

$res = app($resource);

$widgetQuery = $res->getEloquentQuery();

if ($plugin->shouldExcludeTrashed() && in_array(SoftDeletes::class, class_uses_recursive($widgetQuery->getModel()))) {
$widgetQuery = $widgetQuery->withoutTrashed();
}

if ($res instanceof CustomizeOverlookWidget) {
$rawCount = $res->getOverlookWidgetQuery($widgetQuery)->count();
$title = $res->getOverlookWidgetTitle();
Expand Down
47 changes: 47 additions & 0 deletions tests/src/Feature/WidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,50 @@
&& $data[0]['name'] === 'Unverified Users';
});
});

it('excludes soft deleted records when withoutTrashed is enabled', function () {
// setUp creates 1 user for authentication
User::factory()->count(3)->create();
User::factory()->count(2)->create(['deleted_at' => now()]);

$this->panel
->plugins([
OverlookPlugin::make()
->withoutTrashed()
->includes([
UserResource::class,
]),
])
->widgets([
OverlookWidget::class,
]);

// 1 (from setUp) + 3 (created) = 4 non-trashed users
livewire(OverlookWidget::class)
->assertViewHas('data', function ($data) {
return $data[0]['count'] === '4';
});
});

it('excludes soft deleted records by default due to SoftDeletes global scope', function () {
// setUp creates 1 user for authentication
User::factory()->count(3)->create();
User::factory()->count(2)->create(['deleted_at' => now()]);

$this->panel
->plugins([
OverlookPlugin::make()
->includes([
UserResource::class,
]),
])
->widgets([
OverlookWidget::class,
]);

// Default SoftDeletes global scope excludes trashed: 1 (from setUp) + 3 = 4
livewire(OverlookWidget::class)
->assertViewHas('data', function ($data) {
return $data[0]['count'] === '4';
});
});
2 changes: 2 additions & 0 deletions tests/src/Fixtures/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements FilamentUser
{
use HasFactory;
use Notifiable;
use SoftDeletes;

protected $guarded = [];

Expand Down
10 changes: 10 additions & 0 deletions tests/src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Filament\Tables\TablesServiceProvider;
use Filament\Widgets\WidgetsServiceProvider;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as Orchestra;
Expand All @@ -36,6 +37,15 @@ protected function setUp(): void
$this->actingAs(User::factory()->create());
}

protected function afterRefreshingDatabase(): void
{
if (! Schema::hasColumn('users', 'deleted_at')) {
Schema::table('users', function ($table) {
$table->softDeletes();
});
}
}

protected function getPackageProviders($app): array
{
$providers = [
Expand Down
15 changes: 15 additions & 0 deletions tests/src/Unit/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,18 @@
expect(Filament::getPlugin('awcodes/overlook')->getIncludes())
->toContain('Awcodes\Overlook\Tests\Fixtures\Resources\Users\UserResource');
});

it('sets withoutTrashed', function (bool|Closure|null $condition) {
$this->panel
->plugins([
OverlookPlugin::make()->withoutTrashed($condition),
]);

expect(Filament::getPlugin('awcodes/overlook')->shouldExcludeTrashed())
->toBe($condition);
})->with([
true,
fn () => true,
false,
fn () => false,
]);
Loading