Module: MongoTranslatable::Translated::InstanceMethods

Defined in:
lib/mongo_translatable.rb

Instance Method Summary collapse

Instance Method Details

#available_in_these_localesObject

get a list of locales as syms for all translations locales, plus object’s original locale



273
274
275
# File 'lib/mongo_translatable.rb', line 273

def available_in_these_locales
  [original_locale] + translations_locales.collect(&:locale)
end

#needed_in_these_localesObject

list of locales that haven’t been translated yet



278
279
280
# File 'lib/mongo_translatable.rb', line 278

def needed_in_these_locales
  TranslationsHelper::available_locales.keys - available_in_these_locales
end

#set_translation_for_this(attribute_name, translated_value) ⇒ Object

this will replace specified attribute with its translated value taks an attribute name as a string or symbol



259
260
261
# File 'lib/mongo_translatable.rb', line 259

def set_translation_for_this(attribute_name, translated_value)
  send(attribute_name.to_s + "=", translated_value)
end

#translate(options = {}) ⇒ Object

this will create a new translation (but won’t save it) with either passed in options note that we don’t save the changes to self only the new translation will return nothing if translate to locale is the same as the object to translate’s original locale



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/mongo_translatable.rb', line 293

def translate(options = {})
  translation_locale = options[:locale] || I18n.locale

  @translation = self.class::Translation.new({
    :locale => translation_locale,
    :translatable_locale => self.locale, # save original locale
    self.class.as_foreign_key_sym => id
  })

  if translation_locale.to_s == original_locale.to_s
    # TODO: locale's emptiness is the reported error
    # when this is triggered, figure out why
    # serving its purpose though to prevent a translation to be added for original_locale
    @translation.errors.replace(:locale, "Cannot add translation the same as the original locale.")
  else
    # work through self and replace attributes
    # with the passed in translations for defined translatable_attributes
    translatable_attributes.each do |translated_attribute|
      translated_value = options[translated_attribute]
      @translation.send("#{translated_attribute.to_sym}=", translated_value) if translated_value.present?
    end
  end

  @translation
end

#translation_for(locale) ⇒ Object

assumes unique locale



283
284
285
# File 'lib/mongo_translatable.rb', line 283

def translation_for(locale)
  self.class::Translation.first(self.class.as_foreign_key_sym => id, :locale => locale)
end

#translationsObject



263
264
265
# File 'lib/mongo_translatable.rb', line 263

def translations
  self.class::Translation.all(self.class.as_foreign_key_sym => id)
end

#translations_localesObject

sometimes all you need is only the locales of translations



268
269
270
# File 'lib/mongo_translatable.rb', line 268

def translations_locales
  self.class::Translation.all(self.class.as_foreign_key_sym => id, :select => 'locale')
end

#update_translation_for_methods_if_necessary_with(new_translatable_attributes) ⇒ Object

for classes that have dynamice translatable attributes, we may need to update accessor methods for a dynamic translatable attribute



249
250
251
252
253
254
255
# File 'lib/mongo_translatable.rb', line 249

def update_translation_for_methods_if_necessary_with(new_translatable_attributes)
  new_translatable_attributes.each do |attribute|
    unless respond_to?("#{attribute.to_s}_translation_for".to_sym)
      self.class.define_translation_accessor_method_for(attribute)
    end
  end
end