From 285b688afd345af61b2977f0a037f1be4feb6aab Mon Sep 17 00:00:00 2001 From: Dennis Elsinga Date: Sat, 13 Apr 2024 13:38:39 +0200 Subject: [PATCH] feat: Add table summaries for average, count, range, sum --- src/Handlers/StubHandler.php | 5 +++++ src/Stubs/Base.php | 5 +++++ .../Page/Index/Table/Summaries/Average.php | 20 +++++++++++++++++++ .../Page/Index/Table/Summaries/Count.php | 20 +++++++++++++++++++ .../Page/Index/Table/Summaries/Range.php | 20 +++++++++++++++++++ src/Stubs/Page/Index/Table/Summaries/Sum.php | 17 ++++++++++++++++ stubs/Page/Index/Table/Filters/Add.stub | 4 ++-- stubs/Page/Index/Table/Filters/Remove.stub | 4 ++-- stubs/Page/Index/Table/Filters/Reset.stub | 2 +- stubs/Page/Index/Table/Summaries/Average.stub | 3 +++ stubs/Page/Index/Table/Summaries/Count.stub | 3 +++ stubs/Page/Index/Table/Summaries/Range.stub | 3 +++ stubs/Page/Index/Table/Summaries/Sum.stub | 3 +++ 13 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 src/Stubs/Page/Index/Table/Summaries/Average.php create mode 100644 src/Stubs/Page/Index/Table/Summaries/Count.php create mode 100644 src/Stubs/Page/Index/Table/Summaries/Range.php create mode 100644 src/Stubs/Page/Index/Table/Summaries/Sum.php create mode 100644 stubs/Page/Index/Table/Summaries/Average.stub create mode 100644 stubs/Page/Index/Table/Summaries/Count.stub create mode 100644 stubs/Page/Index/Table/Summaries/Range.stub create mode 100644 stubs/Page/Index/Table/Summaries/Sum.stub diff --git a/src/Handlers/StubHandler.php b/src/Handlers/StubHandler.php index 58cdd171..63a55e09 100644 --- a/src/Handlers/StubHandler.php +++ b/src/Handlers/StubHandler.php @@ -61,6 +61,11 @@ public function getStubs(): Collection \CodeWithDennis\FilamentTests\Stubs\Page\Index\Table\Columns\Select::make($resource)->get(), \CodeWithDennis\FilamentTests\Stubs\Page\Index\Table\Columns\ExtraAttributes::make($resource)->get(), + \CodeWithDennis\FilamentTests\Stubs\Page\Index\Table\Summaries\Average::make($resource)->get(), + \CodeWithDennis\FilamentTests\Stubs\Page\Index\Table\Summaries\Count::make($resource)->get(), + \CodeWithDennis\FilamentTests\Stubs\Page\Index\Table\Summaries\Range::make($resource)->get(), + \CodeWithDennis\FilamentTests\Stubs\Page\Index\Table\Summaries\Sum::make($resource)->get(), + \CodeWithDennis\FilamentTests\Stubs\Page\Index\Table\Filters\Reset::make($resource)->get(), \CodeWithDennis\FilamentTests\Stubs\Page\Index\Table\Filters\Add::make($resource)->get(), \CodeWithDennis\FilamentTests\Stubs\Page\Index\Table\Filters\Remove::make($resource)->get(), diff --git a/src/Stubs/Base.php b/src/Stubs/Base.php index ed7a98aa..081303b4 100644 --- a/src/Stubs/Base.php +++ b/src/Stubs/Base.php @@ -400,6 +400,11 @@ public function getTableSelectColumnsWithOptions(Resource $resource): array ])->toArray(); } + public function getResourceTableColumnsWithSummarizers(Resource $resource): Collection + { + return $this->getTableColumns($resource)->filter(fn($column) => $column->getSummarizers()); + } + public function hasSoftDeletes(Resource $resource): bool { return method_exists($resource->getModel(), 'bootSoftDeletes'); diff --git a/src/Stubs/Page/Index/Table/Summaries/Average.php b/src/Stubs/Page/Index/Table/Summaries/Average.php new file mode 100644 index 00000000..0394cdf8 --- /dev/null +++ b/src/Stubs/Page/Index/Table/Summaries/Average.php @@ -0,0 +1,20 @@ +getResourceTableColumnsWithSummarizers($this->resource) + ->filter(fn($column) => collect($column->getSummarizers()) + ->filter(fn($summarizer) => $summarizer::class === \Filament\Tables\Columns\Summarizers\Average::class) + ->count()) + ->isNotEmpty(); + } +} diff --git a/src/Stubs/Page/Index/Table/Summaries/Count.php b/src/Stubs/Page/Index/Table/Summaries/Count.php new file mode 100644 index 00000000..ce609198 --- /dev/null +++ b/src/Stubs/Page/Index/Table/Summaries/Count.php @@ -0,0 +1,20 @@ +getResourceTableColumnsWithSummarizers($this->resource) + ->filter(fn($column) => collect($column->getSummarizers()) + ->filter(fn($summarizer) => $summarizer::class === \Filament\Tables\Columns\Summarizers\Count::class) + ->count()) + ->isNotEmpty(); + } +} diff --git a/src/Stubs/Page/Index/Table/Summaries/Range.php b/src/Stubs/Page/Index/Table/Summaries/Range.php new file mode 100644 index 00000000..250e1e64 --- /dev/null +++ b/src/Stubs/Page/Index/Table/Summaries/Range.php @@ -0,0 +1,20 @@ +getResourceTableColumnsWithSummarizers($this->resource) + ->filter(fn($column) => collect($column->getSummarizers()) + ->filter(fn($summarizer) => $summarizer::class === \Filament\Tables\Columns\Summarizers\Range::class) + ->count()) + ->isNotEmpty(); + } +} diff --git a/src/Stubs/Page/Index/Table/Summaries/Sum.php b/src/Stubs/Page/Index/Table/Summaries/Sum.php new file mode 100644 index 00000000..dd1dc0aa --- /dev/null +++ b/src/Stubs/Page/Index/Table/Summaries/Sum.php @@ -0,0 +1,17 @@ +getResourceTableColumnsWithSummarizers($this->resource)->isNotEmpty(); + } +} diff --git a/stubs/Page/Index/Table/Filters/Add.stub b/stubs/Page/Index/Table/Filters/Add.stub index 15f24eca..b1591b13 100644 --- a/stubs/Page/Index/Table/Filters/Add.stub +++ b/stubs/Page/Index/Table/Filters/Add.stub @@ -1,3 +1,3 @@ it('can add a table filter', function () { - -})->group('filters', 'table')->todo(); + // +}){{ RESOLVED_GROUP_METHOD }}->todo(); diff --git a/stubs/Page/Index/Table/Filters/Remove.stub b/stubs/Page/Index/Table/Filters/Remove.stub index 53602138..ae63effa 100644 --- a/stubs/Page/Index/Table/Filters/Remove.stub +++ b/stubs/Page/Index/Table/Filters/Remove.stub @@ -1,3 +1,3 @@ it('can remove a table filter', function () { - -})->group('filters', 'table')->todo(); + // +}){{ RESOLVED_GROUP_METHOD }}->todo(); diff --git a/stubs/Page/Index/Table/Filters/Reset.stub b/stubs/Page/Index/Table/Filters/Reset.stub index fb45d8a0..c4eef9dd 100644 --- a/stubs/Page/Index/Table/Filters/Reset.stub +++ b/stubs/Page/Index/Table/Filters/Reset.stub @@ -4,4 +4,4 @@ it('can reset table filters', function () { livewire(List{{ MODEL_PLURAL_NAME }}::class) ->resetTableFilters(){{ LOAD_TABLE_METHOD_IF_DEFERRED }} ->assertCanSeeTableRecords($records); -})->group('filters', 'table'); +}){{ RESOLVED_GROUP_METHOD }}; diff --git a/stubs/Page/Index/Table/Summaries/Average.stub b/stubs/Page/Index/Table/Summaries/Average.stub new file mode 100644 index 00000000..ba6f1819 --- /dev/null +++ b/stubs/Page/Index/Table/Summaries/Average.stub @@ -0,0 +1,3 @@ +it('can average values in a column', function () { + // +}){{ RESOLVED_GROUP_METHOD }}->todo(); diff --git a/stubs/Page/Index/Table/Summaries/Count.stub b/stubs/Page/Index/Table/Summaries/Count.stub new file mode 100644 index 00000000..2c17b2e9 --- /dev/null +++ b/stubs/Page/Index/Table/Summaries/Count.stub @@ -0,0 +1,3 @@ +it('can count values in a column', function () { + // +}){{ RESOLVED_GROUP_METHOD }}->todo(); diff --git a/stubs/Page/Index/Table/Summaries/Range.stub b/stubs/Page/Index/Table/Summaries/Range.stub new file mode 100644 index 00000000..df4c038d --- /dev/null +++ b/stubs/Page/Index/Table/Summaries/Range.stub @@ -0,0 +1,3 @@ +it('can range values in a column', function () { + // +}){{ RESOLVED_GROUP_METHOD }}->todo(); diff --git a/stubs/Page/Index/Table/Summaries/Sum.stub b/stubs/Page/Index/Table/Summaries/Sum.stub new file mode 100644 index 00000000..73c52e3e --- /dev/null +++ b/stubs/Page/Index/Table/Summaries/Sum.stub @@ -0,0 +1,3 @@ +it('can sum values in a column', function () { + // +}){{ RESOLVED_GROUP_METHOD }}->todo();