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

.all(options = {}) ⇒ Object



72
73
74
75
76
77
# File 'app/models/couch_i18n/translation.rb', line 72

def self.all(options = {})
  total_entries = database.view(all_documents(reduce: true))
  with_pagination_options options.merge(total_entries: total_entries) do |options|
    database.view(all_documents(options.merge(reduce: false, include_docs: true)))
  end
end

.deeper_keys_for_offset(offset) ⇒ Object



162
163
164
165
166
167
# File 'app/models/couch_i18n/translation.rb', line 162

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



95
96
97
98
99
100
# File 'app/models/couch_i18n/translation.rb', line 95

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

.find_all_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



137
138
139
140
141
142
# File 'app/models/couch_i18n/translation.rb', line 137

def self.find_all_by_value(part, options = {})
  total_entries = database.view(by_translation_value(key: part, reduce: true))
  with_pagination_options options.merge(total_entries: total_entries) do |options|
    database.view(by_translation_value(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



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

def self.find_all_untranslated_by_key_part(part, options = {})
  total_entries = database.view(untranslated_by_translation_key_part(key: part, reduce: true))
  with_pagination_options options.merge(total_entries: total_entries) do |options|
    database.view(untranslated_by_translation_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



123
124
125
126
127
128
# File 'app/models/couch_i18n/translation.rb', line 123

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

.find_by_translation_key(key) ⇒ Object



79
80
81
82
83
84
85
86
# File 'app/models/couch_i18n/translation.rb', line 79

def self.find_by_translation_key(key)
  begin
    find("t::#{key}")
  rescue SimplyStored::RecordNotFound
    CouchI18n.handle_missing_key(key)
    nil
  end
end

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



156
157
158
159
160
# File 'app/models/couch_i18n/translation.rb', line 156

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

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



53
54
55
56
57
# File 'app/models/couch_i18n/translation.rb', line 53

def self.get_translation_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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/couch_i18n/translation.rb', line 169

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



145
146
147
148
149
150
# File 'app/models/couch_i18n/translation.rb', line 145

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.merge(reduce: false, include_docs: true)))
  end
end

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



152
153
154
# File 'app/models/couch_i18n/translation.rb', line 152

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



64
65
66
67
68
69
70
# File 'app/models/couch_i18n/translation.rb', line 64

def self.with_offset(offset, options = {})
  key = "#{offset}.".."#{offset}.ZZZZZZZZZ"
  total_entries = database.view(all_documents(key: key, reduce: true))
  with_pagination_options options.merge(total_entries: total_entries) do |options|
    database.view(all_documents(options.merge(key: key, reduce: false, include_docs: true)))
  end
end

Instance Method Details

#keyObject



13
14
15
# File 'app/models/couch_i18n/translation.rb', line 13

def key
  translation_key
end

#translation_keyObject



9
10
11
# File 'app/models/couch_i18n/translation.rb', line 9

def translation_key
  id.present? ? id[3..-1] : nil
end

#translation_key=(val) ⇒ Object



59
60
61
# File 'app/models/couch_i18n/translation.rb', line 59

def translation_key=(val)
  self.id = "t::#{val}"
end