Class: CouchI18n::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_i18n/store.rb

Constant Summary collapse

KNOWN_DEFAULTS =
{
  'support.array' => %({"words_connector":", ","two_words_connector":" and ","last_word_connector":", and "})
}

Instance Method Summary collapse

Instance Method Details

#[](key, options = {}) ⇒ Object

alias for read



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/couch_i18n/store.rb', line 22

def [](key, options = {})
  key = key.to_s.gsub('/', '.')
  key_without_locale = key.sub(/\w+\./, '')
  old_database_name = get_couchrest_name
  Rails.cache.fetch("couch_i18n-#{key}") do
    begin
      set_couchrest_name CouchPotato::Config.database_name # Set database to original configured name
      translation = CouchI18n::Translation.find_by_translation_key(key.to_s)
      translation ||= CouchI18n::Translation.create(translation_key: key, translation_value: options[:default].presence || KNOWN_DEFAULTS[key_without_locale].presence || key[/\w+$/].humanize, translated: false)
    #rescue SimplyStored::RecordNotFound
    #  binding.pry
    ensure
      set_couchrest_name old_database_name
    end
    translation.try(:translation_value)
  end
end

#[]=(key, value, options = {}) ⇒ Object

Now the store features



12
13
14
15
16
17
18
19
# File 'lib/couch_i18n/store.rb', line 12

def []=(key, value, options = {})
  key = key.to_s.gsub('/', '.')
  existing = CouchI18n::Translation.find_by_translation_key(key) rescue nil
  translation = existing || CouchI18n::Translation.new(translation_key: key)
  translation.translation_value = value
  translation.save and Rails.cache.write("couch_i18n-#{key}", value)
  value
end

#available_localesObject



7
8
9
# File 'lib/couch_i18n/store.rb', line 7

def available_locales
  CouchI18n::Translation.get_keys_by_level(0)
end

#get_couchrest_nameObject



52
53
54
# File 'lib/couch_i18n/store.rb', line 52

def get_couchrest_name
  CouchPotato.database.couchrest_database.name
end

#keysObject

DISABLE THIS FUNCTIONALITY, IT KILLS PERFORMANCE



57
58
59
# File 'lib/couch_i18n/store.rb', line 57

def keys
  [] #@kyes ||= CouchI18n::Translation.all.map(&:translation_key)
end

#set_couchrest_name(full_database_namename) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/couch_i18n/store.rb', line 40

def set_couchrest_name(full_database_namename)
  # we assume the database server is the same, just change the name
  name = full_database_namename[/\w+$/]
  d = CouchPotato.database.couchrest_database
  d.instance_variable_set('@name', name)
  #d.instance_variable_set('@uri', "/#{name.gsub('/', '%2F')}")
  d.instance_variable_set('@path', "/#{CGI.escape(name)}")
  d.instance_variable_set('@bulk_save_cache', [])
  #d.instance_variable_set('@root', d.host + d.uri)
  #CouchPotato.class_variable_set('@@__couchrest_database', CouchRest.database(CouchPotato.full_url_to_database(name, CouchPotato::Config.database_host)))
end