Class: Spree::Metafield

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/metafield.rb

Constant Summary collapse

TYPE_TOKENS =

Map of API-facing tokens to Ruby STI class names. The wire format is the token (‘short_text`); the database column stores the class name. Reads translate to the token via `field_type`; writes accept either form. Plugin-defined types fall through to the raw class name until 6.0 when a registration API lands.

{
  'short_text' => 'Spree::Metafields::ShortText',
  'long_text'  => 'Spree::Metafields::LongText',
  'rich_text'  => 'Spree::Metafields::RichText',
  'number'     => 'Spree::Metafields::Number',
  'boolean'    => 'Spree::Metafields::Boolean',
  'json'       => 'Spree::Metafields::Json'
}.freeze
TYPE_CLASS_TO_TOKEN =
TYPE_TOKENS.invert.freeze
FIELD_TYPE_TOKENS =

Array form consumed by serializers via ‘typelize field_type: Spree::Metafield::FIELD_TYPE_TOKENS`. Typelizer emits a string-literal union in TypeScript and `string, enum: […]` in OpenAPI (string-array form was added in typelizer 0.10.0).

TYPE_TOKENS.keys.freeze

Instance Method Summary collapse

Instance Method Details

#csv_valueObject



75
76
77
# File 'app/models/spree/metafield.rb', line 75

def csv_value
  value.to_s
end

#field_typeObject

API-facing form of the STI ‘type` column. Returns the token (`short_text`) when the row’s type is a registered built-in; falls through to the raw class name for plugin types.

self` reads the raw column to bypass AR’s STI reader (which returns the resolved class constant, not a string).



43
44
45
# File 'app/models/spree/metafield.rb', line 43

def field_type
  TYPE_CLASS_TO_TOKEN[self[:type]] || self[:type]
end

#serialize_valueObject



71
72
73
# File 'app/models/spree/metafield.rb', line 71

def serialize_value
  value
end