Skip to content

Commit

Permalink
fixes for 1.0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur LORENT committed Feb 21, 2019
1 parent 6cdfeba commit 2a72aae
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 102 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [1.0.9](https://github.com/Okipa/laravel-table/releases/tag/1.0.9)
2019-02-21
- Updated design in order to respect the bootstrap basics.
- Updated config architecture to improve the logic.
- The `edit` and `destroy` buttons are now hidden when a line is disabled.
- Improved compatibility with `postgres` for the searching action, using `ILIKE` instead of `LIKE` operator for case-insensitive searching.

## [1.0.8](https://github.com/Okipa/laravel-table/releases/tag/1.0.8)
2019-02-21
- Updated the result displaying in one and only `td` html tag : the title is displayed on the left and the result html on the right.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function index(Request $request) {
<h3 id="table-rowsNumber">->rowsNumber</h3>

> Override the number of rows to display on the table.
> The default number of displayed rows is defined in the `config('laravel-table.rows.number.default')` config value.
> The default number of displayed rows is defined in the `config('laravel-table.value.rowsNumber')` config value.
**Note :**
- Signature : `rowsNumber(int $rows): \Okipa\LaravelTable\Table`
Expand All @@ -199,7 +199,7 @@ public function index(Request $request) {

> Override the default rows number selection activation status.
> Calling this method displays a rows number input that enable the user to choose how much rows to show.
> The default rows number selection activation status is managed by the `config('laravel-table.rows.number.selection')` value.
> The default rows number selection activation status is managed by the `config('laravel-table.value.rowsNumberSelectionActivation')` value.
**Note :**`
- Signature : `rowsNumberSelectionActivation($activate = true): \Okipa\LaravelTable\Table`
Expand Down Expand Up @@ -371,7 +371,7 @@ destroyButton.click(function(e){

> Set the disable lines closure that will be executed during the table generation.
> The optional second param let you override the classes that will be applied for the disabled lines.
> By default, the « config('laravel-table.rows.disabled.classes') » config value is applied.
> By default, the « config('laravel-table.classes.disabled') » config value is applied.
> For example, you can disable the current logged user to prevent him being edited or deleted from the table.
> The closure let you manipulate the following attribute : `$model`.
Expand Down
18 changes: 7 additions & 11 deletions config/laravel-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
return [

'classes' => [
'container' => ['table-responsive', 'pt-3'],
'table' => ['table-striped', 'table-hover', 'mt-3'],
'container' => ['table-responsive'],
'table' => ['table-striped', 'table-hover'],
'tr' => [],
'th' => ['align-middle'],
'td' => ['align-middle'],
'results' => ['table-secondary', 'font-weight-bold'],
'results' => ['table-dark', 'font-weight-bold'],
'disabled' => ['table-danger', 'disabled'],
],

'icon' => [
Expand All @@ -24,14 +25,9 @@
'destroy' => '<i class="fas fa-trash fa-fw"></i>',
],

'rows' => [
'number' => [
'default' => 20,
'selection' => true,
],
'disabled' => [
'classes' => ['disabled', 'bg-secondary', 'text-white'],
],
'value' => [
'rowsNumber' => 20,
'rowsNumberSelectionActivation' => true,
],

'template' => [
Expand Down
31 changes: 22 additions & 9 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function __construct()
{
$this->initializeDefaultComponents();
$this->initializeTableDefaultClasses();
$this->rows = config('laravel-table.rows.number.default');
$this->rowsNumberSelectionActivation = config('laravel-table.rows.number.selection');
$this->rows = config('laravel-table.value.rowsNumber');
$this->rowsNumberSelectionActivation = config('laravel-table.value.rowsNumberSelectionActivation');
$this->sortableColumns = new Collection();
$this->searchableColumns = new Collection();
$this->request = request();
Expand Down Expand Up @@ -102,7 +102,7 @@ public function routes(array $routes): Table

/**
* Override the number of rows to display on the table.
* The default number of displayed rows is defined in the config('laravel-table.rows.number.default') config value.
* The default number of displayed rows is defined in the config('laravel-table.value.rowsNumber') config value.
*
* @param int $rows
*
Expand All @@ -119,7 +119,7 @@ public function rowsNumber(int $rows): Table
* Override the default rows number selection activation status.
* Calling this method displays a rows number input that enable the user to choose how much rows to show.
* The default rows number selection activation status is defined in the
* config('laravel-table.rows.number.selection') config value.
* config('laravel-table.value.rowsNumberSelectionActivation') config value.
*
* @param bool $activate
*
Expand Down Expand Up @@ -161,7 +161,7 @@ public function disableRows(Closure $rowDisableClosure, array $classes = []): Ta
{
$this->disableRows->push([
'closure' => $rowDisableClosure,
'classes' => ! empty($classes) ? $classes : config('laravel-table.rows.disabled.classes'),
'classes' => ! empty($classes) ? $classes : config('laravel-table.classes.disabled'),
]);

return $this;
Expand Down Expand Up @@ -413,15 +413,15 @@ protected function applySearchClauses(Builder $query): void
$databaseSearchedTable = $column->databaseSearchedTable
? $column->databaseSearchedTable
: $column->databaseDefaultTable;
$operator = $columnKey > 0 ? 'orWhere' : 'where';
$whereOperator = $columnKey > 0 ? 'orWhere' : 'where';
$databaseSearchedColumns = $column->databaseSearchedColumns
? $column->databaseSearchedColumns
: [$column->databaseDefaultColumn];
foreach ($databaseSearchedColumns as $searchedDatabaseColumnKey => $searchedDatabaseColumn) {
$operator = $searchedDatabaseColumnKey > 0 ? 'orWhere' : $operator;
$subQuery->{$operator}(
$whereOperator = $searchedDatabaseColumnKey > 0 ? 'orWhere' : $whereOperator;
$subQuery->{$whereOperator}(
$databaseSearchedTable . '.' . $searchedDatabaseColumn,
'like',
$this->casInsensitiveLikeOperator(),
'%' . $searched . '%'
);
}
Expand All @@ -430,6 +430,19 @@ protected function applySearchClauses(Builder $query): void
}
}

/**
* Get insensitive like operator according to the used database driver.
*
* @return string
*/
protected function casInsensitiveLikeOperator(): string
{
$connection = config('database.default');
$driver = config('database.connections.' . $connection . '.driver');

return in_array($driver, ['pgsql']) ? 'ILIKE' : 'LIKE';
}

/**
* Apply sort clauses.
*
Expand Down
16 changes: 6 additions & 10 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testConfigStructure()
// laravel-table
$this->assertTrue(array_key_exists('classes', config('laravel-table')));
$this->assertTrue(array_key_exists('icon', config('laravel-table')));
$this->assertTrue(array_key_exists('rows', config('laravel-table')));
$this->assertTrue(array_key_exists('value', config('laravel-table')));
$this->assertTrue(array_key_exists('template', config('laravel-table')));
// laravel-table.classes
$this->assertTrue(array_key_exists('container', config('laravel-table.classes')));
Expand All @@ -22,6 +22,7 @@ public function testConfigStructure()
$this->assertTrue(array_key_exists('th', config('laravel-table.classes')));
$this->assertTrue(array_key_exists('td', config('laravel-table.classes')));
$this->assertTrue(array_key_exists('results', config('laravel-table.classes')));
$this->assertTrue(array_key_exists('disabled', config('laravel-table.classes')));
// laravel-table.icon
$this->assertTrue(array_key_exists('rowsNumber', config('laravel-table.icon')));
$this->assertTrue(array_key_exists('sort', config('laravel-table.icon')));
Expand All @@ -33,14 +34,9 @@ public function testConfigStructure()
$this->assertTrue(array_key_exists('create', config('laravel-table.icon')));
$this->assertTrue(array_key_exists('edit', config('laravel-table.icon')));
$this->assertTrue(array_key_exists('destroy', config('laravel-table.icon')));
// laravel-table.rows
$this->assertTrue(array_key_exists('number', config('laravel-table.rows')));
$this->assertTrue(array_key_exists('disabled', config('laravel-table.rows')));
// laravel-table.rows.number
$this->assertTrue(array_key_exists('default', config('laravel-table.rows.number')));
$this->assertTrue(array_key_exists('selection', config('laravel-table.rows.number')));
// laravel-table.rows.classes
$this->assertTrue(array_key_exists('classes', config('laravel-table.rows.disabled')));
// laravel-table.value
$this->assertTrue(array_key_exists('rowsNumber', config('laravel-table.value')));
$this->assertTrue(array_key_exists('rowsNumberSelectionActivation', config('laravel-table.value')));
// laravel-table.template
$this->assertTrue(array_key_exists('table', config('laravel-table.template')));
$this->assertTrue(array_key_exists('thead', config('laravel-table.template')));
Expand All @@ -50,7 +46,7 @@ public function testConfigStructure()

public function testCustomDefaultValueRowsNumber()
{
config()->set('laravel-table.rows.number.default', 9999);
config()->set('laravel-table.value.rowsNumber', 9999);
$this->createMultipleUsers(3);
$this->routes(['users'], ['index', 'create', 'edit', 'destroy']);
$table = (new Table)->model(User::class)->routes([
Expand Down
28 changes: 19 additions & 9 deletions tests/Unit/RowDisableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testDisableLineWithDefaultClassHtml()
return $model->id === 1 || $model->id === 2;
};
$classes = ['test-disabled-default-class'];
config()->set('laravel-table.rows.disabled.classes', $classes);
config()->set('laravel-table.classes.disabled', $classes);
$table = (new Table)->model(User::class)
->routes([
'index' => ['name' => 'users.index'],
Expand All @@ -46,9 +46,15 @@ public function testDisableLineWithDefaultClassHtml()
}
$html = view('laravel-table::' . $table->tbodyComponentPath, compact('table'))->render();
$this->assertContains(implode(' ', $classes), $html);
$this->assertContains('disabled="disabled"', $html);
$this->assertEquals(2, substr_count($html, implode(' ', $classes)));
$this->assertEquals(4, substr_count($html, 'disabled="disabled"'));
foreach ($users as $user) {
if($user->id === 1 || $user->id === 2) {
$this->assertNotContains('edit-' . $user->id, $html);
$this->assertNotContains('action="http://localhost/users/edit?id=' . $user->id . '"', $html);
} else {
$this->assertContains('edit-' . $user->id, $html);
$this->assertContains('action="http://localhost/users/edit?id=' . $user->id . '"', $html);
}
}
}

public function testDisableLineWithCustomClassHtml()
Expand Down Expand Up @@ -77,11 +83,15 @@ public function testDisableLineWithCustomClassHtml()
}
$html = view('laravel-table::' . $table->tbodyComponentPath, compact('table'))->render();
$this->assertContains(implode(' ', $classes), $html);
$this->assertContains('disabled', $html);
$this->assertContains('disabled="disabled"', $html);
$this->assertEquals(2, substr_count($html, implode(' ', $classes)));
$this->assertEquals(14, substr_count($html, 'disabled'));
$this->assertEquals(4, substr_count($html, 'disabled="disabled"'));
foreach ($users as $user) {
if($user->id === 1 || $user->id === 2) {
$this->assertNotContains('edit-' . $user->id, $html);
$this->assertNotContains('action="http://localhost/users/edit?id=' . $user->id . '"', $html);
} else {
$this->assertContains('edit-' . $user->id, $html);
$this->assertContains('action="http://localhost/users/edit?id=' . $user->id . '"', $html);
}
}
}

public function testWithNoDisableLinesHtml()
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/RowsNumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testSetRowsNumberAttribute()

public function testDeactivateRowsNumberSelectionFromConfigHtml()
{
config()->set('laravel-table.rows.number.selection', false);
config()->set('laravel-table.value.rowsNumberSelectionActivation', false);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)->routes(['index' => ['name' => 'users.index']]);
$table->column('name')->title('Name');
Expand All @@ -43,7 +43,7 @@ public function testDeactivateRowsNumberSelectionFromConfigHtml()

public function testDeactivateRowsNumberSelectionFromMethodHtml()
{
config()->set('laravel-table.rows.number.selection', true);
config()->set('laravel-table.value.rowsNumberSelectionActivation', true);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)
->routes(['index' => ['name' => 'users.index']])
Expand All @@ -64,7 +64,7 @@ public function testDeactivateRowsNumberSelectionFromMethodHtml()

public function testActivateRowsNumberSelectionHtml()
{
config()->set('laravel-table.rows.number.selection', false);
config()->set('laravel-table.value.rowsNumberSelectionActivation', false);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)
->routes(['index' => ['name' => 'users.index']])
Expand Down
23 changes: 22 additions & 1 deletion tests/Unit/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Okipa\LaravelTable\Test\LaravelTableTestCase;
use Okipa\LaravelTable\Test\Models\Company;
use Okipa\LaravelTable\Test\Models\User;
use PDOException;

class SearchTest extends LaravelTableTestCase
{
Expand Down Expand Up @@ -405,7 +406,7 @@ public function testSearchWrappedInSubWhereQuery()
$this->assertEmpty($table->list->toArray()['data']);
}

public function testCaseInsensitiveTestHtml()
public function testSqliteCaseInsensitiveTestHtml()
{
$users = $this->createMultipleUsers(10);
$users->each(function ($user, $key) {
Expand All @@ -430,4 +431,24 @@ public function testCaseInsensitiveTestHtml()
return in_array($user->name, ['alpha', 'ALPHA']);
})->toArray(), $table->list->toArray()['data']);
}

public function testPostgresCaseInsensitiveTestHtml()
{
$this->expectException(PDOException::class);
$this->expectExceptionMessage('SQLSTATE[HY000]: General error: 1 near "ILIKE": syntax error (SQL: select '
. 'count(*) as aggregate from "users_test" where ("users_test"."name" ILIKE '
. '%alpha% or "users_test"."email" ILIKE %alpha%))');
$connection = config('database.default');
config()->set('database.connections.' . $connection . '.driver', 'pgsql');
$this->createMultipleUsers(10);
$searchedValue = 'alpha';
$customRequest = (new Request)->merge(['rows' => 20, 'search' => $searchedValue]);
$this->routes(['users'], ['index']);
$table = (new Table)->model(User::class)
->routes(['index' => ['name' => 'users.index']])
->request($customRequest);
$table->column('name')->title('Name')->searchable();
$table->column('email')->title('Email')->searchable();
$table->render();
}
}
Loading

0 comments on commit 2a72aae

Please sign in to comment.