class Mongoid::Verbalize::TranslatedString
Strongly-typed accessor for this structure: {
'en' => { "value" => "Title", "versions" => [ { "version" => 0, "value" => 'Title' } ] }, 'es' => { "value" => "Título", "versions" => [ { "version" => 0, "value" => 'Título' } ] },
}
Constants
- LocalizedVersion
Attributes
Public Class Methods
Return current locale as string
# File lib/mongoid/verbalize/translated_string.rb, line 124 def current_locale ::I18n.locale end
Get the object as it was stored in the database, and instantiate this custom class from it.
# File lib/mongoid/verbalize/translated_string.rb, line 95 def demongoize(object) localized_values = object.each_with_object({}) do |(key, value), h| versions = (value['versions'] || []).map do |v| LocalizedVersion.new(v['version'], v['value']) end h[key.to_sym] = LocalizedValue.new(value['value'], versions) end TranslatedString.new(localized_values) end
Converts the object that was supplied to a criteria and converts it into a database friendly form.
# File lib/mongoid/verbalize/translated_string.rb, line 116 def evolve(object) case object when TranslatedString then object.mongoize.current_locale_value else object end end
Takes any possible object and converts it to how it would be stored in the database.
# File lib/mongoid/verbalize/translated_string.rb, line 107 def mongoize(object) case object when TranslatedString then object.mongoize else object end end
# File lib/mongoid/verbalize/translated_string.rb, line 35 def initialize(localized_values) @localized_values = localized_values end
Public Instance Methods
Called when determining whether to create a new version.
# File lib/mongoid/verbalize/translated_string.rb, line 69 def changed? @localized_values.values.any?(&:changed?) end
Return translated value of field, accoring to current locale. If :use_default_if_empty is set, then in case when there no translation available for current locale, if will try to get translation for defalt_locale.
# File lib/mongoid/verbalize/translated_string.rb, line 50 def current_locale_value(use_default_if_empty) lookups = [self.class.current_locale] # TODO: Add I18n.fallbacks support instead of :use_default_if_empty if use_default_if_empty lookups.push(::I18n.default_locale) end # Find first localized value in lookup path localized_value = @localized_values[lookups.find { |l| @localized_values[l] }] return nil if localized_value.nil? localized_value.current_value end
# File lib/mongoid/verbalize/translated_string.rb, line 64 def current_locale_value=(value) current_value.current_value = value end
# File lib/mongoid/verbalize/translated_string.rb, line 39 def find_version(language, version) localized_value = @localized_values[language] return unless localized_value.present? localized_value.find_version(version) end
Converts an object of this instance into a database friendly value.
# File lib/mongoid/verbalize/translated_string.rb, line 81 def mongoize @localized_values.each_with_object({}) do |(key, value), h| h[key.to_s] = { 'value' => value.current_value, 'versions' => value.versions.map do |v| { 'version' => v.version, 'value' => v.value } end } end end
Creates a new version for changed values
# File lib/mongoid/verbalize/translated_string.rb, line 74 def prepare_for_save(new_version_number) @localized_values.values.select(&:changed?).each do |v| v.add_version(new_version_number) end end
Private Instance Methods
TODO: Rename this method.
# File lib/mongoid/verbalize/translated_string.rb, line 132 def current_value @localized_values[self.class.current_locale] ||= LocalizedValue.new end