Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
add new crudtrait command, remove non-closure from conditionals, refa…
Browse files Browse the repository at this point in the history
…ctor stubs, separate timestamps for easier indexing, force db seeding for production
  • Loading branch information
redbastie committed Oct 26, 2020
1 parent 0a4ea53 commit 15e455b
Show file tree
Hide file tree
Showing 18 changed files with 332 additions and 8 deletions.
6 changes: 6 additions & 0 deletions readme.md
Expand Up @@ -72,6 +72,12 @@ Generate CRUD scaffolding for a new model:

This will create the model, factory, nav item, and CRUD components.

Generate CRUD for a new model with a shared trait for form fields & rules:

php artisan make:crudtrait ModelName

This will create the model, factory, nav item, CRUD components and trait.

Generate a new Swift model:

php artisan make:swiftmodel ModelName
Expand Down
Expand Up @@ -13,7 +13,8 @@ class Create extends SwiftComponent
public function view()
{
return S::form(
S::modal('create-modal')->fade()->heading('Create DummyTitle')
S::modal('create-modal')->fade()
->heading('Create DummyTitle')
->body(
S::input('name')->label('Name')->modelDefer(),
)
Expand Down
Expand Up @@ -14,7 +14,8 @@ class Read extends SwiftComponent

public function view()
{
return S::modal('read-modal')->fade()->heading('DummyTitle')
return S::modal('read-modal')->fade()
->heading('DummyTitle')
->body(
S::each($this->dummyVariable ? $this->dummyVariable->toArray() : [], function ($value, $key) {
return S::div(
Expand Down
Expand Up @@ -14,7 +14,8 @@ class Update extends SwiftComponent
public function view()
{
return S::form(
S::modal('update-modal')->fade()->heading('Update DummyTitle')
S::modal('update-modal')->fade()
->heading('Update DummyTitle')
->body(
S::input('name')->label('Name')->modelDefer(),
)
Expand Down
3 changes: 2 additions & 1 deletion resources/stubs/crud/app/Models/DummyModel.php.stub
Expand Up @@ -15,7 +15,8 @@ class DummyModel extends Model
{
$table->id();
$table->string('name');
$table->timestamps();
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
}

public function definition(Generator $faker)
Expand Down
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Livewire\DummyModels;

use App\Models\DummyModel;
use Redbastie\Swift\Components\SwiftComponent as S;
use Redbastie\Swift\Livewire\SwiftComponent;

class Create extends SwiftComponent
{
use Crud;

protected $listeners = ['showCreateModal'];

public function view()
{
return S::form(
S::modal('create-modal')->fade()
->heading('Create DummyTitle')
->body($this->formFields())
->footer(
S::button('Cancel')->secondary()->click('$emit', 'hideModal', 'create-modal'),
S::button('Save')->submit()->primary()
)
)->submitPrevent('save');
}

public function showCreateModal()
{
$this->reset('model');
$this->emit('showModal', 'create-modal');
}

public function save()
{
$validatedData = $this->validate();

DummyModel::query()->create($validatedData);

$this->emit('hideModal', 'create-modal');
$this->emit('toastSuccess', 'DummyTitle created!');
$this->emit('$refresh');
}
}
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Livewire\DummyModels;

use Redbastie\Swift\Components\SwiftComponent as S;

trait Crud
{
public $dummyVariable;

protected function formFields()
{
return S::div(
S::input('name')->label('Name')->modelDefer(),
);
}

protected function rules()
{
return [
'name' => ['required'],
];
}
}
@@ -0,0 +1,74 @@
<?php

namespace App\Http\Livewire\DummyModels;

use App\Models\DummyModel;
use Redbastie\Swift\Components\SwiftComponent as S;
use Redbastie\Swift\Livewire\SwiftComponent;

class Index extends SwiftComponent
{
public $routeUri = '/dummy-routes';
public $routeName = 'dummy-routes';
public $routeMiddleware = 'auth';
public $pageTitle = 'DummyTitles';
protected $listeners = ['$refresh'];

public function query()
{
$query = DummyModel::query();

if (!empty($this->model['search'])) {
$query->where('name', 'like', '%' . $this->model['search'] . '%');
}

return $query->orderBy('name');
}

public function view()
{
$dummyVariables = $this->query()->paginate();

return S::div(
S::livewire('layouts.navbar'),

S::container(
S::heading($this->pageTitle),

S::row(
S::col(S::input('search')->type('search')->placeholder('Search')->modelDebounce())->mdAuto()->mb(3),
S::col(S::button('Create DummyTitle')->primary()->click('$emit', 'showCreateModal'))->mdAuto()->mb(3)
)->justifyContentBetween(),

S::listGroup(
S::each($dummyVariables, function ($dummyVariable) {
return S::listGroupItem(
S::row(
S::col(e($dummyVariable->name))->md(),
S::col(
S::button(S::icon('eye'))->title('Read')->link()->p(1)->click('$emit', 'showReadModal', $dummyVariable->id),
S::button(S::icon('edit'))->title('Update')->link()->p(1)->click('$emit', 'showUpdateModal', $dummyVariable->id),
S::button(S::icon('trash-alt'))->title('Delete')->link()->p(1)->click('delete', $dummyVariable->id)->confirm(),
)->mdAuto()
)->alignItemsCenter()
)->action();
})->empty(
S::listGroupItem('No DummyTitles to display.')
)
)->mb(3),

S::pagination($dummyVariables)
)->py(4),

S::livewire('dummy-routes.create'),
S::livewire('dummy-routes.read'),
S::livewire('dummy-routes.update')
);
}

public function delete(DummyModel $dummyVariable)
{
$dummyVariable->delete();
$this->emit('toastSuccess', 'DummyTitle deleted!');
}
}
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Livewire\DummyModels;

use App\Models\DummyModel;
use Illuminate\Support\Str;
use Redbastie\Swift\Components\SwiftComponent as S;
use Redbastie\Swift\Livewire\SwiftComponent;

class Read extends SwiftComponent
{
public $dummyVariable;
protected $listeners = ['showReadModal'];

public function view()
{
return S::modal('read-modal')->fade()
->heading('DummyTitle')
->body(
S::each($this->dummyVariable ? $this->dummyVariable->toArray() : [], function ($value, $key) {
return S::div(
S::label(Str::title(str_replace('_', ' ', Str::snake($key))))->textMuted(),

is_array($value) ?
S::pre(json_encode($value, JSON_PRETTY_PRINT)) :
S::paragraph($value ?? 'N/A')
);
})
)
->footer(
S::button('Close')->secondary()->click('$emit', 'hideModal', 'read-modal')
);
}

public function showReadModal(DummyModel $dummyVariable)
{
$this->dummyVariable = $dummyVariable;
$this->emit('showModal', 'read-modal');
}
}
@@ -0,0 +1,45 @@
<?php

namespace App\Http\Livewire\DummyModels;

use App\Models\DummyModel;
use Redbastie\Swift\Components\SwiftComponent as S;
use Redbastie\Swift\Livewire\SwiftComponent;

class Update extends SwiftComponent
{
use Crud;

protected $listeners = ['showUpdateModal'];

public function view()
{
return S::form(
S::modal('update-modal')->fade()
->heading('Update DummyTitle')
->body($this->formFields())
->footer(
S::button('Cancel')->secondary()->click('$emit', 'hideModal', 'update-modal'),
S::button('Save')->submit()->primary()
)
)->submitPrevent('save');
}

public function showUpdateModal(DummyModel $dummyVariable)
{
$this->dummyVariable = $dummyVariable;
$this->model = $dummyVariable->toArray();
$this->emit('showModal', 'update-modal');
}

public function save()
{
$validatedData = $this->validate();

$this->dummyVariable->update($validatedData);

$this->emit('hideModal', 'update-modal');
$this->emit('toastSuccess', 'DummyTitle updated!');
$this->emit('$refresh');
}
}
28 changes: 28 additions & 0 deletions resources/stubs/crudtrait/app/Models/DummyModel.php.stub
@@ -0,0 +1,28 @@
<?php

namespace App\Models;

use Faker\Generator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Redbastie\Swift\Traits\SwiftModel;

class DummyModel extends Model
{
use SwiftModel;

public function migration(Blueprint $table)
{
$table->id();
$table->string('name');
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
}

public function definition(Generator $faker)
{
return [
'name' => $faker->name,
];
}
}
16 changes: 16 additions & 0 deletions resources/stubs/crudtrait/database/factories/DummyFactory.php.stub
@@ -0,0 +1,16 @@
<?php

namespace Database\Factories;

use App\Models\DummyModel;
use Illuminate\Database\Eloquent\Factories\Factory;

class DummyFactory extends Factory
{
protected $model = DummyModel::class;

public function definition()
{
return app($this->model)->definition($this->faker);
}
}
3 changes: 2 additions & 1 deletion resources/stubs/install/app/Models/User.php.stub
Expand Up @@ -23,7 +23,8 @@ class User extends Authenticatable
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
}

public function definition(Generator $faker)
Expand Down
3 changes: 2 additions & 1 deletion resources/stubs/swiftmodel/app/Models/DummyModel.php.stub
Expand Up @@ -15,7 +15,8 @@ class DummyModel extends Model
{
$table->id();
$table->string('name');
$table->timestamps();
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
}

public function definition(Generator $faker)
Expand Down
39 changes: 39 additions & 0 deletions src/Commands/MakeCrudTraitCommand.php
@@ -0,0 +1,39 @@
<?php

namespace Redbastie\Swift\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;

class MakeCrudTraitCommand extends Command
{
use CreatesFiles;

protected $signature = 'make:crudtrait {model}';

public function handle()
{
$this->argument = $this->argument('model');
$this->stubDir = __DIR__ . '/../../resources/stubs/crudtrait';

$this->createFiles();

$navbar = 'app/Http/Livewire/Layouts/Navbar.php';

if ($this->filesystem->exists($navbar)) {
$navbarContents = $this->filesystem->get($navbar);
$navItem = "S::navItem(S::navLink('" . $this->replaces['DummyTitles'] . "')->href(route('" . $this->replaces['dummy-routes'] . "'))),";

if (!Str::contains($navbarContents, $navItem)) {
$hook = '// crud command hook';
preg_match('/(.*)' . str_replace('/', '\/', $hook) . '/', $navbarContents, $indent);

$this->filesystem->put($navbar, str_replace($hook, $navItem . PHP_EOL . $indent[1] . $hook, $this->filesystem->get($navbar)));
$this->info('Nav item inserted: ' . $navbar);
}
}

$this->info($this->argument . ' CRUD + Trait generated!');
$this->warn("Don't forget to update the new model migration and run the <info>migrate:auto</info> command.");
}
}
2 changes: 1 addition & 1 deletion src/Commands/MigrateAutoCommand.php
Expand Up @@ -52,7 +52,7 @@ public function handle()
$this->info('Migration complete!');

if ($this->option('seed')) {
Artisan::call('db:seed');
Artisan::call('db:seed --force');

$this->info('Seeding complete!');
}
Expand Down

0 comments on commit 15e455b

Please sign in to comment.