Class: SimpleModelTranslations::TranslationHelper
- Inherits:
-
Object
- Object
- SimpleModelTranslations::TranslationHelper
- Defined in:
- lib/simple_model_translations/translation_helper.rb
Instance Method Summary collapse
- #build_translation_for_locale(locale) ⇒ Object
- #current_locale_for_translation ⇒ Object
- #current_translation ⇒ Object
- #default_locale_for_translation ⇒ Object
- #find_or_build_current_translation ⇒ Object
- #find_or_build_translation_by_locale(locale) ⇒ Object
- #find_translation_by_locale(locale) ⇒ Object
- #force_translation_with_locale(locale) ⇒ Object
-
#initialize(record, translation_association = :translations) ⇒ TranslationHelper
constructor
A new instance of TranslationHelper.
Constructor Details
#initialize(record, translation_association = :translations) ⇒ TranslationHelper
Returns a new instance of TranslationHelper.
3 4 5 6 |
# File 'lib/simple_model_translations/translation_helper.rb', line 3 def initialize(record, translation_association = :translations) @record = record @translation_association = translation_association end |
Instance Method Details
#build_translation_for_locale(locale) ⇒ Object
16 17 18 |
# File 'lib/simple_model_translations/translation_helper.rb', line 16 def build_translation_for_locale(locale) translations.build(:locale => locale, foreign_object_key => @record) end |
#current_locale_for_translation ⇒ Object
20 21 22 |
# File 'lib/simple_model_translations/translation_helper.rb', line 20 def current_locale_for_translation I18n.locale end |
#current_translation ⇒ Object
28 29 30 |
# File 'lib/simple_model_translations/translation_helper.rb', line 28 def current_translation @current_translation || find_translation_by_locale(current_locale_for_translation) || find_translation_by_locale(default_locale_for_translation) end |
#default_locale_for_translation ⇒ Object
24 25 26 |
# File 'lib/simple_model_translations/translation_helper.rb', line 24 def default_locale_for_translation I18n.default_locale end |
#find_or_build_current_translation ⇒ Object
32 33 34 |
# File 'lib/simple_model_translations/translation_helper.rb', line 32 def find_or_build_current_translation find_or_build_translation_by_locale(current_locale_for_translation) end |
#find_or_build_translation_by_locale(locale) ⇒ Object
12 13 14 |
# File 'lib/simple_model_translations/translation_helper.rb', line 12 def find_or_build_translation_by_locale(locale) find_translation_by_locale(locale) || build_translation_for_locale(locale) end |
#find_translation_by_locale(locale) ⇒ Object
8 9 10 |
# File 'lib/simple_model_translations/translation_helper.rb', line 8 def find_translation_by_locale(locale) translations.detect { |t| t.locale.to_sym == locale } end |
#force_translation_with_locale(locale) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/simple_model_translations/translation_helper.rb', line 36 def force_translation_with_locale(locale) translation = if translations.loaded? find_translation_by_locale(locale) else translations.find_by_locale(locale) end if translation @record.readonly! @current_translation = translation else raise ActiveRecord::RecordNotFound.new("Cannot use translation with locale '#{locale}' for object '#{@record}'") end end |