Class: TypedEAV::Field::MultiSelect

Inherits:
Base show all
Includes:
Optionable
Defined in:
app/models/typed_eav/field/multi_select.rb

Overview

Multi-choice option-set field. Stores the chosen values as a JSON array in ‘json_value`. Inherits `optionable? = true`, the public-facing sorted `allowed_values` helper, and the protected `validate_multi_option_inclusion` helper from `Field::Optionable`. Stays a direct child of `Field::Base` — Optionable is a concern (mixin), not an intermediate STI class, because Select and MultiSelect don’t share a ‘value_column`.

‘validate_array_size` is called directly from `Field::Base` (cross-family outlier; also used by `Field::IntegerArray`).

Constant Summary

Constants inherited from Base

Base::RESERVED_NAMES

Constants included from TypedStorage

TypedStorage::DEFAULT_OPERATORS_BY_COLUMN, TypedStorage::FALLBACK_OPERATORS

Instance Method Summary collapse

Methods included from Optionable

#allowed_values, #optionable?

Methods inherited from Base

#allowed_option_values, #backfill_default!, #clear_option_cache!, #default_value, #default_value=, export_schema, #field_type_name, import_schema, #insert_at, #move_higher, #move_lower, #move_to_bottom, #move_to_top, #optionable?

Methods included from TypedStorage

#after_snapshot, #apply_default, #before_snapshot, #read_value, #value_changed?, #write_value

Instance Method Details

#array_field?Boolean

Returns:



21
# File 'app/models/typed_eav/field/multi_select.rb', line 21

def array_field? = true

#cast(raw) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/typed_eav/field/multi_select.rb', line 23

def cast(raw)
  return [nil, false] if raw.nil?

  # Rails emits a hidden "" sentinel for `select multiple: true` so an
  # empty submission still round-trips. Drop nil/blank elements here so
  # the inclusion check doesn't reject the form's own placeholder.
  elements = Array(raw).filter_map do |v|
    next nil if v.nil?

    s = v.to_s
    s.strip.empty? ? nil : s
  end
  [elements.presence, false]
end

#validate_typed_value(record, val) ⇒ Object



38
39
40
41
# File 'app/models/typed_eav/field/multi_select.rb', line 38

def validate_typed_value(record, val)
  validate_multi_option_inclusion(record, val)
  validate_array_size(record, val)
end