Module: TypedEAV::Field::Optionable
- Extended by:
- ActiveSupport::Concern
- Included in:
- MultiSelect, Select
- Defined in:
- app/models/typed_eav/field/optionable.rb
Overview
Concern (mixin) for field types whose valid values are constrained
to an enumeration of Field::Option rows.
Included by: Field::Select, Field::MultiSelect. Both leaves stay
as direct children of Field::Base — Select uses string_value,
MultiSelect uses json_value. Inheritance can't unify storage for
both, so we use a concern instead of an intermediate class.
Provides:
optionable?overridden totrue(default onField::Baseisfalse).allowed_values— the public-facing, sorted option-values helper. Whenfield_optionsis loaded, sorts in memory by[sort_order || 0, label]; otherwise issues asorted.pluckquery. Matches the per-leaf implementation that was duplicated verbatim between Select and MultiSelect pre-refactor.validate_option_inclusion/validate_multi_option_inclusionprotected helpers (moved fromField::Base). Both call intoallowed_option_values(the validator-facing fast path that still lives onField::Base) so they avoid the sort overhead on the hot validation path.
Public extension point: external authors can include TypedEAV::Field::Optionable to opt into the option-set surface
without joining the Select/MultiSelect inheritance chain (see
README §"Custom field types").
Instance Method Summary collapse
Instance Method Details
#allowed_values ⇒ Object
37 38 39 40 41 42 43 |
# File 'app/models/typed_eav/field/optionable.rb', line 37 def allowed_values if .loaded? .sort_by { |o| [o.sort_order || 0, o.label.to_s] }.map(&:value) else .sorted.pluck(:value) end end |
#optionable? ⇒ Boolean
35 |
# File 'app/models/typed_eav/field/optionable.rb', line 35 def optionable? = true |