Class: TypedEAV::Field::Base
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- TypedEAV::Field::Base
- Includes:
- ColumnMapping
- Defined in:
- app/models/typed_eav/field/base.rb
Direct Known Subclasses
Boolean, Color, Date, DateArray, DateTime, Decimal, DecimalArray, Email, Integer, IntegerArray, Json, LongText, MultiSelect, Select, Text, TextArray, Url
Constant Summary collapse
- RESERVED_NAMES =
── Validations ──
%w[id type class created_at updated_at].freeze
Constants included from ColumnMapping
ColumnMapping::DEFAULT_OPERATORS_BY_COLUMN, ColumnMapping::FALLBACK_OPERATORS
Instance Method Summary collapse
-
#allowed_option_values ⇒ Object
Allowed option values for select/multi-select validation.
- #array_field? ⇒ Boolean
-
#cast(raw) ⇒ Object
── Type casting ── Returns a tuple: [casted_value, invalid?].
-
#clear_option_cache! ⇒ Object
Kept for backward compatibility but now a no-op since we don’t cache.
-
#default_value ⇒ Object
── Default value handling ── Stored in default_value_meta as <raw_value> so the jsonb column can hold any type’s default without an extra typed column.
- #default_value=(val) ⇒ Object
-
#field_type_name ⇒ Object
── Introspection ──.
- #optionable? ⇒ Boolean
-
#validate_typed_value(record, val) ⇒ Object
── Per-type value validation (polymorphic dispatch from Value) ──.
Instance Method Details
#allowed_option_values ⇒ Object
Allowed option values for select/multi-select validation. When ‘field_options` is already loaded (eager-load path), read from memory instead of issuing a fresh `pluck` query.
98 99 100 101 102 103 104 |
# File 'app/models/typed_eav/field/base.rb', line 98 def allowed_option_values if .loaded? .map(&:value) else .pluck(:value) end end |
#array_field? ⇒ Boolean
87 88 89 |
# File 'app/models/typed_eav/field/base.rb', line 87 def array_field? false end |
#cast(raw) ⇒ Object
── Type casting ──Returns a tuple: [casted_value, invalid?].
-
casted_value is the coerced value (or nil when raw is nil/blank)
-
invalid? is true when raw was non-empty but unparseable for this type; Value#validate_value uses the flag to surface :invalid errors (vs :blank for nil-from-nil).
Subclasses override to enforce type semantics. Default is an identity pass-through that never flags invalid.
Callers that only need the coerced value should use ‘cast(raw).first`.
77 78 79 |
# File 'app/models/typed_eav/field/base.rb', line 77 def cast(raw) [raw, false] end |
#clear_option_cache! ⇒ Object
Kept for backward compatibility but now a no-op since we don’t cache.
107 108 109 |
# File 'app/models/typed_eav/field/base.rb', line 107 def clear_option_cache! # no-op end |
#default_value ⇒ Object
── Default value handling ──Stored in default_value_meta as <raw_value> so the jsonb column can hold any type’s default without an extra typed column.
56 57 58 |
# File 'app/models/typed_eav/field/base.rb', line 56 def default_value cast(["v"]).first end |
#default_value=(val) ⇒ Object
60 61 62 |
# File 'app/models/typed_eav/field/base.rb', line 60 def default_value=(val) ["v"] = val end |
#field_type_name ⇒ Object
── Introspection ──
83 84 85 |
# File 'app/models/typed_eav/field/base.rb', line 83 def field_type_name self.class.name.demodulize.underscore end |
#optionable? ⇒ Boolean
91 92 93 |
# File 'app/models/typed_eav/field/base.rb', line 91 def optionable? false end |
#validate_typed_value(record, val) ⇒ Object
── Per-type value validation (polymorphic dispatch from Value) ──
Default no-op. Subclasses override to enforce their constraints (length, range, pattern, option inclusion, array size, etc.) and add errors to ‘record.errors`. Shared helpers below (validate_length, validate_pattern, validate_range, etc.) are available to subclasses.
117 118 119 |
# File 'app/models/typed_eav/field/base.rb', line 117 def validate_typed_value(record, val) # no-op by default end |