Module: ActiveRecordTranslatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/version.rb,
lib/activerecord_translatable.rb,
lib/activerecord_translatable/extension.rb

Defined Under Namespace

Modules: ClassMethods Classes: Railtie

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/activerecord_translatable/extension.rb', line 61

def method_missing(method_name, *arguments, &block)
  translatable.each do |attribute|
    attribute = attribute.to_s
    if method_name.to_s =~ /^#{attribute}_(.{2})=$/
      return set_translation(attribute, arguments.first, $1.to_sym)
    elsif method_name.to_s =~ /^#{attribute}=$/
      return set_translation(attribute, arguments.first)
    elsif method_name.to_s =~ /^#{attribute}_(.{2})$/
      return translation(attribute, $1.to_sym)
    elsif method_name.to_s =~ /^#{attribute}$/
      return translation(attribute)
    end
  end
  super
end

Instance Attribute Details

#translationsObject

Returns the value of attribute translations.



4
5
6
# File 'lib/activerecord_translatable/extension.rb', line 4

def translations
  @translations
end

Instance Method Details

#_get_stored_translation(attribute, locale) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/activerecord_translatable/extension.rb', line 36

def _get_stored_translation(attribute, locale)
  begin
    translation = I18n.t("#{base_name}.#{attribute}-#{self.id}", locale: locale, raise: true)
    setup_locale(locale)
    translations[locale][attribute] = translation
  rescue I18n::MissingTranslationData
    nil
  end
end

#available_localesObject



10
11
12
# File 'lib/activerecord_translatable/extension.rb', line 10

def available_locales
  self.locales.map { |locale| locale.to_sym }
end

#base_nameObject



24
25
26
# File 'lib/activerecord_translatable/extension.rb', line 24

def base_name
  self.class.name.downcase
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/activerecord_translatable/extension.rb', line 77

def respond_to_missing?(method_name, include_private = false)
  translatable.each do |attribute|
    attribute = attribute.to_s
    if method_name.to_s =~ /^#{attribute}_(.{2})=$/
      return true
    elsif method_name.to_s =~ /^#{attribute}=$/
      return true
    elsif method_name.to_s =~ /^#{attribute}_(.{2})$/
      return true
    elsif method_name.to_s =~ /^#{attribute}$/
      return true
    end
  end
  false
end

#set_translation(attribute, value, locale = I18n.locale) ⇒ Object



46
47
48
49
# File 'lib/activerecord_translatable/extension.rb', line 46

def set_translation(attribute, value, locale = I18n.locale)
  setup_locale(locale)
  translations[locale][attribute] = value
end

#setup_locale(locale) ⇒ Object



14
15
16
17
18
# File 'lib/activerecord_translatable/extension.rb', line 14

def setup_locale(locale)
  locales = self.locales || []
  locales << locale.to_s
  self.locales = locales.uniq
end

#translatableObject



6
7
8
# File 'lib/activerecord_translatable/extension.rb', line 6

def translatable
  self._translatable[base_name]
end

#translation(attribute, locale = I18n.locale) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/activerecord_translatable/extension.rb', line 28

def translation(attribute, locale = I18n.locale)
  begin
    translation = translations.fetch(locale).fetch(attribute)
  rescue KeyError
    _get_stored_translation(attribute, locale)
  end
end

#write_translationsObject



51
52
53
54
55
56
57
58
59
# File 'lib/activerecord_translatable/extension.rb', line 51

def write_translations
  return if translations.empty? # there are no translations to be saved

  translations.each do |locale, trans|
    trans.each do |attribute, value|
      I18n.backend.store_translations(locale, { "#{base_name}.#{attribute}-#{self.id}" => value }, escape: false)
    end
  end
end