Skip to content

Commit

Permalink
Rework service providet, readme, static methods have own class
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyslik committed Sep 7, 2016
1 parent 48f7093 commit d190b15
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 182 deletions.
83 changes: 44 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,33 @@ Add the package to your application service providers in `config/app.php`

Publish the package configuration file to your application.

$ php artisan vendor:publish --provider="Kyslik\ColumnSortable\ColumnSortableServiceProvider" --tag="columnsortable"
$ php artisan vendor:publish --provider="Kyslik\ColumnSortable\ColumnSortableServiceProvider" --tag="config"

See configuration file [(`config/columnsortable.php`)](https://github.com/Kyslik/column-sortable/blob/master/src/config/columnsortable.php) yourself and make adjustments as you wish.

# Usage

Use `Sortable` trait inside your `Eloquent` model(s). Define `$sortable` array (see example code below).

>`Scheme::hasColumn()` is run only when `$sortable` is not defined - less DB hits per request.
> **Note**: `Scheme::hasColumn()` is run only when `$sortable` is not defined - less DB hits per request.

```
use Kyslik\ColumnSortable\Sortable;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, Sortable;
...
protected $sortable = ['id',
'name',
'email',
'created_at',
'updated_at'];
public $sortable = ['id',
'name',
'email',
'created_at',
'updated_at'];
...
}
```

You're set to go.
Expand All @@ -110,19 +113,19 @@ Sortablelink blade extension distinguishes between "types" (numeric, amount and

```
'columns' => [
'numeric' => [
'rows' => ['created_at', 'updated_at', 'level', 'id'],
'class' => 'fa fa-sort-numeric'
],
'amount' => [
'rows' => ['price'],
'class' => 'fa fa-sort-amount'
],
'alpha' => [
'rows' => ['name', 'description', 'email', 'slug'],
'class' => 'fa fa-sort-alpha',
],
'numeric' => [
'rows' => ['created_at', 'updated_at', 'level', 'id'],
'class' => 'fa fa-sort-numeric'
],
'amount' => [
'rows' => ['price'],
'class' => 'fa fa-sort-amount'
],
'alpha' => [
'rows' => ['name', 'description', 'email', 'slug'],
'class' => 'fa fa-sort-alpha',
],
],
```

Rest of the [config file](https://github.com/Kyslik/column-sortable/blob/master/src/config/columnsortable.php) should be crystal clear and I advise you to read it.
Expand All @@ -145,25 +148,27 @@ Route::get('users', ['as' => 'users.index', 'uses' => 'HomeController@index']);
public function index(User $user)
{
$users = $user->sortable()->paginate(10);
return view('user.index')->withUsers($users);
return view('user.index')->withUsers($users);
}
```

You can set default sort (when nothing is in (URL) query strings yet).
You can set default sort when nothing is in (URL) query strings yet.
> **For example**: page is loaded for first time, default order is [configurable](https://github.com/Kyslik/column-sortable/blob/master/src/config/columnsortable.php#L77) (asc)
```
$users = $user->sortable('name')->paginate(10);
//generate ->orderBy('name', 'asc')
$users = $user->sortable(['name'])->paginate(10);
//generate ->orderBy('name', 'asc')
$users = $user->sortable(['name'])->paginate(10); //default order is asc
//generate ->orderBy('id', 'desc')
$users = $user->sortable(['id' => 'desc'])->paginate(10);
$users = $user->sortable(['name' => 'desc'])->paginate(10);
//generate ->orderBy('name', 'desc')
```

### View

In Laravel 5.2 and 5.3 **\Input** facade is not aliased by default. To do so, open `config/app.php` and add `'Input' => Illuminate\Support\Facades\Input::class,` to *aliases* array.

_pagination included_

```
Expand All @@ -173,36 +178,38 @@ _pagination included_
@foreach ($users as $user)
{{ $user->name }}
@endforeach
{!! $users->appends(\Input::except('page'))->render() !!}
{!! $users->appends(\Request::except('page'))->render() !!}
```

>**Note**: Blade's ability to recognize directives depends on having space before directive itself `<tr> @sortablelink('Name')`
# One To One Relation sorting

## Define HasOne relation
## Define hasOne relation

In order to make relation sorting work, you have to define **hasOne()** relation in your model in question.
In order to make relation sorting work, you have to define **hasOne()** relation in your model.

```
/**
* Get the user_detail record associated with the user.
*/
public function detail()
{
return $this->hasOne('App\UserDetail');
return $this->hasOne(App\UserDetail::class);
}
```

In *User* model we define **hasOne** relation to *UserDetail* model (which holds phone number and address details).

## Define `$sortable` array
## Define `$sortable` arrays

Define `$sortable` array in both models (else, package uses `Scheme::hasColumn()` which is extra database query).


for *User*

```
protected $sortable = ['id', 'name', 'email', 'created_at', 'updated_at'];
public $sortable = ['id', 'name', 'email', 'created_at', 'updated_at'];
```

for *UserDetail*
Expand All @@ -211,16 +218,14 @@ for *UserDetail*
public $sortable = ['address', 'phone_number'];
```

>note that `$sortable` array in *UserDetail* is declared as **public** and not protected because we need to access it inside *User* model.
## Blade and relation sorting

In order to tell package to sort using relation:

```
@sortablelink ('detail.phone_number', 'phone')
```
>package works with relation "name" that you define in model instead of table name.
>**Note**: package works with relation "name" (method) that you define in model instead of table name.
In config file you can set your own separator if `.` (dot) is not what you want.

Expand Down Expand Up @@ -249,4 +254,4 @@ try {
}
```

>I strongly recommend to catch **ColumnSortableException** because there is a user input in question (GET parameter) and any user can modify it in such way that package throws ColumnSortableException with code 0.
>**Note**: I strongly recommend to catch **ColumnSortableException** because there is a user input in question (GET parameter) and any user can modify it in such way that package throws ColumnSortableException with code 0.
6 changes: 2 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
stopOnFailure="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="Test.php">./tests/</directory>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
41 changes: 17 additions & 24 deletions src/ColumnSortable/ColumnSortableServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php namespace Kyslik\ColumnSortable;
<?php

namespace Kyslik\ColumnSortable;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

/**
* Class ColumnSortableServiceProvider
* @package Kyslik\ColumnSortable
*/
class ColumnSortableServiceProvider extends ServiceProvider
{

/**
* Indicates if loading of the provider is deferred.
*
Expand All @@ -19,9 +25,14 @@ class ColumnSortableServiceProvider extends ServiceProvider
*/
public function boot()
{
$config_path = __DIR__ . '/../config/columnsortable.php';
$this->publishes([$config_path => config_path('columnsortable.php')], 'columnsortable');
$this->registerBladeExtensions();
$this->publishes([
__DIR__ . '/../config/columnsortable.php' => config_path('columnsortable.php')
], 'config');

Blade::directive('sortablelink', function ($expression) {
$expression = ($expression[0] === '(') ? substr($expression, 1, -1) : $expression;
return "<?php echo \Kyslik\ColumnSortable\SortableLink::render({$expression});?>";
});
}

/**
Expand All @@ -31,24 +42,6 @@ public function boot()
*/
public function register()
{
$config_path = __DIR__ . '/../config/columnsortable.php';
$this->mergeConfigFrom($config_path, 'columnsortable');
}

/**
* Register Blade extensions.
*
* @return void
*/
protected function registerBladeExtensions()
{
$blade = $this->app['view']->getEngineResolver()->resolve('blade')->getCompiler();

$blade->directive('sortablelink', function ($expression) {
if ($expression[0] === '(') {
return "<?php echo \Kyslik\ColumnSortable\Sortable::link(array {$expression});?>";
}
return "<?php echo \Kyslik\ColumnSortable\Sortable::link(array ({$expression}));?>";
});
$this->mergeConfigFrom(__DIR__ . '/../config/columnsortable.php', 'columnsortable');
}
}
Loading

0 comments on commit d190b15

Please sign in to comment.