Module: Monglobal

Defined in:
lib/monglobal.rb,
lib/monglobal/version.rb,
lib/generators/monglobal/setup_generator.rb

Defined Under Namespace

Modules: ClassMethods Classes: SetupGenerator

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

allows for :translates method on model in rails



8
9
10
# File 'lib/monglobal.rb', line 8

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#update_translation(locale, params) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/monglobal.rb', line 13

def update_translation(locale, params)

  if locale == "#{I18n.default_locale}"
    puts "I AM DEFAULT"
    puts self.title
    self.update_attributes(params)
  else
    puts "I AM TRANSLATED"
    params = {locale => params}

    translations = send("#{self.class.to_s}Translation".underscore.pluralize.to_sym)
    translation_class = Kernel.const_get("#{self.class.to_s}Translation")

    # update existing translations
    translations.each do |translation|
      translation.update_attributes(params.delete(translation.locale))
    end
    
    # create new translations
    (LANGS - [I18n.default_locale]).each do |lang|
      attrs = params[lang.to_s]
      if attrs
        tr = translation_class.new(attrs)
        tr.locale = lang.to_s
        translations << tr
      end
    end
    
    save
  end #if default locale
end