Class: Mobility::Backend::ActiveRecord::KeyValue
- Includes:
- Mobility::Backend
- Defined in:
- lib/mobility/backend/active_record/key_value.rb
Overview
Implements the KeyValue backend for ActiveRecord models.
Defined Under Namespace
Classes: QueryMethods
Instance Attribute Summary collapse
-
#association_name ⇒ Symbol
readonly
Name of the association.
Attributes included from Mobility::Backend
Backend Accessors collapse
-
#read(locale, **options) ⇒ Object
Value of translation.
-
#write(locale, value, **options) ⇒ Object
Value of translation.
Backend Configuration collapse
Cache Methods collapse
Instance Method Summary collapse
-
#initialize(model, attribute, **options) ⇒ KeyValue
constructor
A new instance of KeyValue.
-
#translation_for(locale) ⇒ Mobility::ActiveRecord::TextTranslation, Mobility::ActiveRecord::StringTranslation
Returns translation for a given locale, or builds one if none is present.
- #translations ⇒ Object
Methods included from Mobility::Backend
Constructor Details
#initialize(model, attribute, **options) ⇒ KeyValue
Returns a new instance of KeyValue.
31 32 33 34 |
# File 'lib/mobility/backend/active_record/key_value.rb', line 31 def initialize(model, attribute, **) super @association_name = [:association_name] end |
Instance Attribute Details
#association_name ⇒ Symbol (readonly)
Returns Name of the association.
27 28 29 |
# File 'lib/mobility/backend/active_record/key_value.rb', line 27 def association_name @association_name end |
Class Method Details
.configure!(options) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mobility/backend/active_record/key_value.rb', line 53 def self.configure!() [:type] ||= :text case type = [:type].to_sym when :text, :string [:class_name] ||= Mobility::ActiveRecord.const_get("#{type.capitalize}Translation") else raise ArgumentError, "type must be one of: [text, string]" end [:class_name] = [:class_name].constantize if [:class_name].is_a?(String) [:association_name] ||= [:class_name].table_name.to_sym %i[type association_name].each { |key| [key] = [key].to_sym } end |
Instance Method Details
#new_cache ⇒ KeyValue::TranslationsCache
102 103 104 |
# File 'lib/mobility/backend/active_record/key_value.rb', line 102 def new_cache KeyValue::TranslationsCache.new(self) end |
#read(locale, **options) ⇒ Object
Returns Value of translation.
38 39 40 |
# File 'lib/mobility/backend/active_record/key_value.rb', line 38 def read(locale, **) translation_for(locale).value end |
#translation_for(locale) ⇒ Mobility::ActiveRecord::TextTranslation, Mobility::ActiveRecord::StringTranslation
Returns translation for a given locale, or builds one if none is present.
115 116 117 118 119 |
# File 'lib/mobility/backend/active_record/key_value.rb', line 115 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 |
#translations ⇒ Object
121 122 123 |
# File 'lib/mobility/backend/active_record/key_value.rb', line 121 def translations model.send(association_name) end |
#write(locale, value, **options) ⇒ Object
Returns Value of translation.
43 44 45 |
# File 'lib/mobility/backend/active_record/key_value.rb', line 43 def write(locale, value, **) translation_for(locale).tap { |t| t.value = value }.value end |
#write_to_cache? ⇒ Boolean
107 108 109 |
# File 'lib/mobility/backend/active_record/key_value.rb', line 107 def write_to_cache? true end |