Module: TranslationFromVersion
- Defined in:
- lib/kete_translatable_content/extensions/translation_from_version.rb
Overview
For classes where their ::Version subclass is what has mongo_translatable declaration is on
Instance Method Summary collapse
-
#available_in_these_locales ⇒ Object
because of versioning, we may have an old version that is the same locale as original, modified to handle this case.
- #locales_of_versions ⇒ Object
- #translate(options = {}) ⇒ Object
-
#translation_for(locale) ⇒ Object
Get the current translation or the latest.
-
#translations_locales ⇒ Object
sometimes all you need is only the locales of translations because of versioning we need to split out duplicates.
Instance Method Details
#available_in_these_locales ⇒ Object
because of versioning, we may have an old version that is the same locale as original, modified to handle this case
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kete_translatable_content/extensions/translation_from_version.rb', line 12 def available_in_these_locales locales = translations_locales.collect(&:locale) + locales_of_versions # we only need one mention of the locale locales.uniq! # get rid of any extra original_locale mentions locales.delete(original_locale) [original_locale] + locales end |
#locales_of_versions ⇒ Object
5 6 7 8 |
# File 'lib/kete_translatable_content/extensions/translation_from_version.rb', line 5 def locales_of_versions @locales_of_versions ||= versions.find(:all, :select => 'distinct locale').collect(&:locale) end |
#translate(options = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/kete_translatable_content/extensions/translation_from_version.rb', line 79 def translate( = {}) translation_locale = [: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, :version => self.version }) if translation_locale.to_s == original_locale.to_s # 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 = [translated_attribute] @translation.send("#{translated_attribute.to_sym}=", translated_value) if translated_value.present? end end @translation end |
#translation_for(locale) ⇒ Object
Get the current translation or the latest
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kete_translatable_content/extensions/translation_from_version.rb', line 38 def translation_for(locale) translation = self.class::Translation.first(self.class.as_foreign_key_sym => id, :locale => locale, :version => self.version.to_s) # none available for the specified version, get latest translation ||= self.class::Translation.first(self.class.as_foreign_key_sym => id, :locale => locale) # none available for locale at all # see if there is an earlier version of the item # in the locale specified # find version with original_locale that matches # revert_to version and return that unless translation version = versions.find_by_locale(locale) if version.present? # if attribute is an extended field # doing a direct call on the version won't work # so instantiating a tempory dummy item dummy_attributes = version.attributes dummy_attributes.delete(self.class.as_foreign_key_sym.to_s) dummy_attributes.delete('id') dummy = self.class.new(dummy_attributes) translatable_attributes_from_version = Hash.new translatable_attributes.each do |attr| translatable_attributes_from_version[attr] = dummy.send(attr) end dummy = nil translatable_attributes_from_version[:locale] = locale translation = self.class::Translation.new(translatable_attributes_from_version) end end translation end |
#translations_locales ⇒ Object
sometimes all you need is only the locales of translations because of versioning we need to split out duplicates
27 28 29 30 31 32 33 34 35 |
# File 'lib/kete_translatable_content/extensions/translation_from_version.rb', line 27 def translations_locales translations = self.class::Translation.all(self.class.as_foreign_key_sym => id, :select => 'locale') uniq_translations = [] translations.each do |trans| uniq_translations << trans unless uniq_translations.collect(&:locale).include?(trans.locale) end uniq_translations end |