Class: LcpRuby::Pages::FilterForm

Inherits:
VirtualFields::VirtualForm show all
Defined in:
lib/lcp_ruby/pages/filter_form.rb

Overview

First consumer of ‘LcpRuby::VirtualFields::VirtualForm`. Builds a per-request synthetic form-like object from a page’s ‘filter_form_definition` (already shape-validated by PR 1 `FilterFormValidator`).

Wires together:

* `VirtualField` for each filter_form: entry, with `am_type`
  chosen by input_type × multi: (Type::ArrayOf for multi).
* Param whitelist + coercion from `VirtualForm`.
* `field_config_for(name)` enrichment mirroring
  Presenter::LayoutBuilder so form_helper renders each input
  the same way as on a normal form.
* `auto_submit_effective?` derivation per spec § 4 UX truth
  table (auto only if no multi + no cascade).

Spec: docs/design/page_filters_as_virtual_forms.md § 5 step 10 (Pages::FilterForm), § 5 step 11 (field_config enrichment), § 4 (auto_submit derivation).

Constant Summary collapse

INPUT_TYPE_TO_AM_TYPE =

Default AM::Attributes type per input_type. Conservative mapping — anything not listed falls back to ‘:string` (the AM default), which matches how URL params arrive anyway.

{
  "number"      => :integer,
  "boolean"     => :boolean,
  "checkbox"    => :boolean,
  "toggle"      => :boolean,
  "date"        => :date,
  "date_picker" => :date,
  "datetime"    => :datetime
}.freeze

Instance Attribute Summary collapse

Attributes inherited from VirtualFields::VirtualForm

#context, #fields, #name, #target_model

Instance Method Summary collapse

Methods inherited from VirtualFields::VirtualForm

#any?, #attributes, #each_field, #field, #model_name, #persisted?, #to_model

Constructor Details

#initialize(page:, params:, current_user: nil) ⇒ FilterForm

Returns a new instance of FilterForm.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lcp_ruby/pages/filter_form.rb', line 40

def initialize(page:, params:, current_user: nil)
  @page = page
  @scope_params = (params[:page_filter] || params["page_filter"] || {}).then do |raw|
    raw.respond_to?(:to_unsafe_h) ? raw.to_unsafe_h : raw
  end

  super(
    name: "page_filter",
    fields: build_fields(page),
    target_model: page.model,
    context: { params: @scope_params, current_user: current_user, page: page }
  )
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



38
39
40
# File 'lib/lcp_ruby/pages/filter_form.rb', line 38

def page
  @page
end

Instance Method Details

#auto_submit_effective?Boolean

Spec § 4 UX: auto_submit derivation.

* explicit `auto_submit: true`  → true
* explicit `auto_submit: false` → false
* `auto_submit: derive` (default) → true iff no multi: and
  no depends_on: on any filter_form field.

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lcp_ruby/pages/filter_form.rb', line 59

def auto_submit_effective?
  case page.auto_submit
  when true  then true
  when false then false
  else
    page.filter_form_definition.none? do |f|
      io = f["input_options"] || {}
      io["multi"] || io["depends_on"]
    end
  end
end

#field_config_for(name) ⇒ Object

Enriches a single field’s config hash with the runtime data form_helper.rb expects (association object, select_options, …). Mirrors the layout enrichment Presenter::LayoutBuilder does for presenter form fields.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lcp_ruby/pages/filter_form.rb', line 75

def field_config_for(name)
  raw = page.filter_form_definition.find { |f| f["field"].to_s == name.to_s }
  return {} unless raw

  input_type    = raw["input_type"].to_s
  input_options = raw["input_options"] || {}

  config = {
    "field"         => raw["field"],
    "input_type"    => input_type,
    "input_options" => input_options,
    "placeholder"   => raw["placeholder"] || input_options["placeholder"],
    "label"         => resolve_label(raw)
  }
  config["select_options"] = build_select_options(input_options) if select_with_values?(input_type, input_options)
  merge_association(config, raw, input_type, input_options)
  config
end