Class: CouchI18n::Translation

Inherits:
Object
  • Object
show all
Includes:
SimplyStored::Couch
Defined in:
app/models/couch_i18n/translation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deeper_keys_for_offset(offset) ⇒ Object



116
117
118
119
120
121
# File 'app/models/couch_i18n/translation.rb', line 116

def self.deeper_keys_for_offset( offset )
  return get_keys_by_level(0).map{|dl| {:name => dl, :offset => dl}} unless offset.present?
  levels = offset.split('.')
  get_keys_by_level(levels.size, :startkey => levels, :endkey => levels + [{}]).
        map{|dl| {:name => dl, :offset => [offset, dl].join('.')}}
end

.find_all_by_key_part(part, options = {}) ⇒ Object

Find all records having the term part in their key

nl.action.one
en.action.two
en.activemodel.plural.models.user

and using

find_all_by_part('action')

will return the first two since they have action as key part



62
63
64
65
66
67
# File 'app/models/couch_i18n/translation.rb', line 62

def self.find_all_by_key_part(part, options = {})
  total_entries = database.view(by_key_part(key: part, reduce: true))
  with_pagination_options options.merge(total_entries: total_entries) do |options|
    database.view(by_key_part(options.merge(key: part, reduce: false, include_docs: true)))
  end
end

.find_all_untranslated_by_key_part(part, options = {}) ⇒ Object

Find all untranslated records having the term part in their key

nl.action.one
en.action.two
en.activemodel.plural.models.user

and using

find_all_by_part('action')

will return the first two since they have action as key part



76
77
78
79
80
81
# File 'app/models/couch_i18n/translation.rb', line 76

def self.find_all_untranslated_by_key_part(part, options = {})
  total_entries = database.view(by_key_part(key: part, reduce: true))
  with_pagination_options options.merge(total_entries: total_entries) do |options|
    database.view(untranslated_by_key_part(options.merge(key: part, reduce: false, include_docs: true)))
  end
end

.find_all_untranslated_by_value(part, options = {}) ⇒ Object

Find all untranslated records having the term part as value

nl.action.one: 'Value', translated: true
en.action.two: 'Value', translated: false
en.activemodel.plural.models.user: 'Other Value', translated: false

and using

find_all_untranslated_by_value('Value')

will return en.action.two



90
91
92
93
94
95
# File 'app/models/couch_i18n/translation.rb', line 90

def self.find_all_untranslated_by_value(part, options = {})
  total_entries = database.view(untranslated_by_value(key: part, reduce: true))
  with_pagination_options options.merge(total_entries: total_entries) do |options|
    database.view(untranslated_by_value(options.merge(key: part, reduce: false, include_docs: true)))
  end
end

.get_keys_by_level(level = 0, options = {}) ⇒ Object



44
45
46
47
48
# File 'app/models/couch_i18n/translation.rb', line 44

def self.get_keys_by_level(level = 0, options = {})
  data = database.view(with_key_array(options.merge(:group_level => level.succ)))["rows"]
  # data = data.select{|h| h["key"].size > level } # Only select ones that have a deeper nesting
  data.map{|h| h['key'][level].try(:to_sym)}.compact
end

.higher_keys_for_offset(offset) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/couch_i18n/translation.rb', line 123

def self.higher_keys_for_offset( offset )
  return [] unless offset.present?
  higher_keys = []
  levels = offset.split('.')
  return [] unless levels.size > 1
  # Add higher levels. Do not add the last level, since it is the current one => 0..-2
  levels[0..-2].each_with_index do |level_name, i|
    higher_keys<< {
      :name => level_name,
      :offset => levels[0..i].join('.')
    }
  end
  higher_keys
end

.untranslated(options = {}) ⇒ Object



98
99
100
101
102
103
# File 'app/models/couch_i18n/translation.rb', line 98

def self.untranslated(options = {})
  total_entries = database.view(untranslated_view(options.slice(:key, :keys, :startkey, :endkey).merge(reduce: true)))
  with_pagination_options options.merge(total_entries: total_entries) do |options|
    database.view(untranslated_view(options))
  end
end

.untranslated_with_offset(offset, options = {}) ⇒ Object



105
106
107
# File 'app/models/couch_i18n/translation.rb', line 105

def self.untranslated_with_offset(offset, options = {})
  CouchI18n::Translation.untranslated(options.merge(key: "#{offset}.".."#{offset}.ZZZZZZZZZ"))
end

.with_offset(offset, options = {}) ⇒ Object

Shorthand for selecting all stored with a given offset



51
52
53
# File 'app/models/couch_i18n/translation.rb', line 51

def self.with_offset(offset, options = {})
  CouchI18n::Translation.find_all_by_key("#{offset}.".."#{offset}.ZZZZZZZZZ", options)
end

Instance Method Details

#reload_i18nObject

Expire I18n when record is update



110
111
112
113
114
# File 'app/models/couch_i18n/translation.rb', line 110

def reload_i18n
  Rails.cache.write("couch_i18n-#{key}", value)
  #I18n.reload!
  #I18n.cache_store.clear if I18n.respond_to?(:cache_store) && I18n.cache_store.respond_to?(:clear)
end