Module: Mongoid::Translate::ClassMethods

Defined in:
lib/mongoid/translate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#translated_fieldsObject

Returns the value of attribute translated_fields.



49
50
51
# File 'lib/mongoid/translate.rb', line 49

def translated_fields
  @translated_fields
end

Instance Method Details

#translate(*argv) ⇒ Object

creates accessor methods



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mongoid/translate.rb', line 53

def translate(*argv)
  self.translated_fields = *argv

  translated_fields.each do |field|
    # Return field for current locale.
    #
    # Otherwise, fallbacks to main_language.
    #
    # @return [ Object ]
    #
    define_method field do
      locale = languages.include?(I18n.locale) ? I18n.locale : main_language
      translations.where(language: locale).one.try(field)
    end

    # Setter on field for current locale.
    #
    # @return [ Object ]
    #
    define_method "#{field}=" do |arg|
      translations.where(language: I18n.locale).one.try("#{field}=", arg)
    end
  end
end