Module: HasTranslations::ModelAdditions
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/has_translations/model_additions.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #all_translations ⇒ Object
- #find_or_build_translation(locale) ⇒ Object
- #find_or_create_translation(locale) ⇒ Object
- #find_translation(locale) ⇒ Object
- #has_translation?(locale) ⇒ Boolean
- #translation(locale, fallback = ) ⇒ Object
Instance Method Details
#all_translations ⇒ Object
105 106 107 108 109 110 |
# File 'lib/has_translations/model_additions.rb', line 105 def all_translations t = I18n.available_locales.map do |locale| [locale, find_or_create_translation(locale)] end ActiveSupport::OrderedHash[t] end |
#find_or_build_translation(locale) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/has_translations/model_additions.rb', line 94 def find_or_build_translation(locale) locale = locale.to_s (find_translation(locale) || self.translations.build).tap do |t| t.locale = locale end end |
#find_or_create_translation(locale) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/has_translations/model_additions.rb', line 86 def find_or_create_translation(locale) locale = locale.to_s (find_translation(locale) || self.[:translation_class].new).tap do |t| t.locale = locale t.send(:"#{self.class.model_name.to_s.demodulize.underscore.to_sym}_id=", self.id) end end |
#find_translation(locale) ⇒ Object
116 117 118 119 |
# File 'lib/has_translations/model_additions.rb', line 116 def find_translation(locale) locale = locale.to_s translations.detect { |t| t.locale == locale } end |
#has_translation?(locale) ⇒ Boolean
112 113 114 |
# File 'lib/has_translations/model_additions.rb', line 112 def has_translation?(locale) find_translation(locale).present? end |
#translation(locale, fallback = ) ⇒ Object
101 102 103 |
# File 'lib/has_translations/model_additions.rb', line 101 def translation(locale, fallback=[:fallback]) find_translation(locale) || (fallback && !translations.empty? ? find_translation(I18n.default_locale) || translations.first : nil) end |