Fix: Update active columns

This commit is contained in:
philipp lang 2024-04-23 23:12:14 +02:00
parent 4997895dfb
commit 1328f359b2
2 changed files with 21 additions and 1 deletions

View File

@ -139,11 +139,12 @@ class Form extends Model implements HasMedia
public static function booted(): void
{
static::saving(function (self $model) {
if (is_null($model->meta)) {
if (is_null(data_get($model->meta, 'active_columns'))) {
$model->setAttribute('meta', [
'active_columns' => $model->getFields()->count() ? $model->getFields()->take(4)->pluck('key')->toArray() : null,
'sorting' => $model->getFields()->count() ? [$model->getFields()->first()->key, 'asc'] : null,
]);
return;
}
if (is_array(data_get($model->meta, 'active_columns'))) {
@ -151,6 +152,7 @@ class Form extends Model implements HasMedia
...$model->meta,
'active_columns' => array_values(array_intersect([...$model->getFields()->pluck('key')->toArray(), 'created_at'], $model->meta['active_columns'])),
]);
return;
}
});
}

View File

@ -47,4 +47,22 @@ class FormUpdateActionTest extends FormTestCase
$this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk();
$this->assertEquals(['firstname'], $form->fresh()->meta['active_columns']);
}
public function testItUpdatesActiveColumnsWhenFieldsAdded(): void
{
$this->login()->loginNami()->withoutExceptionHandling();
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([])])
->create();
$payload = FormRequest::new()->sections([
FormtemplateSectionRequest::new()->fields([
$this->textField('firstname'),
$this->textField('geb'),
$this->textField('lastname'),
])
])->create();
$this->patchJson(route('form.update', ['form' => $form]), $payload)->assertSessionDoesntHaveErrors()->assertOk();
$this->assertEquals(['firstname', 'geb', 'lastname'], $form->fresh()->meta['active_columns']);
}
}