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: Coder, 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.
54 55 56 57 58 59 60 61 62 |
# File 'lib/mobility/backends/active_record/container.rb', line 54 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
64 65 66 |
# File 'lib/mobility/backends/active_record/container.rb', line 64 def column_type @column_type ||= get_column_type end |
.configure(options) ⇒ Object
44 45 46 47 |
# File 'lib/mobility/backends/active_record/container.rb', line 44 def configure() [:column_name] ||= :translations [:column_name] = [:column_name].to_sym end |
Instance Method Details
#each_locale {|Locale| ... } ⇒ Object
Yields locales available for this attribute.
80 81 82 83 84 |
# File 'lib/mobility/backends/active_record/container.rb', line 80 def each_locale model[column_name].each do |l, v| yield l.to_sym if v.present? 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 |
# File 'lib/mobility/backends/active_record/container.rb', line 24 def read(locale, _ = nil) model_translations(locale)[attribute] 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.
34 35 36 37 |
# File 'lib/mobility/backends/active_record/container.rb', line 34 def write(locale, value, _ = nil) set_attribute_translation(locale, value) model_translations(locale)[attribute] end |