Class: LcpRuby::ModelFactory::JsonTypeApplicator
- Inherits:
-
Object
- Object
- LcpRuby::ModelFactory::JsonTypeApplicator
- Defined in:
- lib/lcp_ruby/model_factory/json_type_applicator.rb
Overview
Registers an explicit ActiveRecord ‘:json` attribute type for json-backed fields.
PostgreSQL (jsonb) and SQLite (json) are reflected correctly by their adapters, but MariaDB reports JSON columns as ‘longtext` — so without an explicit type ActiveRecord treats the column as text and serializes a Hash via Ruby’s ‘Hash#to_s` (`“k”=>“v”`). That is invalid JSON and violates MariaDB’s implicit ‘json_valid()` CHECK constraint on insert. Declaring the attribute type makes Hash<->JSON (de)serialization adapter-agnostic.
Array fields are also json-backed on non-PostgreSQL adapters, but ArrayTypeApplicator already registers their own ArrayType, so they are excluded here. The custom_data column (custom fields) is handled separately in CustomFields::Applicator since it is not part of ‘model_definition.fields`.
Instance Method Summary collapse
- #apply! ⇒ Object
-
#initialize(model_class, model_definition) ⇒ JsonTypeApplicator
constructor
A new instance of JsonTypeApplicator.
Constructor Details
#initialize(model_class, model_definition) ⇒ JsonTypeApplicator
Returns a new instance of JsonTypeApplicator.
18 19 20 21 |
# File 'lib/lcp_ruby/model_factory/json_type_applicator.rb', line 18 def initialize(model_class, model_definition) @model_class = model_class @model_definition = model_definition end |
Instance Method Details
#apply! ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lcp_ruby/model_factory/json_type_applicator.rb', line 23 def apply! @model_definition.fields.each do |field| next if field.array? next unless field.column_type == LcpRuby.json_column_type = {} [:default] = field.default unless field.default.nil? @model_class.attribute field.name.to_sym, :json, ** end end |