Class: Mobility::Backends::ActiveRecord::Container
- Inherits:
-
Object
- Object
- Mobility::Backends::ActiveRecord::Container
- Includes:
- Mobility::Backends::ActiveRecord, Container
- Defined in:
- lib/mobility/backends/active_record/container.rb
Overview
Implements the Container backend for ActiveRecord models.
Defined Under Namespace
Classes: InvalidColumnType
Backend Accessors collapse
-
#read(locale, _ = nil) ⇒ String, ...
Value of translation.
-
#write(locale, value, _ = nil) ⇒ String, ...
Updated value.
Backend Configuration collapse
Class Method Summary collapse
-
.build_node(attr, locale) ⇒ Mobility::Plugins::Arel::Nodes::Json, Mobility::Arel::Nodes::Jsonb
Arel node for attribute on json or jsonb column.
- .column_type ⇒ Object
Instance Method Summary collapse
-
#each_locale {|Locale| ... } ⇒ Object
Yields locales available for this attribute.
Methods included from Container
Methods included from Mobility::Backends::ActiveRecord
Class Method Details
.build_node(attr, locale) ⇒ Mobility::Plugins::Arel::Nodes::Json, Mobility::Arel::Nodes::Jsonb
Returns Arel node for attribute on json or jsonb column.
57 58 59 60 61 62 63 64 65 |
# File 'lib/mobility/backends/active_record/container.rb', line 57 def build_node(attr, locale) column = model_class.arel_table[column_name] case column_type when :json Plugins::Arel::Nodes::JsonContainer.new(column, build_quoted(locale), build_quoted(attr)) when :jsonb Plugins::Arel::Nodes::JsonbContainer.new(column, build_quoted(locale), build_quoted(attr)) end end |
.column_type ⇒ Object
67 68 69 |
# File 'lib/mobility/backends/active_record/container.rb', line 67 def column_type @column_type ||= get_column_type end |
.configure(options) ⇒ Object
48 49 50 |
# File 'lib/mobility/backends/active_record/container.rb', line 48 def configure() [:column_name] = [:column_name]&.to_sym || :translations end |
Instance Method Details
#each_locale {|Locale| ... } ⇒ Object
Yields locales available for this attribute.
83 84 85 86 87 |
# File 'lib/mobility/backends/active_record/container.rb', line 83 def each_locale model[column_name].each_key do |l| yield l.to_sym end end |
#read(locale, _ = nil) ⇒ String, ...
Note:
Translation may be a string, integer, boolean, hash or array since value is stored on a JSON hash.
Returns Value of translation.
24 25 26 27 28 29 30 |
# File 'lib/mobility/backends/active_record/container.rb', line 24 def read(locale, _ = nil) locale_translations = model_translations(locale) return unless locale_translations locale_translations[attribute.to_s] end |
#write(locale, value, _ = nil) ⇒ String, ...
Note:
Translation may be a string, integer, boolean, hash or array since value is stored on a JSON hash.
Returns Updated value.
38 39 40 41 |
# File 'lib/mobility/backends/active_record/container.rb', line 38 def write(locale, value, _ = nil) set_attribute_translation(locale, value) read(locale) end |