Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jun 28, 2024
1 parent a8366a3 commit 26dfc86
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Fields/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ public function modelUrl(Model $model): string
public function resolveFormat(Request $request, Model $model): ?string
{
if (is_null($this->formatResolver)) {
$this->formatResolver = function (Request $request, Model $model, array $value): string {
$this->formatResolver = function (Request $request, Model $model, ?array $value = null): string {
$values = array_map(function (array $value) use ($request, $model): array {
return $this->resolveOptionFields($request, $model, $this->newTemporaryModel($value))
->authorized($request, $model)
->visible('show')
->mapToDisplay($request, $model);
}, $value);
}, (array) $value);

return View::make('root::fields.repeater-table', ['values' => $values])->render();
};
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/MorphToControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function setUp(): void
$this->admin = User::factory()->create();
}

public function test_a_relation_controller_handles_request(): void
public function test_a_morph_to_controller_handles_request(): void
{
$this->actingAs($this->admin)
->get('/root/users/'.$this->admin->getKey().'/fields/employer')
Expand Down
37 changes: 37 additions & 0 deletions tests/Http/RepeaterControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Cone\Root\Tests\Http;

use Cone\Root\Fields\Repeater;
use Cone\Root\Root;
use Cone\Root\Tests\TestCase;
use Cone\Root\Tests\User;

class RepeaterControllerTest extends TestCase
{
protected Repeater $field;

protected User $admin;

public function setUp(): void
{
parent::setUp();

$this->field = Root::instance()
->resources
->resolve('users')
->resolveFields($this->app['request'])
->first(function ($field) {
return $field->getModelAttribute() === 'settings';
});

$this->admin = User::factory()->create();
}

public function test_a_repeater_controller_handles_request(): void
{
$this->actingAs($this->admin)
->post('/root/users/'.$this->admin->getKey().'/fields/settings')
->assertOk();
}
}
9 changes: 8 additions & 1 deletion tests/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cone\Root\Fields\ID;
use Cone\Root\Fields\Media;
use Cone\Root\Fields\MorphTo;
use Cone\Root\Fields\Repeater;
use Cone\Root\Fields\Select;
use Cone\Root\Fields\Text;
use Cone\Root\Resources\Resource;
Expand Down Expand Up @@ -61,14 +62,20 @@ public function fields(Request $request): array
]),
];
}),

MorphTo::make('Employer')
->display('name')
->nullable()
->types([
User::class,
Team::class,
]),
Repeater::make('Settings')
->withFields(function () {
return [
Text::make('Key'),
Text::make('Value'),
];
}),
];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function definition(): array
'employer_id' => null,
'employer_type' => null,
'deleted_at' => null,
'settings' => null,
]);
}
};
Expand All @@ -41,10 +42,9 @@ public function definition(): array
protected function casts(): array
{
return [
'created_at' => 'datetime',
'email_verified_at' => 'datetime',
'password' => 'hashed',
'updated_at' => 'datetime',
'settings' => 'json',
];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/migrations/2024_06_14_213755_alter_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public function up(): void
$table->after('id', function (Blueprint $table) {
$table->nullableMorphs('employer');
});
$table->after('password', function (Blueprint $table) {
$table->json('settings')->nullable();
});
$table->after('updated_at', function (Blueprint $table) {
$table->softDeletes();
});
Expand All @@ -28,6 +31,7 @@ public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropMorphs('employer');
$table->dropColumn('settings');
$table->dropSoftDeletes();
});
}
Expand Down

0 comments on commit 26dfc86

Please sign in to comment.