Module: Monglobal

Included in:
Post
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.5"

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



12
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
# File 'lib/monglobal.rb', line 12

def update_translation(locale, params)

  if locale == "#{I18n.default_locale}"
    #just update the main record
    self.update_attributes(params)
  else
    #or update the translations
    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
    attrs = params[locale]
    if attrs
      tr = translation_class.new(attrs)
      tr.locale = locale
      translations << tr
    end
    
    save
  end #if default locale
end