Skip to content

Commit

Permalink
Merge pull request #87 from anavel/fix/recovery-relation-in-minicrud
Browse files Browse the repository at this point in the history
Fix/recovery relation in minicrud
  • Loading branch information
edusalguero authored Dec 28, 2016
2 parents 2e09c65 + 1cd34f3 commit 5a40015
Show file tree
Hide file tree
Showing 20 changed files with 1 addition and 47 deletions.
1 change: 0 additions & 1 deletion src/Abstractor/Eloquent/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ protected function getFormElement()
$formElementType = 'select';
}


$formElement = $this->factory->get($formElementType, []);

if (!empty($this->config['attr']) && is_array($this->config['attr'])) {
Expand Down
5 changes: 0 additions & 5 deletions src/Abstractor/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public function getColumns($action, $withForeignKeys = false)
}
$tableColumns = $filteredColumns;


$foreignKeysName = [];
if ($withForeignKeys === false) {
$foreignKeys = $this->dbal->getTableForeignKeys();
Expand Down Expand Up @@ -238,7 +237,6 @@ public function getRelations()

$secondaryRelations = $relation->getSecondaryRelations();


if (!$secondaryRelations->isEmpty()) {
$relations->put(
$relationName,
Expand Down Expand Up @@ -426,7 +424,6 @@ public function persist(Request $request)
$item = $modelManager->getModelInstance($this->getModel());
}


$fields = $this->getEditFields(true);
if (empty($fields['main']) && $this->getRelations()->isEmpty()) {
return;
Expand Down Expand Up @@ -465,7 +462,6 @@ public function persist(Request $request)
}
}


if (!$field->saveIfEmpty() && empty($requestValue)) {
continue;
}
Expand All @@ -483,7 +479,6 @@ public function persist(Request $request)

$this->setInstance($item);


if (!empty($relations = $this->getRelations())) {
foreach ($relations as $relationKey => $relation) {
if ($relation instanceof Collection) {
Expand Down
1 change: 0 additions & 1 deletion src/Abstractor/Eloquent/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public function getByClassName($classname, array $config, $id = null)
$model = new Model(array_merge(['model' => $classname], $config), $this->modelManager->getAbstractionLayer($classname), $this->relationFactory, $this->fieldFactory, $this->generator);
$model->setSlug($this->slugger->slugify((new ReflectionClass($classname))->getShortName()));


if (is_null($id)) {
$model->setInstance($this->modelManager->getModelInstance($classname));
} else {
Expand Down
6 changes: 0 additions & 6 deletions src/Abstractor/Eloquent/Relation/MiniCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ public function getEditFields($arrayKey = null)

$fieldsBase = $this->getEditFieldsBase();


/** @var Collection $results */
$results = $this->getResults();


$results->put('emptyResult', '');
if (!empty($fieldsBase)) {
foreach ($results as $key => $result) {
Expand All @@ -81,7 +79,6 @@ public function getEditFields($arrayKey = null)
$tempFields[$columnName] = $field;
}


$relationModel = $this->eloquentRelation->getRelated()->newInstance();
if (!empty($result)) {
$relationModel = $result;
Expand Down Expand Up @@ -135,7 +132,6 @@ public function getEditFieldsBase()
->get();
$fields['__delete'] = $field;


foreach ($columns as $columnName => $column) {
if (in_array($columnName, $readOnly, true)) {
continue;
Expand Down Expand Up @@ -211,7 +207,6 @@ public function persist(array $relationArray = null, Request $request)
$this->modelAbstractor->setInstance($relationModel);
$secondaryRelations = $this->getSecondaryRelations();


$this->setKeys($relationModel);

$shouldBeSkipped = true;
Expand Down Expand Up @@ -243,7 +238,6 @@ public function persist(array $relationArray = null, Request $request)
}
}


foreach ($relation as $fieldKey => $fieldValue) {
if ($secondaryRelations->has($fieldKey)) {
$delayedRelations->put($fieldKey, $fieldValue);
Expand Down
2 changes: 0 additions & 2 deletions src/Abstractor/Eloquent/Relation/MiniCrudSingle.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function getEditFields($arrayKey = null)
$this->eloquentRelation->getParent()->getKeyName(),
];


$this->readConfig('edit');

if (!empty($columns)) {
Expand Down Expand Up @@ -98,7 +97,6 @@ public function getEditFields($arrayKey = null)

$fields = $this->addSecondaryRelationFields($fields);


return $fields;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Abstractor/Eloquent/Relation/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct(array $config, ModelManager $modelManager, Model $mo
}
}

$config = array_merge($this->config,$relatedmodelRelationsConfig);
$config = array_merge($this->config, $relatedmodelRelationsConfig);

$this->modelAbstractor = \App::make('Anavel\Crud\Contracts\Abstractor\ModelFactory')->getByClassName(get_class($this->eloquentRelation->getRelated()), $config);
}
Expand Down
1 change: 0 additions & 1 deletion src/Abstractor/Eloquent/Relation/SelectMultiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function persist(array $relationArray = null, Request $request)

$missing = $alreadyAssociated->diff($results);


$keyName = $this->eloquentRelation->getPlainForeignKey();
foreach ($results as $result) {
$result->$keyName = $this->relatedModel->getKey();
Expand Down
1 change: 0 additions & 1 deletion src/Abstractor/Eloquent/RelationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function get($name)
$relationInstance = $this->model->$name();
$relationEloquentType = get_class($relationInstance);


if (empty($this->config['type'])) {
if (!array_key_exists($relationEloquentType, $this->eloquentTypeToRelationType)) {
throw new FactoryException($relationEloquentType.' relation not supported');
Expand Down
4 changes: 0 additions & 4 deletions src/Abstractor/Eloquent/Traits/HandleFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ protected function handleField(Request $request, $item, array $fields, $groupNam
$skip = null;
$requestValue = null;


if (!empty($fields["{$fieldName}__delete"])) {
//We never want to save this field, it doesn't exist in the DB
$skip = "{$fieldName}__delete";


//If user wants to delete the existing file
if (!empty($request->input("{$groupName}.{$fieldName}__delete"))) {
$adapter = new Local($basePath);
Expand All @@ -45,7 +43,6 @@ protected function handleField(Request $request, $item, array $fields, $groupNam
$filesystem->delete($item->$fieldName);
}


$item->setAttribute(
$fieldName,
null
Expand All @@ -68,7 +65,6 @@ protected function handleField(Request $request, $item, array $fields, $groupNam
$fileName .= '.'.$extension;
}


$request->file($groupName.'.'.$fieldName)->move(
$modelPath,
$fileName
Expand Down
1 change: 0 additions & 1 deletion src/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function index(ModelAbstractorFactory $modelFactory)
throw new \Exception('No models configured.');
}


foreach ($models as $modelName => $model) {
$modelSlug = Slugger::slugify($modelName);
$modelAbstractor = $modelFactory->getByName($modelSlug);
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Form/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function getForm($action)
'class' => 'form-horizontal',
]);


$formFields = [];
foreach ($this->fields as $fieldGroupName => $fieldGroup) {
$tempFields = $this->addFormFields($fieldGroup, $fieldGroupName);
Expand All @@ -96,7 +95,6 @@ public function getForm($action)

$form->add($formFields);


return $form;
}

Expand Down
1 change: 0 additions & 1 deletion tests/Abstractor/Eloquent/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function test_returns_type()

$type = $this->sut->type();


$this->assertInstanceOf('Doctrine\DBAL\Types\Type', $type);
}

Expand Down
4 changes: 0 additions & 4 deletions tests/Abstractor/Eloquent/MiniCrudPolymorphicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,13 @@ public function test_get_edit_fields_returns_array()

$postMock->shouldReceive('getAttribute')->andReturn('idValue');


$fieldMock->shouldReceive('setOptions');

$fieldMock->shouldReceive('setValue')->times(1);

$this->modelAbstractorMock->shouldReceive('getRelations')->times(2)->andReturn([$this->secondaryRelationMock = $this->mock('Anavel\Crud\Abstractor\Eloquent\Relation\Select')]);
$this->secondaryRelationMock->shouldReceive('getEditFields')->andReturn([]);



$this->getClassMock->andReturn('Illuminate\Database\Eloquent\Relations\MorphMany');
$this->buildRelation();
$fields = $this->sut->getEditFields();
Expand Down Expand Up @@ -191,7 +188,6 @@ public function test_persist_with_old_results()

$this->modelAbstractorMock->shouldReceive('setInstance')->with($modelMock);


$modelMock->shouldReceive('getKey')->andReturn(1);
$modelMock->shouldReceive('setAttribute')->times(10);
$modelMock->shouldReceive('save')->times(2);
Expand Down
2 changes: 0 additions & 2 deletions tests/Abstractor/Eloquent/MiniCrudSingleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public function test_get_edit_fields_returns_array()
$postMock->shouldReceive('getAttributeValue')->andReturn('1');
$postMock->shouldReceive('getAttribute')->andReturn('chompy');


$this->fieldFactoryMock->shouldReceive('setColumn', 'setConfig')->andReturn($this->fieldFactoryMock);
$this->fieldFactoryMock->shouldReceive('get')->andReturn($fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\Field'));
$fieldMock->shouldReceive('setOptions');
Expand All @@ -103,7 +102,6 @@ public function test_get_edit_fields_returns_array()
$this->modelAbstractorMock->shouldReceive('getRelations')->times(1)->andReturn([$secondaryRelationMock = $this->mock('Anavel\Crud\Abstractor\Eloquent\Relation\Select')]);
$secondaryRelationMock->shouldReceive('getEditFields')->andReturn([]);


$this->getClassMock->andReturn('Illuminate\Database\Eloquent\Relations\MorphOne');
$this->buildRelation();
$fields = $this->sut->getEditFields();
Expand Down
5 changes: 0 additions & 5 deletions tests/Abstractor/Eloquent/MiniCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public function test_get_edit_fields_returns_array()
$this->fieldFactoryMock->shouldReceive('setColumn', 'setConfig')->andReturn($this->fieldFactoryMock);
$this->fieldFactoryMock->shouldReceive('get')->andReturn($fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\Field'));


$this->relationMock->shouldReceive('newInstance')->andReturn($postMock);

$this->modelAbstractorMock->shouldReceive('setInstance')->with($postMock);
Expand All @@ -98,7 +97,6 @@ public function test_get_edit_fields_returns_array()

$secondaryRelationMock->shouldReceive('getEditFields')->andReturn([]);


$postMock->shouldReceive('getAttribute')->andReturn('idValue');

$fieldMock->shouldReceive('setOptions');
Expand Down Expand Up @@ -154,7 +152,6 @@ public function test_persist_with_no_old_results()
$this->fieldFactoryMock->shouldReceive('get')->andReturn($fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\Field'));
////////


$fieldMock->shouldReceive('getName');
$fieldMock->shouldReceive('getFormField');
$modelMock->shouldReceive('getKey')->andReturn(1);
Expand Down Expand Up @@ -191,8 +188,6 @@ public function test_persist_with_old_results()
$this->relationMock->shouldReceive('getKeyName')->andReturn('id');
$this->modelAbstractorMock->shouldReceive('getRelations')->andReturn(collect());



$this->relationMock->shouldReceive('getResults')->andReturn($this->relationMock, collect([1 => $modelMock = $this->mock('Anavel\Crud\Tests\Models\Post')]));
// This, basically, re-tests getEditFields... It shouldn't be re-tested, but I can't figure out how to partially mock that method
$this->relationMock->shouldReceive('getRelated', 'getPlainForeignKey', 'getParent',
Expand Down
4 changes: 0 additions & 4 deletions tests/Abstractor/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function setUp()
$this->columnMock = $this->mock('Doctrine\DBAL\Schema\Column');
$this->generatorMock = $this->mock('Anavel\Crud\Contracts\Form\Generator');


$this->getClassMock = PHPMockery::mock('Anavel\Crud\Abstractor\Eloquent', 'get_class');

$this->sut = \Mockery::mock(Model::class, [$config['Users'], $this->dbalMock, $this->relationFactoryMock, $this->fieldFactoryMock, $this->generatorMock])->makePartial();
Expand Down Expand Up @@ -458,7 +457,6 @@ public function test_get_form_with_instance()

$this->sut->setInstance($modelMock);


$form = $this->sut->getForm('crudoado.model.store', 'users');

$this->assertInstanceOf('FormManager\ElementInterface', $form);
Expand Down Expand Up @@ -508,8 +506,6 @@ public function test_persist()

$this->getClassMock->andReturn('nomatch');



$result = $this->sut->persist($requestMock);

$this->assertInstanceOf('Anavel\Crud\Tests\Models\User', $result);
Expand Down
1 change: 0 additions & 1 deletion tests/Abstractor/Eloquent/SelectMultipleManyToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function setUp()
$config = require __DIR__.'/../../config.php';
$this->wrongConfig = require __DIR__.'/../../wrong-config.php';


$this->relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation');
$this->fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory');
$this->requestMock = $this->mock('Illuminate\Http\Request');
Expand Down
1 change: 0 additions & 1 deletion tests/Abstractor/Eloquent/SelectMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function setUp()
$config = require __DIR__.'/../../config.php';
$this->wrongConfig = require __DIR__.'/../../wrong-config.php';


$this->relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation');
$this->fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory');
$this->requestMock = $this->mock('Illuminate\Http\Request');
Expand Down
2 changes: 0 additions & 2 deletions tests/Abstractor/Eloquent/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function test_get_edit_fields_return_array_with_one_field()

$field->shouldReceive('setOptions');


$fields = $this->sut->getEditFields();

$this->assertInternalType('array', $fields, 'getEditFields should return an array');
Expand All @@ -84,7 +83,6 @@ public function test_get_edit_fields_return_array_with_one_field_with_key()

$field->shouldReceive('setOptions');


$fields = $this->sut->getEditFields('chompy');

$this->assertInternalType('array', $fields, 'getEditFields should return an array');
Expand Down
2 changes: 0 additions & 2 deletions tests/Abstractor/Eloquent/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public function test_persist_with_no_results()
$modelMock->shouldReceive('setAttribute')->times(3); // Should pass the language with only locale
$modelMock->shouldReceive('save')->once();


$this->sut->persist($inputArray, $this->requestMock);
}

Expand Down Expand Up @@ -151,7 +150,6 @@ public function test_persist_with_old_results()
$modelMock->shouldReceive('delete')->once(); // Should delete the already existing language, since it's empty
$modelMock->shouldReceive('save')->once();


$this->sut->persist($inputArray, $this->requestMock);
}
}

0 comments on commit 5a40015

Please sign in to comment.