Class: Idioma::Phrase

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/idioma/phrase.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.duplicate_for_locales(base_locale, locales) ⇒ Object

Given a base locale, duplicate all phrases to a set of other locales

Parameters:

  • The (Symbol)

    base locale

  • An (Array)

    array of other locales



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/idioma/phrase.rb', line 33

def self.duplicate_for_locales(base_locale, locales)
  locales = [locales] unless locales.class == Array
  where(locale: base_locale).find_each do |base_phrase|
    locales.each do |locale|
      unless where(locale: locale, i18n_key: base_phrase.i18n_key).exists?
        phrase = Phrase.create!({
          locale: locale,
          i18n_key: base_phrase.i18n_key
        })
      end
    end
  end
end

.prime_backendObject

Take what’s in the database and prime the i18n backend store (Redis)



48
49
50
51
52
# File 'app/models/idioma/phrase.rb', line 48

def self.prime_backend
  find_each do |phrase|
    phrase.update_backend
  end
end

Instance Method Details

#save_and_update_backend(params = {}) ⇒ Boolean

Save and push the changes to the i18n backend

Parameters:

  • The (Hash)

    Phrase attributes to save

Returns:

  • (Boolean)

    Whether the save was successful



74
75
76
77
78
79
80
# File 'app/models/idioma/phrase.rb', line 74

def save_and_update_backend(params = {})
  result = self.save(params)
  if result
    self.update_backend
  end
  result
end

#translated?Boolean

Is this phrase translated?

Returns:

  • (Boolean)


95
96
97
# File 'app/models/idioma/phrase.rb', line 95

def translated?
  translated_at.present?
end

#untranslated?Boolean

Is this phrase untranslated?

Returns:

  • (Boolean)


101
102
103
# File 'app/models/idioma/phrase.rb', line 101

def untranslated?
  !translated?
end

#update_and_update_backend(params = {}) ⇒ Boolean

Update the record and then push those changes to the i18n backend

Parameters:

  • The (Hash)

    Phrase attributes to update

Returns:

  • (Boolean)

    Whether the update was successful



60
61
62
63
64
65
66
67
68
# File 'app/models/idioma/phrase.rb', line 60

def update_and_update_backend(params = {})
  self.translated_at = Time.zone.now if self.untranslated?

  result = self.update(params)
  if result
    self.update_backend
  end
  result
end

#update_backendObject

Will update the i18n backend if it has been configured



83
84
85
86
87
88
89
90
91
# File 'app/models/idioma/phrase.rb', line 83

def update_backend
  if Idioma.configuration.redis_backend
    if i18n_value.present?
      Idioma::RedisBackend.update_phrase(self)
    else
      Idioma::RedisBackend.delete_phrase(self)
    end
  end
end