Skip to content

Commit

Permalink
Treat searchableRaw with beforeSearch (#1704)
Browse files Browse the repository at this point in the history
* Fixing search issue with Raw searchable

* Returns the value of phpunit.mysql

* Fix pgsql tests
  • Loading branch information
hecktorvn authored Sep 27, 2024
1 parent 41db144 commit 0a80362
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/Providers/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,26 @@ public static function columns(): void
return $this;
});

Column::macro('searchableRaw', function (string $sql = '') {
Column::macro('searchableRaw', function (string $sql = ''): Column {
/** @var Column $this */
$field = $this->dataField ?: $this->field;

$this->rawQueries[] = [
'method' => 'orWhereRaw',
'sql' => $sql,
'bindings' => ['%{search}%'],
'enabled' => function (PowerGridComponent $component) {
'bindings' => [function (PowerGridComponent $component) use ($field): string {
$search = $component->search;
$fieldMethod = 'beforeSearch' . str($field)->camel()->ucfirst();

if (method_exists($component, $fieldMethod)) {
$search = $component->{$fieldMethod}($field, $search);
} elseif (method_exists($component, 'beforeSearch')) {
$search = $component->beforeSearch($field, $search);
}

return "%$search%";
}],
'enabled' => function (PowerGridComponent $component) {
return filled($component->search);
},
];
Expand Down
28 changes: 27 additions & 1 deletion tests/Concerns/Components/DishesSearchableRawTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use NumberFormatter;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Dish;
use PowerComponents\LivewirePowerGrid\{Column, Facades\PowerGrid, PowerGridComponent, PowerGridFields};

Expand Down Expand Up @@ -48,6 +49,11 @@ public function fields(): PowerGridFields
return PowerGrid::fields()
->add('id')
->add('name')
->add('price_formatted', function (Dish $row): string {
$formatter = new NumberFormatter('pt_BR', NumberFormatter::CURRENCY);

return $formatter->formatCurrency($row->price, 'BRL');
})
->add('produced_at_formatted');
}

Expand All @@ -62,7 +68,11 @@ public function columns(): array

return [
Column::make('ID', 'id')
->searchable()
->searchableRaw($this->database === 'pgsql' ? 'dishes.id::text ilike ?' : 'dishes.id like ?')
->sortable(),

Column::make('Preço', 'price_formatted', 'price')
->searchableRaw($this->database === 'pgsql' ? 'price::text ilike ?' : 'price like ?')
->sortable(),

Column::make('Prato', 'name')
Expand All @@ -78,6 +88,22 @@ public function columns(): array
];
}

public function beforeSearch(string $field): string
{
return $field === 'id'
? preg_replace('/[^0-9]/', '', $this->search)
: $this->search;
}

public function beforeSearchPrice(): float
{
return (float) str_replace(
',',
'.',
preg_replace('/[^0-9,]/', '', $this->search)
);
}

public function setTestThemeClass(string $themeClass): void
{
config(['livewire-powergrid.theme' => $themeClass]);
Expand Down
45 changes: 42 additions & 3 deletions tests/Feature/SearchableRawTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@
->assertSee('Polpetone Filé Mignon')
->assertDontSee('Barco-Sushi Simples')
->assertDontSee('No records found')
# Id: 2
->set('search', '#2')
->assertSee('Peixada da chef Nábia')
->assertDontSee('Polpetone Filé Mignon')
->assertDontSee('No records found')
# 09/09/2046
->set('search', '09/09/2046') //No dishes in this date
->assertSee('No records found')
# 06/2026
->set('search', '06/2026')
->assertSee('Francesinha')
->assertDontSee('Polpetone Filé Mignon');
->assertDontSee('Polpetone Filé Mignon')
# $60.50
->set('search', 'R$ 60,50')
->assertSee('Francesinha')
->assertDontSee('Polpetone Filé Mignon')
# 1,500.40
->set('search', '1.500,40')
->assertSee('Barco-Sushi Simples')
->assertDontSee('Francesinha');
})->with('searchable_raw_themes')->requiresSQLite();

it('searches data using whereRaw on mysql', function (string $component, object $params) {
Expand All @@ -28,13 +41,26 @@
->assertSee('Polpetone Filé Mignon')
->assertDontSee('Barco-Sushi Simples')
->assertDontSee('No records found')
# Id: 2
->set('search', '#2')
->assertSee('Peixada da chef Nábia')
->assertDontSee('Polpetone Filé Mignon')
->assertDontSee('No records found')
# 09/09/2046
->set('search', '09/09/2046') //No dishes in this date
->assertSee('No records found')
# 06/2026
->set('search', '06/2026')
->assertSee('Francesinha')
->assertDontSee('Polpetone Filé Mignon');
->assertDontSee('Polpetone Filé Mignon')
# $60.50
->set('search', 'R$ 60,50')
->assertSee('Francesinha')
->assertDontSee('Polpetone Filé Mignon')
# 1,500.40
->set('search', '1.500,40')
->assertSee('Barco-Sushi Simples')
->assertDontSee('Francesinha');
})->with('searchable_raw_themes')->requiresMySQL();

it('searches data using whereRaw on pgsql', function (string $component, object $params) {
Expand All @@ -44,13 +70,26 @@
->assertSee('Polpetone Filé Mignon')
->assertDontSee('Barco-Sushi Simples')
->assertDontSee('No records found')
# Id: 2
->set('search', '#2')
->assertSee('Peixada da chef Nábia')
->assertDontSee('Polpetone Filé Mignon')
->assertDontSee('No records found')
# 09/09/2046
->set('search', '09/09/2046') //No dishes in this date
->assertSee('No records found')
# 06/2026
->set('search', '06/2026')
->assertSee('Francesinha')
->assertDontSee('Polpetone Filé Mignon');
->assertDontSee('Polpetone Filé Mignon')
# $60.50
->set('search', 'R$ 60,50')
->assertSee('Francesinha')
->assertDontSee('Polpetone Filé Mignon')
# 1,500.40
->set('search', '1.500,40')
->assertSee('Barco-Sushi Simples')
->assertDontSee('Francesinha');
})->with('searchable_raw_themes')->requiresPostgreSQL();

dataset('searchable_raw_themes', [
Expand Down

0 comments on commit 0a80362

Please sign in to comment.