Module: Mobility::Backend::Serialized
- Includes:
- OrmDelegator
- Defined in:
- lib/mobility/backend/serialized.rb
Overview
Stores translations as serialized attributes in a single text column. This implies that the translated values are not searchable, and thus this backend is not recommended unless specific constraints prevent use of other solutions.
To use this backend, ensure that the model table has a text column on its table with the same name as the translated attribute.
Backend Options
format
Format for serialization. Either :yaml
(default) or :json
.
Class Method Summary collapse
Methods included from OrmDelegator
Class Method Details
.deserializer_for(format) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/mobility/backend/serialized.rb', line 42 def deserializer_for(format) case format when :yaml lambda { |v| YAML.load(v) } when :json lambda { |v| JSON.parse(v, symbolize_names: true) } end end |
.serializer_for(format) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mobility/backend/serialized.rb', line 26 def serializer_for(format) lambda do |obj| return if obj.nil? if obj.is_a? Hash obj = obj.inject({}) do |translations, (locale, value)| translations[locale] = value.to_s if value.present? translations end else raise ArgumentError, "Attribute is supposed to be a Hash, but was a #{obj.class}. -- #{obj.inspect}" end obj.send("to_#{format}") end end |