Browse Source

Always uppercase first letter of model name

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
f686ac7006
  1. 22
      src/CrudMake.php
  2. 8
      tests/CrudMakeCommandTest.php
  3. 2
      tests/Generators/ControllerGeneratorTest.php
  4. 4
      tests/Generators/LangGeneratorTest.php
  5. 30
      tests/Generators/ViewsGeneratorTest.php

22
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.');
}
/**

8
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 */

2
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);

4
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 = "<?php
return [
// Labels
'{$this->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.',

30
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')
<div class=\"pull-right\">
{{ 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']) }}
</div>
<h3 class=\"page-header\">
{{ trans('{$this->single_model_var_name}.list') }}
<small>{{ trans('app.total') }} : {{ \${$this->table_name}->total() }} {{ trans('{$this->single_model_var_name}.{$this->single_model_var_name}') }}</small>
{{ trans('{$this->lang_name}.list') }}
<small>{{ trans('app.total') }} : {{ \${$this->collection_model_var_name}->total() }} {{ trans('{$this->lang_name}.{$this->single_model_var_name}') }}</small>
</h3>
<div class=\"row\">
<div class=\"col-md-8\">
<div class=\"panel panel-default table-responsive\">
<div class=\"panel-heading\">
{{ 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() }}
</div>
@ -39,15 +39,15 @@ class ViewsGeneratorTest extends TestCase
<thead>
<tr>
<th class=\"text-center\">{{ trans('app.table_no') }}</th>
<th>{{ trans('{$this->single_model_var_name}.name') }}</th>
<th>{{ trans('{$this->single_model_var_name}.description') }}</th>
<th>{{ trans('{$this->lang_name}.name') }}</th>
<th>{{ trans('{$this->lang_name}.description') }}</th>
<th class=\"text-center\">{{ trans('app.action') }}</th>
</tr>
</thead>
<tbody>
@foreach(\${$this->table_name} as \$key => \${$this->single_model_var_name})
@foreach(\${$this->collection_model_var_name} as \$key => \${$this->single_model_var_name})
<tr>
<td class=\"text-center\">{{ \${$this->table_name}->firstItem() + \$key }}</td>
<td class=\"text-center\">{{ \${$this->collection_model_var_name}->firstItem() + \$key }}</td>
<td>{{ \${$this->single_model_var_name}->name }}</td>
<td>{{ \${$this->single_model_var_name}->description }}</td>
<td class=\"text-center\">
@ -68,7 +68,7 @@ class ViewsGeneratorTest extends TestCase
@endforeach
</tbody>
</table>
<div class=\"panel-body\">{{ \${$this->table_name}->appends(Request::except('page'))->render() }}</div>
<div class=\"panel-body\">{{ \${$this->collection_model_var_name}->appends(Request::except('page'))->render() }}</div>
</div>
</div>
<div class=\"col-md-4\">
@ -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})
<div class=\"panel panel-default\">
<div class=\"panel-heading\"><h3 class=\"panel-title\">{{ trans('{$this->single_model_var_name}.delete') }}</h3></div>
<div class=\"panel-heading\"><h3 class=\"panel-title\">{{ trans('{$this->lang_name}.delete') }}</h3></div>
<div class=\"panel-body\">
<label class=\"control-label\">{{ trans('{$this->single_model_var_name}.name') }}</label>
<label class=\"control-label\">{{ trans('{$this->lang_name}.name') }}</label>
<p>{{ \$editable{$this->model_name}->name }}</p>
{!! \$errors->first('{$this->single_model_var_name}_id', '<span class=\"form-error small\">:message</span>') !!}
</div>

Loading…
Cancel
Save