class Mobility::Backends::Sequel::Container
Implements the {Mobility::Backends::Container} backend for Sequel
models.
Public Class Methods
@!endgroup
@!group Backend
Configuration @option options [Symbol] column_name (:translations) Name of column on which to store translations
# File lib/mobility/backends/sequel/container.rb, line 42 def self.configure(options) options[:column_name] ||= :translations options[:column_name] = options[:column_name].to_sym column_name, db_schema = options[:column_name], model_class.db_schema options[:column_type] = db_schema[column_name] && (db_schema[column_name][:db_type]).to_sym unless %i[json jsonb].include?(options[:column_type]) raise InvalidColumnType, "#{options[:column_name]} must be a column of type json or jsonb" end end
Private Class Methods
@param [Symbol] name Attribute name @param [Symbol] locale Locale @return [Mobility::Backends::Sequel::Container::JSONOp,Mobility::Backends::Sequel::Container::JSONBOp]
# File lib/mobility/backends/sequel/container.rb, line 103 def self.build_op(attr, locale) klass = const_get("#{options[:column_type].upcase}Op") klass.new(klass.new(column_name.to_sym)[locale.to_s]).get_text(attr) end
Public Instance Methods
@!endgroup
@!macro backend_iterator
# File lib/mobility/backends/sequel/container.rb, line 54 def each_locale model[column_name].each do |l, _| yield l.to_sym unless read(l).nil? end end
@!group Backend
Accessors
@note Translation may be a string, integer, boolean, hash or array
since value is stored on a JSON hash.
@param [Symbol] locale Locale to read @param [Hash] options @return [String,Integer,Boolean] Value of translation
# File lib/mobility/backends/sequel/container.rb, line 24 def read(locale, _ = nil) model_translations(locale)[attribute] end
@note Translation may be a string, integer, boolean, hash or array
since value is stored on a JSON hash.
@param [Symbol] locale Locale to write @param [String,Integer,Boolean] value Value to write @param [Hash] options @return [String,Integer,Boolean] Updated value
# File lib/mobility/backends/sequel/container.rb, line 34 def write(locale, value, _ = nil) set_attribute_translation(locale, value) model_translations(locale)[attribute] end
Private Instance Methods
# File lib/mobility/backends/sequel/container.rb, line 82 def model_translations(locale) model[column_name][locale.to_s] ||= {} end
# File lib/mobility/backends/sequel/container.rb, line 86 def set_attribute_translation(locale, value) translations = model[column_name] || {} translations[locale.to_s] ||= {} # Explicitly mark translations column as changed if value changed, # otherwise Sequel will not detect it. # TODO: Find a cleaner/easier way to do this. if translations[locale.to_s][attribute] != value model.instance_variable_set(:@changed_columns, model.changed_columns | [column_name]) end translations[locale.to_s][attribute] = value end