Skip to content

Commit ecfdb49

Browse files
author
Ni Nelli
committed
fix: correct resource fields to match command options
refs: #202
1 parent 2689dfa commit ecfdb49

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/Generators/ResourceGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace RonasIT\Support\Generators;
44

5+
use Illuminate\Support\Arr;
56
use RonasIT\Support\Events\SuccessCreateMessage;
67
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
78

@@ -53,7 +54,7 @@ public function generateResource(): void
5354
'entity' => $this->model,
5455
'namespace' => $this->getNamespace('resources'),
5556
'model_namespace' => $this->getNamespace('models', $this->modelSubFolder),
56-
'fields' => $this->fields,
57+
'fields' => when($this->fields, fn () => Arr::flatten($this->fields)),
5758
]);
5859

5960
$this->saveClass('resources', "{$this->model}Resource", $resourceContent, $this->model);

tests/ResourceGeneratorTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@ public function testCreateResourcesWithFields()
7474
app(ResourceGenerator::class)
7575
->setModel('Post')
7676
->setFields([
77-
'id',
78-
'title',
79-
'description',
80-
'owner_id',
77+
'integer' => ['priority'],
78+
'integer-required' => ['media_id'],
79+
'float' => ['seo_score'],
80+
'float-required' => ['rating'],
81+
'string' => ['description'],
82+
'string-required' => ['title'],
83+
'boolean' => ['is_reviewed'],
84+
'boolean-required' => ['is_published'],
85+
'timestamp' => ['reviewed_at', 'created_at', 'updated_at'],
86+
'timestamp-required' => ['published_at'],
87+
'json' => ['meta'],
8188
])
8289
->generate();
8390

tests/fixtures/ResourceGeneratorTest/post_resource_with_fields.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,19 @@ class PostResource extends BaseResource
1313
public function toArray($request): array
1414
{
1515
return [
16-
'id' => $this->resource->id,
17-
'title' => $this->resource->title,
16+
'priority' => $this->resource->priority,
17+
'media_id' => $this->resource->media_id,
18+
'seo_score' => $this->resource->seo_score,
19+
'rating' => $this->resource->rating,
1820
'description' => $this->resource->description,
19-
'owner_id' => $this->resource->owner_id,
21+
'title' => $this->resource->title,
22+
'is_reviewed' => $this->resource->is_reviewed,
23+
'is_published' => $this->resource->is_published,
24+
'reviewed_at' => $this->resource->reviewed_at,
25+
'created_at' => $this->resource->created_at,
26+
'updated_at' => $this->resource->updated_at,
27+
'published_at' => $this->resource->published_at,
28+
'meta' => $this->resource->meta,
2029
];
2130
}
2231
}

0 commit comments

Comments
 (0)