From f686ac7006800df5f2d0a239ec170ba85e3f8455 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sun, 15 Oct 2017 11:32:33 +0800 Subject: [PATCH] Always uppercase first letter of model name --- src/CrudMake.php | 22 +++++++++++--------- tests/CrudMakeCommandTest.php | 8 +++++--- tests/Generators/ControllerGeneratorTest.php | 2 +- tests/Generators/LangGeneratorTest.php | 4 ++-- tests/Generators/ViewsGeneratorTest.php | 30 ++++++++++++++-------------- 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/CrudMake.php b/src/CrudMake.php index e480a11..3931ee9 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -88,12 +88,16 @@ class CrudMake extends Command public function getModelName($modelName = null) { $modelName = is_null($modelName) ? $this->argument('name') : $modelName; + $model_name = ucfirst($modelName); + $plural_model_name = str_plural($model_name); return $this->modelNames = [ - 'plural_model_name' => str_plural($modelName), - 'model_name' => $modelName, - 'lowercase_plural_model_name' => strtolower(str_plural($modelName)), - 'lowercase_single_model_name' => strtolower($modelName), + 'plural_model_name' => $plural_model_name, + 'model_name' => $model_name, + 'table_name' => snake_case($plural_model_name), + 'lang_name' => snake_case($model_name), + 'collection_model_var_name' => camel_case($plural_model_name), + 'single_model_var_name' => camel_case($model_name), ]; } @@ -142,7 +146,7 @@ class CrudMake extends Command public function generateMigration() { $prefix = date('Y_m_d_His'); - $tableName = $this->modelNames['lowercase_plural_model_name']; + $tableName = $this->modelNames['table_name']; $migrationPath = $this->makeDirectory(database_path('migrations')); @@ -159,7 +163,7 @@ class CrudMake extends Command */ public function generateViews() { - $viewPath = $this->makeDirectory(resource_path('views/'.$this->modelNames['lowercase_plural_model_name'])); + $viewPath = $this->makeDirectory(resource_path('views/'.$this->modelNames['table_name'])); $this->generateFile($viewPath.'/index.blade.php', $this->getIndexViewContent()); $this->generateFile($viewPath.'/forms.blade.php', $this->getFormsViewContent()); @@ -176,9 +180,9 @@ class CrudMake extends Command { $langPath = $this->makeDirectory(resource_path('lang/en')); - $this->generateFile($langPath.'/'.$this->modelNames['lowercase_single_model_name'].'.php', $this->getLangFileContent()); + $this->generateFile($langPath.'/'.$this->modelNames['lang_name'].'.php', $this->getLangFileContent()); - $this->info($this->modelNames['lowercase_single_model_name'].' lang files generated.'); + $this->info($this->modelNames['lang_name'].' lang files generated.'); } /** @@ -195,7 +199,7 @@ class CrudMake extends Command $this->getModelFactoryContent() ); - $this->info($this->modelNames['lowercase_single_model_name'].' model factory generated.'); + $this->info($this->modelNames['lang_name'].' model factory generated.'); } /** diff --git a/tests/CrudMakeCommandTest.php b/tests/CrudMakeCommandTest.php index bcdc2f4..7ab6906 100644 --- a/tests/CrudMakeCommandTest.php +++ b/tests/CrudMakeCommandTest.php @@ -25,9 +25,11 @@ class CrudMakeCommandTest extends TestCase $this->assertEquals([ 'plural_model_name' => 'Categories', 'model_name' => 'Category', - 'lowercase_plural_model_name' => 'categories', - 'lowercase_single_model_name' => 'category', - ], $crudMaker->getModelName('Category')); + 'table_name' => 'categories', + 'lang_name' => 'category', + 'collection_model_var_name' => 'categories', + 'single_model_var_name' => 'category', + ], $crudMaker->getModelName('category')); } /** @test */ diff --git a/tests/Generators/ControllerGeneratorTest.php b/tests/Generators/ControllerGeneratorTest.php index 3cbc953..23c480a 100644 --- a/tests/Generators/ControllerGeneratorTest.php +++ b/tests/Generators/ControllerGeneratorTest.php @@ -29,7 +29,7 @@ class {$this->plural_model_name}Controller extends Controller public function index() { \$editable{$this->model_name} = null; - \${$this->table_name} = {$this->model_name}::where(function (\$query) { + \${$this->collection_model_var_name} = {$this->model_name}::where(function (\$query) { \$query->where('name', 'like', '%'.request('q').'%'); })->paginate(25); diff --git a/tests/Generators/LangGeneratorTest.php b/tests/Generators/LangGeneratorTest.php index d1771d6..5c6b4c9 100644 --- a/tests/Generators/LangGeneratorTest.php +++ b/tests/Generators/LangGeneratorTest.php @@ -11,13 +11,13 @@ class LangGeneratorTest extends TestCase { $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true]); - $langPath = resource_path('lang/en/'.$this->single_model_var_name.'.php'); + $langPath = resource_path('lang/en/'.$this->lang_name.'.php'); $this->assertFileExists($langPath); $langFileContent = "single_model_var_name}' => '{$this->model_name}', + '{$this->lang_name}' => '{$this->model_name}', 'list' => '{$this->model_name} List', 'search' => 'Search {$this->model_name}', 'not_found' => '{$this->model_name} not found.', diff --git a/tests/Generators/ViewsGeneratorTest.php b/tests/Generators/ViewsGeneratorTest.php index 587bab8..5e8623a 100644 --- a/tests/Generators/ViewsGeneratorTest.php +++ b/tests/Generators/ViewsGeneratorTest.php @@ -15,23 +15,23 @@ class ViewsGeneratorTest extends TestCase $this->assertFileExists($indexViewPath); $indexViewContent = "@extends('layouts.app') -@section('title', trans('{$this->single_model_var_name}.list')) +@section('title', trans('{$this->lang_name}.list')) @section('content')
- {{ link_to_route('{$this->table_name}.index', trans('{$this->single_model_var_name}.create'), ['action' => 'create'], ['class' => 'btn btn-success']) }} + {{ link_to_route('{$this->table_name}.index', trans('{$this->lang_name}.create'), ['action' => 'create'], ['class' => 'btn btn-success']) }}

- {{ trans('{$this->single_model_var_name}.list') }} - {{ trans('app.total') }} : {{ \${$this->table_name}->total() }} {{ trans('{$this->single_model_var_name}.{$this->single_model_var_name}') }} + {{ trans('{$this->lang_name}.list') }} + {{ trans('app.total') }} : {{ \${$this->collection_model_var_name}->total() }} {{ trans('{$this->lang_name}.{$this->single_model_var_name}') }}

{{ Form::open(['method' => 'get','class' => 'form-inline']) }} - {!! FormField::text('q', ['value' => request('q'), 'label' => trans('{$this->single_model_var_name}.search'), 'class' => 'input-sm']) !!} - {{ Form::submit(trans('{$this->single_model_var_name}.search'), ['class' => 'btn btn-sm']) }} + {!! FormField::text('q', ['value' => request('q'), 'label' => trans('{$this->lang_name}.search'), 'class' => 'input-sm']) !!} + {{ Form::submit(trans('{$this->lang_name}.search'), ['class' => 'btn btn-sm']) }} {{ link_to_route('{$this->table_name}.index', trans('app.reset')) }} {{ Form::close() }}
@@ -39,15 +39,15 @@ class ViewsGeneratorTest extends TestCase {{ trans('app.table_no') }} - {{ trans('{$this->single_model_var_name}.name') }} - {{ trans('{$this->single_model_var_name}.description') }} + {{ trans('{$this->lang_name}.name') }} + {{ trans('{$this->lang_name}.description') }} {{ trans('app.action') }} - @foreach(\${$this->table_name} as \$key => \${$this->single_model_var_name}) + @foreach(\${$this->collection_model_var_name} as \$key => \${$this->single_model_var_name}) - {{ \${$this->table_name}->firstItem() + \$key }} + {{ \${$this->collection_model_var_name}->firstItem() + \$key }} {{ \${$this->single_model_var_name}->name }} {{ \${$this->single_model_var_name}->description }} @@ -68,7 +68,7 @@ class ViewsGeneratorTest extends TestCase @endforeach -
{{ \${$this->table_name}->appends(Request::except('page'))->render() }}
+
{{ \${$this->collection_model_var_name}->appends(Request::except('page'))->render() }}
@@ -91,7 +91,7 @@ class ViewsGeneratorTest extends TestCase {!! Form::open(['route' => '{$this->table_name}.store']) !!} {!! FormField::text('name', ['required' => true]) !!} {!! FormField::textarea('description') !!} - {!! Form::submit(trans('{$this->single_model_var_name}.create'), ['class' => 'btn btn-success']) !!} + {!! Form::submit(trans('{$this->lang_name}.create'), ['class' => 'btn btn-success']) !!} {{ link_to_route('{$this->table_name}.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} {!! Form::close() !!} @endif @@ -105,15 +105,15 @@ class ViewsGeneratorTest extends TestCase @if (request('page')) {{ Form::hidden('page', request('page')) }} @endif - {!! Form::submit(trans('{$this->single_model_var_name}.update'), ['class' => 'btn btn-success']) !!} + {!! Form::submit(trans('{$this->lang_name}.update'), ['class' => 'btn btn-success']) !!} {{ link_to_route('{$this->table_name}.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} {!! Form::close() !!} @endif @if (Request::get('action') == 'delete' && \$editable{$this->model_name})
-

{{ trans('{$this->single_model_var_name}.delete') }}

+

{{ trans('{$this->lang_name}.delete') }}

- +

{{ \$editable{$this->model_name}->name }}

{!! \$errors->first('{$this->single_model_var_name}_id', ':message') !!}