diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index 136b1864..70916e6d 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -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; } }); } diff --git a/tests/Feature/Form/FormUpdateActionTest.php b/tests/Feature/Form/FormUpdateActionTest.php index 3d27ab0e..8dadc212 100644 --- a/tests/Feature/Form/FormUpdateActionTest.php +++ b/tests/Feature/Form/FormUpdateActionTest.php @@ -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']); + } }