feat(form-field): add form_field_options table, service, scope, cascade

Fourth and final WS-5 sibling. Polymorphic morph-owned table for the
RADIO / SELECT / MULTISELECT / CHECKBOX_LIST option rows, shared
between form_fields and form_field_library via the owner_type
discriminator. Matches the WS-5a (bindings) / WS-5b (validation_rules
+ configs) pattern one-for-one: dedicated service as single writer,
UNION-over-two-owner-chains scope, shared cascade observer.

Row shape:
  - value         canonical storage value (string ≤255, UNIQUE per owner)
  - label         default-locale display label (string ≤255)
  - sort_order    int unsigned
  - translations  JSON { "<locale>": "<translated label>" }

The UNIQUE(owner_type, owner_id, value) index ffo_owner_value_unique
is the seed-bug guard — duplicate values per field have no semantic
meaning and must fail at both the service layer (assertSpecsValid)
and the DB level.

Activity log: field.options_replaced emits on FormField subject only,
per the §6.7 WS-5a / §17.4.2 WS-5b convention that library-level
changes are silent in activity log.

No production reads yet. The form_fields.options and
form_field_library.options JSON columns remain the active source of
truth until the commit-3 reader switch — accessing $field->options
still resolves through the JSON cast in commit 1, so model tests
exercise the new morphMany via $field->options() (explicit relation
call). Both FormField and FormFieldLibrary now carry an `options`
morphMany alongside `bindings`, `validation_rules`, and `configs`.

Cascade: FormFieldChildTablesCascadeObserver gains form_field_options
as the fourth child cleaned on owner delete (both FormField soft/
force-delete and FormFieldLibrary delete).

Migration step-count tests in WS-5a/b/c bumped by 1 to account for
the new create_form_field_options_table on the migration stack.

Base scope-class extraction across the four siblings — deliberately
deferred to a follow-up work package per addendum §17.4.3 / §17.5.3.
Now that all four concrete implementations exist, the "what actually
varies" question can be answered empirically.

Tests: 1158 → 1182 green (+24 tests / +42 assertions).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-25 02:07:53 +02:00
parent e17fc7c2f4
commit 11588623c5
13 changed files with 1078 additions and 26 deletions

View File

@@ -117,6 +117,12 @@ final class FormField extends Model
return $this->morphMany(FormFieldConfig::class, 'owner');
}
public function options(): MorphMany
{
return $this->morphMany(FormFieldOption::class, 'owner')
->orderBy('sort_order');
}
public function conditionalLogicGroups(): HasMany
{
return $this->hasMany(FormFieldConditionalLogicGroup::class, 'form_field_id');

View File

@@ -77,4 +77,10 @@ final class FormFieldLibrary extends Model
{
return $this->morphMany(FormFieldConfig::class, 'owner');
}
public function options(): MorphMany
{
return $this->morphMany(FormFieldOption::class, 'owner')
->orderBy('sort_order');
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace App\Models\FormBuilder;
use App\Models\Scopes\FormFieldOptionScope;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
/**
* Relational home for RADIO / SELECT / MULTISELECT / CHECKBOX_LIST option
* rows. See ARCH-FORM-BUILDER §17.6 (WS-5d) and addendum Q3.
*
* Polymorphic owner (`form_field` / `form_field_library` aliases reused
* from WS-5a). One row per (owner, value); duplicates are blocked by the
* `ffo_owner_value_unique` index and at the service level by
* `FormFieldOptionService::assertSpecsValid`. Per-locale translated labels
* live on the row in the `translations` JSON bag.
*
* @property string $id
* @property string $owner_type
* @property string $owner_id
* @property string $value
* @property string $label
* @property int $sort_order
* @property array<string, string>|null $translations
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
*/
final class FormFieldOption extends Model
{
use HasFactory;
use HasUlids;
protected $table = 'form_field_options';
protected static function booted(): void
{
static::addGlobalScope(new FormFieldOptionScope());
}
protected $fillable = [
'owner_type',
'owner_id',
'value',
'label',
'sort_order',
'translations',
];
/** @var array<string, string> */
protected $casts = [
'translations' => 'array',
'sort_order' => 'int',
];
public function owner(): MorphTo
{
return $this->morphTo('owner', 'owner_type', 'owner_id');
}
/**
* Single source of truth for the per-row JSON shape consumed by
* snapshot writer, API resources, and activity-log diff
* reconstruction. Empty translations bag is omitted (preserves the
* current contract for monolingual schemas).
*
* @return array{value:string,label:string,sort_order:int,translations?:array<string,string>}
*/
public function toJsonShape(): array
{
$shape = [
'value' => $this->value,
'label' => $this->label,
'sort_order' => $this->sort_order,
];
if (is_array($this->translations) && $this->translations !== []) {
$shape['translations'] = $this->translations;
}
return $shape;
}
}

View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace App\Models\Scopes;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldLibrary;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
/**
* Fourth and final sibling in the form-field-child-table scope family,
* after `FormFieldBindingScope` (WS-5a),
* `FormFieldValidationRuleScope` and `FormFieldConfigScope` (WS-5b).
*
* Identical UNION-over-two-owner-chains shape:
*
* owner_id (
* SELECT id FROM form_fields
* WHERE form_schema_id (SELECT id FROM form_schemas WHERE organisation_id = ?)
* UNION
* SELECT id FROM form_field_library
* WHERE organisation_id = ?
* )
*
* Now that all four concrete implementations exist, base-class
* extraction across the family is the logical follow-up deferred to
* a separate work package per addendum §17.4.3 / §17.5.3 / §17.6.3.
*
* Escape hatch: `FormFieldOption::withoutGlobalScope(FormFieldOptionScope::class)`
* for cascade observers, backfill migrations, and platform super_admin
* paths.
*/
final class FormFieldOptionScope implements Scope
{
public function __construct(
private readonly ?string $organisationId = null,
) {}
public function apply(Builder $builder, Model $model): void
{
$orgId = $this->resolveOrganisationId();
if ($orgId === null) {
return;
}
$fieldIds = FormField::query()
->withoutGlobalScope(OrganisationScope::class)
->whereIn(
'form_schema_id',
FormSchema::query()
->withoutGlobalScope(OrganisationScope::class)
->where('organisation_id', $orgId)
->select('id'),
)
->select('id');
$libraryIds = FormFieldLibrary::query()
->withoutGlobalScope(OrganisationScope::class)
->where('organisation_id', $orgId)
->select('id');
$table = $model->getTable();
$builder->where(function (Builder $outer) use ($table, $fieldIds, $libraryIds): void {
$outer->where(function (Builder $q) use ($table, $fieldIds): void {
$q->where("$table.owner_type", 'form_field')
->whereIn("$table.owner_id", $fieldIds);
})->orWhere(function (Builder $q) use ($table, $libraryIds): void {
$q->where("$table.owner_type", 'form_field_library')
->whereIn("$table.owner_id", $libraryIds);
});
});
}
private function resolveOrganisationId(): ?string
{
if ($this->organisationId !== null) {
return $this->organisationId;
}
$route = request()->route();
if ($route === null) {
return null;
}
$org = $route->parameter('organisation');
if ($org instanceof \App\Models\Organisation) {
return $org->id;
}
if (is_string($org) && $org !== '') {
return $org;
}
$event = $route->parameter('event');
if ($event instanceof \App\Models\Event) {
return $event->organisation_id;
}
return null;
}
}