Class: Spree::Metafields::Json

Inherits:
Spree::Metafield show all
Defined in:
app/models/spree/metafields/json.rb

Constant Summary

Constants inherited from Spree::Metafield

Spree::Metafield::FIELD_TYPE_TOKENS, Spree::Metafield::TYPE_CLASS_TO_TOKEN, Spree::Metafield::TYPE_TOKENS

Instance Method Summary collapse

Methods inherited from Spree::Metafield

#field_type

Instance Method Details

#csv_valueObject



22
23
24
# File 'app/models/spree/metafields/json.rb', line 22

def csv_value
  value.to_s
end

#serialize_valueObject



16
17
18
19
20
# File 'app/models/spree/metafields/json.rb', line 16

def serialize_value
  JSON.parse(value)
rescue JSON::ParserError
  value
end

#value=(raw) ⇒ Object

Accept either a JSON-serialized String (from CSV / Admin UI text input) or a raw Hash / Array (from API callers that ship parsed objects). Non-String inputs get JSON-serialized so the underlying text column always holds canonical JSON.

Parameters:

  • raw (String, Hash, Array, nil)


12
13
14
# File 'app/models/spree/metafields/json.rb', line 12

def value=(raw)
  super(raw.is_a?(Hash) || raw.is_a?(Array) ? raw.to_json : raw)
end