Class: Mobility::Backends::ActiveRecord::KeyValue
- Inherits:
-
Object
- Object
- Mobility::Backends::ActiveRecord::KeyValue
- Includes:
- Mobility::Backends::ActiveRecord, KeyValue
- Defined in:
- lib/mobility/backends/active_record/key_value.rb
Overview
Implements the KeyValue backend for ActiveRecord models.
Defined Under Namespace
Modules: DestroyKeyValueTranslations Classes: StringTranslation, TextTranslation, Translation, Visitor
Backend Configuration collapse
Class Method Summary collapse
-
.apply_scope(relation, predicate, locale = Mobility.locale, invert: false) ⇒ ActiveRecord::Relation
Joins translations using either INNER/OUTER join appropriate to the query.
-
.build_node(attr, locale) ⇒ Mobility::Plugins::Arel::Attribute
Arel attribute for aliased translation table value column.
Instance Method Summary collapse
-
#translation_for(locale) ⇒ Mobility::Backends::ActiveRecord::KeyValue::TextTranslation, Mobility::Backends::ActiveRecord::KeyValue::StringTranslation
Returns translation for a given locale, or builds one if none is present.
Methods included from KeyValue
#association_name, #class_name, #each_locale, #read, #write
Methods included from Mobility::Backends::ActiveRecord
Class Method Details
.apply_scope(relation, predicate, locale = Mobility.locale, invert: false) ⇒ ActiveRecord::Relation
Joins translations using either INNER/OUTER join appropriate to the query.
61 62 63 64 65 66 67 |
# File 'lib/mobility/backends/active_record/key_value.rb', line 61 def apply_scope(relation, predicate, locale = Mobility.locale, invert: false) visitor = Visitor.new(self, locale) visitor.accept(predicate).inject(relation) do |rel, (attr, join_type)| join_type &&= ::Arel::Nodes::InnerJoin if invert join_translations(rel, attr, locale, join_type) end end |
.build_node(attr, locale) ⇒ Mobility::Plugins::Arel::Attribute
Returns Arel attribute for aliased translation table value column.
50 51 52 53 |
# File 'lib/mobility/backends/active_record/key_value.rb', line 50 def build_node(attr, locale) aliased_table = class_name.arel_table.alias(table_alias(attr, locale)) Plugins::Arel::Attribute.new(aliased_table, :value, locale, self, attr.to_sym) end |
.configure(options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mobility/backends/active_record/key_value.rb', line 34 def configure() super if type = [:type] [:association_name] ||= :"#{[:type]}_translations" [:class_name] ||= const_get("#{type.capitalize}Translation") end [:table_alias_affix] = "#{model_class}_%s_#{[:association_name]}" rescue NameError raise ArgumentError, "You must define a Mobility::Backends::ActiveRecord::KeyValue::#{type.capitalize}Translation class." end |
Instance Method Details
#translation_for(locale) ⇒ Mobility::Backends::ActiveRecord::KeyValue::TextTranslation, Mobility::Backends::ActiveRecord::KeyValue::StringTranslation
Returns translation for a given locale, or builds one if none is present.
196 197 198 199 200 |
# File 'lib/mobility/backends/active_record/key_value.rb', line 196 def translation_for(locale, **) translation = translations.find { |t| t.key == attribute && t.locale == locale.to_s } translation ||= translations.build(locale: locale, key: attribute) translation end |