Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Harmonic/InertiaTable
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Harman committed Jul 4, 2019
2 parents fd4dcef + 9d00002 commit d18525e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 65 deletions.
2 changes: 1 addition & 1 deletion config/inertiatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

return [
//
];
];
60 changes: 31 additions & 29 deletions src/Commands/MakeInertiaTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace harmonic\InertiaTable\Commands;

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Illuminate\Support\Str;
use File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class MakeInertiaTable extends Command
{
Expand Down Expand Up @@ -37,13 +37,14 @@ public function __construct()
parent::__construct();
}

private function inertiaTableJSExists() {
private function inertiaTableJSExists()
{
$process = new Process('npm list --depth=0 | grep inertia-table');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
if ($process->getOutput()=="") {
if (! $process->isSuccessful()) {
if ($process->getOutput() == '') {
return false;
}
$this->error('Failed looking for inertia-table JS package. Is inertia installed?');
Expand All @@ -54,6 +55,7 @@ private function inertiaTableJSExists() {
if (strpos($result, ' inertia-table@') !== false) {
return true;
}

return false;
}

Expand All @@ -66,43 +68,43 @@ public function handle()
{
$model = ucfirst($this->argument('model'));

if (!file_exists(app_path($model).'.php') && !file_exists(app_path('Models\\' . $model).'.php')) {
if (! file_exists(app_path($model).'.php') && ! file_exists(app_path('Models\\'.$model).'.php')) {
// Create Model
$str = file_get_contents(__DIR__ . '/../Stubs/Model.php');
$str = str_replace("@namespace", substr($this->getAppNamespace(), 0, -1), $str);
$str = str_replace("@modelName", $model, $str);
file_put_contents(app_path($model . '.php'), $str);
$str = file_get_contents(__DIR__.'/../Stubs/Model.php');
$str = str_replace('@namespace', substr($this->getAppNamespace(), 0, -1), $str);
$str = str_replace('@modelName', $model, $str);
file_put_contents(app_path($model.'.php'), $str);
}

// Create Controller
$pluralName = Str::plural($model);
$controllerName = $pluralName . 'Controller';
$str = file_get_contents(__DIR__ . '/../Stubs/Controller.php');
$str = str_replace("@namespace", substr($this->getAppNamespace(), 0, -1), $str);
$str = str_replace("@controllerName", $controllerName, $str);
$str = str_replace("@modelName", $model, $str);
file_put_contents(app_path('Http/Controllers/' . $controllerName . '.php'), $str);
$controllerName = $pluralName.'Controller';
$str = file_get_contents(__DIR__.'/../Stubs/Controller.php');
$str = str_replace('@namespace', substr($this->getAppNamespace(), 0, -1), $str);
$str = str_replace('@controllerName', $controllerName, $str);
$str = str_replace('@modelName', $model, $str);
file_put_contents(app_path('Http/Controllers/'.$controllerName.'.php'), $str);

// Add routes
$this->warn('You need to manually add route: Route::inertia(\''. Str::plural(strtolower($model)) . '\');');
$this->warn('You need to manually add route: Route::inertia(\''.Str::plural(strtolower($model)).'\');');

if ($this->inertiaTableJSExists()) {
$pluralNameLowercase = \strtolower($pluralName);
$modelLowercase = \strtolower($model);

// Get column names from table
$class = 'App\\' . $model;
$class = 'App\\'.$model;
$columns = Schema::getColumnListing(with(new $class)->getTable());

// Create an InertiaTable.vue component and then create Index.vue using it
$tableElementPath = resource_path('js/Pages/' . $pluralName);
$tableElementPath = resource_path('js/Pages/'.$pluralName);
File::makeDirectory($tableElementPath, 0755, true);
$str = file_get_contents(__DIR__ . '/../Stubs/Index.vue');
$str = str_replace("@pluralNameLowercase", $pluralNameLowercase, $str);
$str = str_replace("@plural", $pluralName, $str);
$str = str_replace("@columns", implode(",", $columns), $str);
$str = str_replace("@modelLowercase", $modelLowercase, $str);
file_put_contents($tableElementPath . '/Index.vue', $str);
$str = file_get_contents(__DIR__.'/../Stubs/Index.vue');
$str = str_replace('@pluralNameLowercase', $pluralNameLowercase, $str);
$str = str_replace('@plural', $pluralName, $str);
$str = str_replace('@columns', implode(',', $columns), $str);
$str = str_replace('@modelLowercase', $modelLowercase, $str);
file_put_contents($tableElementPath.'/Index.vue', $str);
}

$this->info('Model, Controller and Vue Components successfully created.');
Expand Down
10 changes: 5 additions & 5 deletions src/InertiaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace harmonic\InertiaTable;

use harmonic\InertiaTable\Traits\InertiaTable;
use Illuminate\Database\Eloquent\Model;
use harmonic\InertiaTable\Traits\InertiaTable;

class InertiaModel extends Model {
use InertiaTable;

}
class InertiaModel extends Model
{
use InertiaTable;
}
15 changes: 8 additions & 7 deletions src/InertiaTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace harmonic\InertiaTable;

use harmonic\InertiaTable\InertiaModel;
use Inertia\Inertia;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Schema;
Expand All @@ -11,21 +10,22 @@
class InertiaTable
{
/**
* Generates inertia view data for model
* Generates inertia view data for model.
*
* @param InertiaModel $model The model to use to retrieve data
* @param array $columns An array of column names to send to front end (null for all columns)
* @return void
*/
public function index(InertiaModel $model, array $columns = null) {
public function index(InertiaModel $model, array $columns = null)
{
$modelName = class_basename($model);

if ($columns == null) { // default to all columns
$table = $model->getTable();
$columns = Schema::getColumnListing($table);
}

return Inertia::render(Str::plural($modelName) . '/Index', [
return Inertia::render(Str::plural($modelName).'/Index', [
'filters' => Request::all('search', 'trashed'),
'order' => Request::all('orderColumn', 'orderDirection'),
'users' => $model
Expand All @@ -37,8 +37,9 @@ public function index(InertiaModel $model, array $columns = null) {
foreach ($columns as $column) {
$data[$column] = $item->$column;
}

return $data;
}),
]);
}
}
}
}
27 changes: 13 additions & 14 deletions src/InertiaTableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace harmonic\InertiaTable;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class InertiaTableServiceProvider extends ServiceProvider
{
protected $commands = [
'harmonic\InertiaTable\Commands\MakeInertiaTable',
];



/**
* Perform post-registration booting of services.
*
Expand All @@ -27,18 +26,18 @@ public function boot()
// Easily create all the inertia routes
Route::macro('inertia', function ($routeName) {
$routeName = strtolower($routeName);
$controller = ucfirst($routeName) . 'Controller';
$controller = ucfirst($routeName).'Controller';

Route::group([
'prefix' => '/' . $routeName,
'prefix' => '/'.$routeName,
], function () use ($controller, $routeName) {
Route::get('/')->name($routeName)->uses($controller . '@index')->middleware('remember');
Route::get('/create')->name($routeName . '.create')->uses($controller . '@create');
Route::post('/')->name($routeName . '.store')->uses($controller . '@store');
Route::get('/{user}/edit')->name($routeName . '.edit')->uses($controller . '@edit');
Route::put('/{user}')->name($routeName . '.update')->uses($controller . '@update');
Route::delete('/{user}')->name($routeName . '.destroy')->uses($controller . '@destroy');
Route::put('/{user}/restore')->name($routeName . '.restore')->uses($controller . '@restore');
Route::get('/')->name($routeName)->uses($controller.'@index')->middleware('remember');
Route::get('/create')->name($routeName.'.create')->uses($controller.'@create');
Route::post('/')->name($routeName.'.store')->uses($controller.'@store');
Route::get('/{user}/edit')->name($routeName.'.edit')->uses($controller.'@edit');
Route::put('/{user}')->name($routeName.'.update')->uses($controller.'@update');
Route::delete('/{user}')->name($routeName.'.destroy')->uses($controller.'@destroy');
Route::put('/{user}/restore')->name($routeName.'.restore')->uses($controller.'@restore');
});
});

Expand Down Expand Up @@ -74,7 +73,7 @@ public function provides()
{
return ['inertiatable'];
}

/**
* Console-specific booting.
*
Expand Down
22 changes: 13 additions & 9 deletions src/Traits/InertiaTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

namespace harmonic\InertiaTable\Traits;

trait InertiaTable {
public function resolveRouteBinding($value) {
trait InertiaTable
{
public function resolveRouteBinding($value)
{
return in_array(SoftDeletes::class, class_uses($this))
? $this->where($this->getRouteKeyName(), $value)->withTrashed()->first()
: parent::resolveRouteBinding($value);
}

/**
* Sort a table by a column asc/desc
* Sort a table by a column asc/desc.
*
* @param [type] $query
* @param string $column The name of the column to order by
* @param string $order asc or desc
* @return void
*/
public function scopeOrder($query, $column, $order = 'asc') {
public function scopeOrder($query, $column, $order = 'asc')
{
if ($order !== 'asc') {
$order = 'desc';
}
Expand All @@ -26,20 +29,21 @@ public function scopeOrder($query, $column, $order = 'asc') {
}

/**
* Search filters
* Search filters.
*
* @param [type] $query
* @param array $filters
* @param array $searchColumns An array of column names that can be searched
* @return void
*/
public function scopeFilter($query, array $filters, array $searchColumns) {
public function scopeFilter($query, array $filters, array $searchColumns)
{
$query->when($filters['search'] ?? null, function ($query, $search) use ($searchColumns) {
$query->where(function ($query) use ($search, $searchColumns) {
if (!empty($searchColumns)) {
$query->where(array_shift($searchColumns), 'like', '%' . $search . '%');
if (! empty($searchColumns)) {
$query->where(array_shift($searchColumns), 'like', '%'.$search.'%');
foreach ($searchColumns as $columnName) {
$query->orWhere($columnName, 'like', '%' . $search . '%');
$query->orWhere($columnName, 'like', '%'.$search.'%');
}
}
});
Expand Down

0 comments on commit d18525e

Please sign in to comment.