Skip to content

Commit d8de8d3

Browse files
committed
=Fix issues with soft delete. Also, adds capability to flag a field as is-date to
1 parent c1a6ea8 commit d8de8d3

File tree

10 files changed

+161
-40
lines changed

10 files changed

+161
-40
lines changed

src/Commands/CreateModelCommand.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,19 @@ protected function buildClass($name)
5858
$input = $this->getCommandInput();
5959
$fields = $this->getFields($input->fields, 'model', $input->fieldsFile);
6060

61+
if($input->useSoftDelete)
62+
{
63+
$fields = $this->upsertDeletedAt($fields);
64+
}
65+
6166
$primaryKey = $this->getNewPrimaryKey($this->getPrimaryKeyName($fields, $input->primaryKey));
6267

6368
return $this->replaceNamespace($stub, $name)
6469
->replaceTable($stub, $input->table)
6570
->replaceSoftDelete($stub, $input->useSoftDelete)
6671
->replaceTimestamps($stub, $input->useTimeStamps)
6772
->replaceFillable($stub, $this->getFillables($input->fillable, $fields))
73+
->replaceDateFields($stub, $this->getDateFields($fields))
6874
->replacePrimaryKey($stub, $primaryKey)
6975
->replaceRelationshipPlaceholder($stub, $this->createRelationMethods($input->relationships))
7076
->replaceAccessors($stub, $this->getAccessors($fields))
@@ -87,6 +93,39 @@ protected function getPrimaryKeyName(array $fields, $primaryKey)
8793
return !is_null($primaryField) ? $primaryField->name : $primaryKey;
8894
}
8995

96+
/**
97+
* If a giving fields collection does not contain a field called "deleted_at", one will be created.
98+
*
99+
* @param array $fields
100+
*
101+
* @return array
102+
*/
103+
protected function upsertDeletedAt(array $fields)
104+
{
105+
foreach($fields as $field)
106+
{
107+
if($field->name == 'deleted_at')
108+
{
109+
return $fields;
110+
}
111+
}
112+
113+
$fields[] = $this->getNewDeletedAtField();
114+
return $fields;
115+
}
116+
117+
/**
118+
* Gets a new field called "deleted_at"
119+
*
120+
* @return string
121+
*/
122+
protected function getNewDeletedAtField()
123+
{
124+
$field = new Field('deleted_at');
125+
$field->isDate = true;
126+
127+
return $field;
128+
}
90129
/**
91130
* Gets the stub file.
92131
*
@@ -149,6 +188,26 @@ protected function getFillablefields(array $fields)
149188
return sprintf('[%s]', implode(',', $fillables));
150189
}
151190

191+
/**
192+
* Gets the date fields string from a giving fields array.
193+
*
194+
* @return string
195+
*/
196+
protected function getDateFields(array $fields)
197+
{
198+
$dates = [];
199+
200+
foreach($fields as $field)
201+
{
202+
if($field->isDate)
203+
{
204+
$dates[] = sprintf("'%s'", Helpers::removeNonEnglishChars($field->name));
205+
}
206+
}
207+
208+
return sprintf('[%s]', implode(',', $dates));
209+
}
210+
152211
/**
153212
* Gets a clean user inputs.
154213
*
@@ -345,6 +404,21 @@ protected function replaceAccessors(&$stub, $accessors)
345404
return $this;
346405
}
347406

407+
/**
408+
* Replaces the dates for the given stub.
409+
*
410+
* @param string $stub
411+
* @param string $dates
412+
*
413+
* @return $this
414+
*/
415+
protected function replaceDateFields(&$stub, $dates)
416+
{
417+
$stub = str_replace('{{dates}}', $dates, $stub);
418+
419+
return $this;
420+
}
421+
348422
/**
349423
* Replaces the delimiter for the given stub.
350424
*

src/Models/Field.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ class Field
182182
*/
183183
public $range = [];
184184

185+
/**
186+
* Checks if the field should be mutated to date
187+
*
188+
* @var bool
189+
*/
190+
public $isDate = false;
191+
185192
/**
186193
* Creates a new field instance.
187194
*

src/Support/FieldTransformer.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class FieldTransformer {
5858
'placeholder' => 'placeHolder',
5959
'place-holder' => 'placeHolder',
6060
'delimiter' => 'optionsDelimiter',
61-
'is-header' => 'isHeader'
61+
'is-header' => 'isHeader',
62+
'is-date' => 'isDate'
6263
];
6364

6465

@@ -171,40 +172,42 @@ protected function transfer()
171172
*
172173
* @return array
173174
*/
174-
protected function transferField(array $field)
175+
protected function transferField(array $properties)
175176
{
176-
if(!$this->isKeyExists($field, 'name') || empty(Helpers::removeNonEnglishChars($field['name']) ) )
177+
if(!$this->isKeyExists($properties, 'name') || empty(Helpers::removeNonEnglishChars($properties['name']) ) )
177178
{
178179
throw new Exception("The field 'name' was not provided!");
179180
}
180181

181-
if(!$this->isValidHtmlType($field))
182+
if(!$this->isValidHtmlType($properties))
182183
{
183-
unset($field['html-type']);
184+
unset($properties['html-type']);
184185
}
185186

186-
$newField = new Field(Helpers::removeNonEnglishChars($field['name']));
187+
$field = new Field(Helpers::removeNonEnglishChars($properties['name']));
187188

188-
$this->setPredefindProperties($newField, $field)
189-
->setDataType($newField, $field)
190-
->setOptionsProperty($newField, $field)
191-
->setValidationProperty($newField, $field)
192-
->setLabelsProperty($newField, $field)
193-
->setDataTypeParams($newField, $field)
194-
->setMultipleAnswers($newField, $field)
195-
->setRange($newField, $field);
189+
$field->isDate = in_array($field->name, ['created_at','updated_at','deleted_at']);
196190

197-
if($this->isValidSelectRangeType($field))
191+
$this->setPredefindProperties($field, $properties)
192+
->setDataType($field, $properties)
193+
->setOptionsProperty($field, $properties)
194+
->setValidationProperty($field, $properties)
195+
->setLabelsProperty($field, $properties)
196+
->setDataTypeParams($field, $properties)
197+
->setMultipleAnswers($field, $properties)
198+
->setRange($field, $properties);
199+
200+
if($this->isValidSelectRangeType($properties))
198201
{
199-
$newField->htmlType = 'selectRange';
202+
$field->htmlType = 'selectRange';
200203
}
201204

202-
if($newField->dataType == 'enum' && empty($newField->getOptions()) )
205+
if($field->dataType == 'enum' && empty($field->getOptions()) )
203206
{
204207
throw new Exception('To construct an enum data-type field, options must be set');
205208
}
206209

207-
return new FieldMapper($newField, $field);
210+
return new FieldMapper($field, $properties);
208211
}
209212

210213
/**
@@ -238,31 +241,31 @@ protected function isValidSelectRangeType(array $field)
238241
/**
239242
* Checks if a key exists in a giving array
240243
*
241-
* @param array $field
244+
* @param array $properties
242245
* @param string $name
243246
*
244247
* @return bool
245248
*/
246-
protected function isKeyExists(array $field, $name)
249+
protected function isKeyExists(array $properties, $name)
247250
{
248-
return array_key_exists($name, $field);
251+
return array_key_exists($name, $properties);
249252
}
250253

251254
/**
252255
* Sets the dataType for a giving field
253256
*
254-
* @param CrestApps\CodeGenerator\Models\Field $newField
255-
* @param array $field
257+
* @param CrestApps\CodeGenerator\Models\Field $field
258+
* @param array $properties
256259
*
257260
* @return $this
258261
*/
259-
protected function setDataType(Field & $newField, array $field)
262+
protected function setDataType(Field & $field, array $properties)
260263
{
261264
$map = $this->dataTypeMap();
262265

263-
if($this->isKeyExists($field, 'data-type') && $this->isKeyExists($map, 'data-type') )
266+
if($this->isKeyExists($properties, 'data-type') && $this->isKeyExists($map, 'data-type') )
264267
{
265-
$newField->dataType = $map[$field['data-type']];
268+
$field->dataType = $map[$properties['data-type']];
266269
}
267270

268271
return $this;
@@ -290,15 +293,15 @@ protected function setRange(Field & $newField, array $field)
290293
* Sets the DataTypeParam for a giving field
291294
*
292295
* @param CrestApps\CodeGenerator\Models\Field $newField
293-
* @param array $field
296+
* @param array $properties
294297
*
295298
* @return $this
296299
*/
297-
protected function setDataTypeParams(Field & $newField, array $field)
300+
protected function setDataTypeParams(Field & $field, array $properties)
298301
{
299-
if($this->isKeyExists($field, 'data-type-params') && is_array($field['data-type-params']))
302+
if($this->isKeyExists($properties, 'data-type-params') && is_array($properties['data-type-params']))
300303
{
301-
$newField->methodParams = $field['data-type-params'];
304+
$field->methodParams = $properties['data-type-params'];
302305
}
303306

304307
return $this;
@@ -311,12 +314,11 @@ protected function setDataTypeParams(Field & $newField, array $field)
311314
*
312315
* @return $this
313316
*/
314-
protected function setMultipleAnswers(Field & $newField, array $field)
317+
protected function setMultipleAnswers(Field & $field)
315318
{
316-
317-
if(in_array($newField->htmlType, ['checkbox','multipleSelect']))
319+
if(in_array($field->htmlType, ['checkbox','multipleSelect']))
318320
{
319-
$newField->isMultipleAnswers = true;
321+
$field->isMultipleAnswers = true;
320322
}
321323

322324
return $this;

src/Traits/CommonCommand.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ public function arguments()
8181
return parent::argument();
8282
}
8383

84+
/**
85+
* Reduceses multiple new line into one.
86+
*
87+
* @param string $stub
88+
*
89+
* @return $this
90+
*/
91+
protected function reduceNewLines(&$stub)
92+
{
93+
94+
while(strpos($stub, "\r\n\r\n") !== false)
95+
{
96+
$stub = str_replace("\r\n\r\n", "\r\n", $stub);
97+
}
98+
99+
return $this;
100+
}
101+
84102
/**
85103
* Gets all command's options depending on the current framework version.
86104
*

templates/default-collective/controller-with-form-request.stub

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ class DummyClass extends Controller
110110
*/
111111
public function destroy($id)
112112
{
113-
{{modelNameClass}}::destroy($id);
113+
${{modelName}} = {{modelNameClass}}::findOrFail($id);
114+
115+
${{modelName}}->delete();
114116

115-
Session::flash('flash_message', '{{modelNameClass}} was deleted!');
117+
Session::flash('success_message', '{{modelNameClass}} was deleted!');
116118

117119
return redirect()->route('{{indexRouteName}}');
118120
}

templates/default-collective/controller.stub

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ class DummyClass extends Controller
110110
*/
111111
public function destroy($id)
112112
{
113-
{{modelNameClass}}::destroy($id);
113+
${{modelName}} = {{modelNameClass}}::findOrFail($id);
114+
115+
${{modelName}}->delete();
114116

115117
Session::flash('success_message', '{{modelNameClass}} was deleted!');
116118

templates/default-collective/model.stub

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class DummyClass extends Model
2424
*/
2525
protected $fillable = {{fillable}};
2626

27+
/**
28+
* The attributes that should be mutated to dates.
29+
*
30+
* @var array
31+
*/
32+
protected $dates = {{dates}};
2733
{{relationships}}
2834
{{mutators}}
2935
{{accessors}}

templates/default/controller-with-form-request.stub

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ class DummyClass extends Controller
110110
*/
111111
public function destroy($id)
112112
{
113-
{{modelNameClass}}::destroy($id);
113+
${{modelName}} = {{modelNameClass}}::findOrFail($id);
114+
115+
${{modelName}}->delete();
114116

115-
Session::flash('flash_message', '{{modelNameClass}} was deleted!');
117+
Session::flash('success_message', '{{modelNameClass}} was deleted!');
116118

117119
return redirect()->route('{{indexRouteName}}');
118120
}

templates/default/controller.stub

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ class DummyClass extends Controller
110110
*/
111111
public function destroy($id)
112112
{
113-
{{modelNameClass}}::destroy($id);
113+
${{modelName}} = {{modelNameClass}}::findOrFail($id);
114+
115+
${{modelName}}->delete();
114116

115117
Session::flash('success_message', '{{modelNameClass}} was deleted!');
116118

templates/default/model.stub

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class DummyClass extends Model
2424
*/
2525
protected $fillable = {{fillable}};
2626

27+
/**
28+
* The attributes that should be mutated to dates.
29+
*
30+
* @var array
31+
*/
32+
protected $dates = {{dates}};
2733
{{relationships}}
2834
{{mutators}}
2935
{{accessors}}

0 commit comments

Comments
 (0)