Skip to content

Commit

Permalink
Merge pull request #164 from boboldehampsink/record_index_improvements
Browse files Browse the repository at this point in the history
Added more flexibility for getting a record's index
  • Loading branch information
bvangennep authored Nov 19, 2018
2 parents ff08c27 + 4b2ffc8 commit 899ab0e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 4.1.0 - 2018-11-19
### Added
- Added more flexibility for getting a record's index

### 4.0.18 - 2018-11-19
### Added
- Delete empty field and site groups on import with force
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "4.0.18",
"version": "4.1.0",
"name": "nerds-and-company/schematic",
"description": "Craft setup and sync tool",
"type": "craft-plugin",
Expand Down
4 changes: 2 additions & 2 deletions src/Converters/Models/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ abstract public function deleteRecord(Model $record): bool;
/**
* {@inheritdoc}
*/
public function getRecordIndex(): string
public function getRecordIndex(Model $record): string
{
return 'handle';
return $record->handle;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Interfaces/ConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public function deleteRecord(Model $record): bool;
/**
* Gets the record's key to index by.
*
* @param Model $record
*
* @return string
*/
public function getRecordIndex(): string;
public function getRecordIndex(Model $record): string;

/**
* Get single record definition.
Expand Down
8 changes: 4 additions & 4 deletions src/Mappers/ModelMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function export(array $records): array
$modelClass = get_class($record);
$converter = Craft::$app->controller->module->getConverter($modelClass);
if ($converter) {
$index = $converter->getRecordIndex();
$result[$record->$index] = $converter->getRecordDefinition($record);
$index = $converter->getRecordIndex($record);
$result[$index] = $converter->getRecordDefinition($record);
}
}

Expand Down Expand Up @@ -102,8 +102,8 @@ private function getRecordsByHandle(array $records): array
foreach ($records as $record) {
$modelClass = get_class($record);
$converter = Craft::$app->controller->module->getConverter($modelClass);
$index = $converter->getRecordIndex();
$recordsByHandle[$record->$index] = $record;
$index = $converter->getRecordIndex($record);
$recordsByHandle[$index] = $record;
}

return $recordsByHandle;
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/Mappers/ModelMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ private function getMockConverter(): Converter

$mockConverter->expects($this->any())
->method('getRecordIndex')
->willReturn('handle');
->willReturnCallback(function ($model) {
return $model->handle;
});

$mockConverter->expects($this->any())
->method('getRecordDefinition')
Expand Down

0 comments on commit 899ab0e

Please sign in to comment.