Skip to content

Commit

Permalink
Improve the field name detection on the foreign models
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek committed Oct 22, 2017
1 parent b6b7609 commit a78b079
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 21 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServ

> A layout is required for the default views! The code generator allows you to create a layout using the command-line. Of cource you can use your own layout. You'll only need to include [CSS bootstrap framework](http://getbootstrap.com/ "CSS bootstrap framework") in your layout for the default templates to work properly. Additionally, you can chose to you design your own templetes using a different or no css framework.
## Lessons
Checkout our YouTube channel
> https://youtu.be/l21qNcsMAWg
> https://youtu.be/infoecfXOCw

## Available Commands

> The command in between the square brackets **[]** must be replaced with a variable of your choice.
Expand Down
10 changes: 10 additions & 0 deletions src/Models/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ public function getLabels()
return $this->labels;
}

/**
* Checks if the field is a header or not
*
* @return bool
*/
public function isHeader()
{
return $this->isHeader;
}

/**
* Checks if the field is nullable or not.
*
Expand Down
78 changes: 72 additions & 6 deletions src/Models/ForeignRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use CrestApps\CodeGenerator\Support\Config;
use CrestApps\CodeGenerator\Support\Contracts\JsonWriter;
use CrestApps\CodeGenerator\Support\Helpers;
use CrestApps\CodeGenerator\Support\ResourceMapper;
use CrestApps\CodeGenerator\Support\Str;
use DB;
use Exception;
use File;
use Illuminate\Database\Eloquent\Model;

class ForeignRelationship implements JsonWriter
Expand Down Expand Up @@ -179,13 +181,13 @@ protected function guessForeignField()

$primary = $this->getPrimaryKeyForForeignModel();
$idPatterns = Config::getKeyPatterns();

$columns = array_filter($columns, function ($column) use ($primary, $idPatterns) {

return $column != $primary && !Helpers::strIs($idPatterns, $column);
});

if (count($columns) == 1) {
return $columns[0];
if (count($columns) > 0) {
return current($columns);
}

return $primary;
Expand Down Expand Up @@ -278,7 +280,7 @@ public function getPrimaryKeyForForeignModel()
return $model->getKeyName();
}

return 'id';
return $this->getKeyNameFromResource() ?: 'id';
}

/**
Expand All @@ -289,15 +291,79 @@ public function getPrimaryKeyForForeignModel()
public function getModelColumns()
{
$model = $this->getForeignModelInstance();

$columns = [];
if ($this->isModel($model)) {
$tableName = $model->getTable();
return DB::getSchemaBuilder()->getColumnListing($tableName);
$columns = DB::getSchemaBuilder()->getColumnListing($tableName);
}

if (count($columns) == 0) {
$columns = $this->getFieldNamesFromResource();
}

return $columns;
}

/**
* Gets the foreign model columns from the resource file if one exists
*
* @return null | string
*/
protected function getKeyNameFromResource()
{
$resource = $this->getForeignResource();

if (!is_null($resource) && (($field = $resource->getPrimaryField()) != null)) {
return $field->name;
}

return null;
}

/**
* Gets the foreign model columns from the resource file if one exists
*
* @return array
*/
protected function getFieldNamesFromResource()
{
$resource = $this->getForeignResource();

if (!is_null($resource)) {
return $resource->pluckFields();
}

return [];
}

/**
* Gets the foreign model fields from resource file
*
* @return mix (null | CrestApps\CodeGenerator\Models\Resource)
*/
protected function getForeignResource()
{
$modelName = $this->getForeignModelName();
$resourceFile = ResourceMapper::pluckFirst($modelName) ?: Helpers::makeJsonFileName($modelName);
$resourceFileFullName = Config::getResourceFilePath($resourceFile);

if (File::exists($resourceFileFullName)) {
return Resource::fromFile($resourceFile, 'crestapps');
}

return null;
}

/**
* Gets the foreign model's class name
*
* @return string
*/
protected function getForeignModelName()
{
return class_basename($this->getFullForeignModel());
}

/**
* Gets a single instance of the foreign mode.
*
Expand Down
76 changes: 76 additions & 0 deletions src/Models/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,72 @@ public function hasFields()
return $this->totalFields() > 0;
}

/**
* Get the first header field if available
*
* @return min (null | CrestApps\CodeGenerator\Models\Field)
*/
public function getHeaderField()
{
return collect($this->fields)->first(function ($field) {
return $field->isHeader();
});
}

/**
* Get the first primary field if available
*
* @return min (null | CrestApps\CodeGenerator\Models\Field)
*/
public function getPrimaryField()
{
return collect($this->fields)->first(function ($field) {
return $field->isPrimary();
});
}

/**
* Extracts the giving property name from the fields
*
* @return array
*/
public function pluckFields($property = 'name')
{
$names = [];

if ($this->hasFields()) {
foreach ($this->getFields() as $field) {

if (property_exists($field, $property)) {
$names[] = $field->{$property};
}
}
}

return $names;
}

/**
* Extracts the giving property name from the fields
*
* @return array
*/
public function pluckKey($property = 'name')
{
$names = [];

if ($this->hasFields()) {
foreach ($this->getFields() as $field) {

if (property_exists($field, $property)) {
$names[] = $field->{$property};
}
}
}

return $names;
}

/**
* Checks if the resources has relations
*
Expand Down Expand Up @@ -132,6 +198,16 @@ public function toArray()
];
}

/**
* Get the fields
*
* @return array
*/
public function getFields()
{
return $this->fields ?: [];
}

/**
* Converts the fields into a json-ready array
*
Expand Down
2 changes: 2 additions & 0 deletions src/Support/FieldTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ protected function getLabels($title, $name)
$title = $this->getFirstElement($title);
}

$name = Helpers::removePostFixWith($name, '_id');

$this->replaceModelName($title, $name, 'field_');

if ($this->hasLanguages()) {
Expand Down
7 changes: 1 addition & 6 deletions src/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,8 @@ public static function prettifyJson(array $object)
public static function convertNameToLabel($name)
{
$title = ucwords(str_replace('_', ' ', $name));
$idString = ' Id';

if (ends_with($title, $idString)) {
return rtrim($title, $idString);
}

return $title;
return self::removePostFixWith($title, ' Id');
}

/**
Expand Down
Loading

0 comments on commit a78b079

Please sign in to comment.