Class: Mongoid::Verbalize::TranslatedString

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/verbalize/translated_string.rb

Overview

Strongly-typed accessor for this structure: {

'en' => { "value" => "Title",  "versions" => [ { "version" => 0, "value" => 'Title' } ] },
'es' => { "value" => "Título", "versions" => [ { "version" => 0, "value" => 'Título' } ] },

}

Defined Under Namespace

Classes: LocalizedValue, LocalizedVersion

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(localized_values) ⇒ TranslatedString

Returns a new instance of TranslatedString.



35
36
37
# File 'lib/mongoid/verbalize/translated_string.rb', line 35

def initialize(localized_values)
  @localized_values = localized_values
end

Instance Attribute Details

#localized_valuesObject (readonly)

Returns the value of attribute localized_values.



33
34
35
# File 'lib/mongoid/verbalize/translated_string.rb', line 33

def localized_values
  @localized_values
end

Class Method Details

.current_localeObject

Return current locale as string



124
125
126
# File 'lib/mongoid/verbalize/translated_string.rb', line 124

def current_locale
  ::I18n.locale
end

.demongoize(object) ⇒ Object

Get the object as it was stored in the database, and instantiate this custom class from it.



95
96
97
98
99
100
101
102
103
# 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

.evolve(object) ⇒ Object

Converts the object that was supplied to a criteria and converts it into a database friendly form.



116
117
118
119
120
121
# 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

.mongoize(object) ⇒ Object

Takes any possible object and converts it to how it would be stored in the database.



107
108
109
110
111
112
# File 'lib/mongoid/verbalize/translated_string.rb', line 107

def mongoize(object)
  case object
  when TranslatedString then object.mongoize
  else object
  end
end

Instance Method Details

#changed?Boolean

Called when determining whether to create a new version.

Returns:

  • (Boolean)


69
70
71
# File 'lib/mongoid/verbalize/translated_string.rb', line 69

def changed?
  @localized_values.values.any?(&:changed?)
end

#current_locale_value(use_default_if_empty) ⇒ Object

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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

#current_locale_value=(value) ⇒ Object



64
65
66
# File 'lib/mongoid/verbalize/translated_string.rb', line 64

def current_locale_value=(value)
  current_value.current_value = value
end

#find_version(language, version) ⇒ Object



39
40
41
42
43
44
# 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

#mongoizeObject

Converts an object of this instance into a database friendly value.



81
82
83
84
85
86
87
88
89
90
# 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

#prepare_for_save(new_version_number) ⇒ Object

Creates a new version for changed values



74
75
76
77
78
# 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