Class: TypedEAV::Field::Base

Inherits:
ApplicationRecord show all
Includes:
ColumnMapping
Defined in:
app/models/typed_eav/field/base.rb

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

Instance Method Details

#allowed_option_valuesObject

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 field_options.loaded?
    field_options.map(&:value)
  else
    field_options.pluck(:value)
  end
end

#array_field?Boolean

Returns:



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_valueObject

── 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(default_value_meta["v"]).first
end

#default_value=(val) ⇒ Object



60
61
62
# File 'app/models/typed_eav/field/base.rb', line 60

def default_value=(val)
  default_value_meta["v"] = val
end

#field_type_nameObject

── 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

Returns:



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