Skip to content

Commit

Permalink
Fixing search issue with Raw searchable
Browse files Browse the repository at this point in the history
  • Loading branch information
hecktorvn committed Sep 27, 2024
1 parent 096a135 commit b185089
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
4 changes: 2 additions & 2 deletions phpunit.mysql.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<server name="APP_ENV" value="testing"/>
<server name="DB_DRIVER" value="mysql"/>
<server name="DB_HOST" value="127.0.0.1"/>
<server name="DB_PORT" value="3307"/>
<server name="DB_PORT" value="3306"/>
<server name="DB_USERNAME" value="root"/>
<server name="DB_PASSWORD" value="password"/>
<server name="DB_PASSWORD" value=""/>
<server name="DB_DATABASE" value="powergridtest"/>
</php>
<source>
Expand Down
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->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 ilike ?' : 'dishes.id like ?')
->sortable(),

Column::make('Preço', 'price_formatted', 'price')
->searchableRaw($this->database === 'pgsql' ? 'price 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 b185089

Please sign in to comment.