lit 1.1.0 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +47 -31
  3. data/app/assets/javascripts/lit/backend/localizations.js.coffee +13 -6
  4. data/app/assets/stylesheets/lit/application.css +16 -13
  5. data/app/assets/stylesheets/lit/lit_frontend.css +16 -15
  6. data/app/controllers/lit/api/v1/localizations_controller.rb +9 -17
  7. data/app/controllers/lit/cloud_translations_controller.rb +1 -1
  8. data/app/controllers/lit/concerns/request_info_store.rb +5 -14
  9. data/app/controllers/lit/concerns/request_keys_store.rb +5 -13
  10. data/app/controllers/lit/localization_keys_controller.rb +38 -38
  11. data/app/controllers/lit/request_info_store.rb +10 -0
  12. data/app/controllers/lit/request_keys_store.rb +10 -0
  13. data/app/helpers/lit/frontend_helper.rb +65 -65
  14. data/app/models/lit/base.rb +1 -1
  15. data/app/models/lit/incomming_localization.rb +13 -27
  16. data/app/models/lit/localization.rb +6 -14
  17. data/app/services/remote_interactor_service.rb +2 -1
  18. data/app/services/synchronize_source_service.rb +4 -8
  19. data/app/views/lit/localization_keys/_localizations_list.html.erb +18 -10
  20. data/app/views/lit/localization_keys/batch_touch.js.erb +1 -0
  21. data/app/views/lit/localizations/update.js.erb +1 -1
  22. data/config/routes.rb +1 -0
  23. data/lib/generators/lit/install/templates/{initializer.rb → initializer.erb} +6 -1
  24. data/lib/generators/lit/install_generator.rb +8 -10
  25. data/lib/lit/adapters/hash_storage.rb +2 -2
  26. data/lib/lit/adapters/redis_storage.rb +8 -11
  27. data/lib/lit/adapters.rb +2 -0
  28. data/lib/lit/cache.rb +23 -2
  29. data/lib/lit/cloud_translation/providers/google.rb +25 -7
  30. data/lib/lit/engine.rb +4 -6
  31. data/lib/lit/export.rb +5 -11
  32. data/lib/lit/i18n_backend.rb +24 -0
  33. data/lib/lit/import.rb +4 -4
  34. data/lib/lit/middleware.rb +29 -0
  35. data/lib/lit/rails.rb +1 -0
  36. data/lib/lit/railtie.rb +6 -7
  37. data/lib/lit/services/localization_keys_to_hash_service.rb +18 -15
  38. data/lib/lit/version.rb +15 -1
  39. data/lib/lit.rb +11 -11
  40. metadata +13 -22
@@ -1,83 +1,83 @@
1
- module Lit
2
- module FrontendHelper
3
- include ActionView::Helpers::TranslationHelper
1
+ module Lit::FrontendHelper
2
+ include ActionView::Helpers::TranslationHelper
4
3
 
5
- module TranslationKeyWrapper
6
- def translate(key, options = {})
7
- count = options[:count]
8
- options = options.with_indifferent_access
9
- key = scope_key_by_partial(key)
10
- key = pluralized_key(key, count) if count
4
+ module TranslationKeyWrapper
5
+ def translate(key, options = {})
6
+ count = options[:count]
7
+ options = options.with_indifferent_access
8
+ key = scope_key_by_partial(key)
9
+ key = pluralized_key(key, count) if count
11
10
 
12
- content = super(key, options.symbolize_keys)
13
- if !options[:skip_lit] && lit_authorized?
14
- content = get_translateable_span(key, content)
15
- end
16
- content
17
- end
18
-
19
- def pluralized_key(key, count)
20
- pluralizer = I18n.backend.send(:pluralizer, locale)
21
- return unless pluralizer.respond_to?(:call)
22
- last = count.zero? ? :zero : pluralizer.call(count)
23
- format '%<key>s.%<last>s', key: key, last: last
24
- end
25
-
26
- def t(key, options = {})
27
- translate(key, options)
28
- end
11
+ content = super(key, **options.symbolize_keys)
12
+ content = get_translateable_span(key, content) if !options[:skip_lit] && lit_authorized?
13
+ content
29
14
  end
30
- prepend Lit::FrontendHelper::TranslationKeyWrapper
31
15
 
32
- def javascript_lit_tag
33
- javascript_include_tag 'lit/lit_frontend'
16
+ def pluralized_key(key, count)
17
+ pluralizer = I18n.backend.send(:pluralizer, locale)
18
+ return unless pluralizer.respond_to?(:call)
19
+ last = count.zero? ? :zero : pluralizer.call(count)
20
+ format '%<key>s.%<last>s', key: key, last: last
34
21
  end
35
22
 
36
- def stylesheet_lit_tag
37
- stylesheet_link_tag 'lit/lit_frontend'
23
+ def t(key, options = {})
24
+ translate(key, options)
38
25
  end
26
+ end
27
+ prepend Lit::FrontendHelper::TranslationKeyWrapper
39
28
 
40
- def lit_frontend_assets
41
- return unless lit_authorized?
42
- meta = content_tag :meta,
43
- '',
44
- value: lit.find_localization_localization_keys_path,
45
- name: 'lit-url-base'
46
- safe_join [javascript_lit_tag, stylesheet_lit_tag, meta]
47
- end
29
+ def javascript_lit_tag
30
+ javascript_include_tag 'lit/lit_frontend'
31
+ end
32
+
33
+ def stylesheet_lit_tag
34
+ stylesheet_link_tag 'lit/lit_frontend'
35
+ end
36
+
37
+ def lit_frontend_assets
38
+ return unless lit_authorized?
48
39
 
49
- def lit_translations_info
50
- return if Thread.current[:lit_request_keys].nil?
51
- return unless lit_authorized?
52
- content_tag :div, class: 'lit-translations-info collapsed' do
53
- concat content_tag(:span, 'Show translations', class: 'lit-open-button')
54
- concat content_tag(:span, 'X', class: 'lit-close-button')
55
- concat translations_list_content_tag
56
- end
40
+ meta = content_tag :meta, '', value: lit.find_localization_localization_keys_path, name: 'lit-url-base'
41
+ safe_join [javascript_lit_tag, stylesheet_lit_tag, meta]
42
+ end
43
+
44
+ def lit_translations_info
45
+ return if Thread.current[:lit_request_keys].nil?
46
+ return unless lit_authorized?
47
+
48
+ content_tag :div, class: 'lit-translations-info collapsed' do
49
+ concat content_tag(:span, 'Show translations', class: 'lit-open-button')
50
+ concat content_tag(:span, 'X', class: 'lit-close-button')
51
+ concat translations_list_content_tag
57
52
  end
53
+ end
58
54
 
59
- def translations_list_content_tag
60
- content_tag :ul, class: 'lit-translations-list' do
61
- Lit.init.cache.request_keys.each do |k, v|
62
- concat(content_tag(:li) do
63
- concat content_tag(:code, "#{k}:")
64
- concat get_translateable_span(k, v, alternative_text: '[empty]')
65
- end)
55
+ def translations_list_content_tag
56
+ content_tag :ul, class: 'lit-translations-list' do
57
+ Lit
58
+ .init
59
+ .cache
60
+ .request_keys
61
+ .each do |k, v|
62
+ concat(
63
+ content_tag(:li) do
64
+ concat content_tag(:code, "#{k}:")
65
+ concat get_translateable_span(k, v, alternative_text: '[empty]')
66
+ end,
67
+ )
66
68
  end
67
- end
68
69
  end
70
+ end
69
71
 
70
- def lit_authorized?
71
- return false if Lit.authentication_verification.blank?
72
- send Lit.authentication_verification
73
- end
72
+ def lit_authorized?
73
+ return false if Lit.authentication_verification.blank?
74
+
75
+ send Lit.authentication_verification
76
+ end
74
77
 
75
- def get_translateable_span(key, localization, alternative_text: nil)
76
- content_tag :span,
77
- class: 'lit-key-generic',
78
- data: { key: key, locale: I18n.locale } do
79
- localization.blank? ? alternative_text : localization
80
- end
78
+ def get_translateable_span(key, localization, alternative_text: nil)
79
+ content_tag :span, class: 'lit-key-generic', data: { key: key, locale: I18n.locale } do
80
+ localization.blank? ? alternative_text : localization
81
81
  end
82
82
  end
83
83
  end
@@ -21,7 +21,7 @@ class Lit::Base < ActiveRecord::Base
21
21
 
22
22
  private
23
23
 
24
- def create_or_update(*args, &block)
24
+ def create_or_update(**kwargs, &block)
25
25
  @was_saved_with_insert = true if new_record?
26
26
  @was_saved_with_update = true if persisted?
27
27
 
@@ -36,39 +36,27 @@ module Lit
36
36
  def duplicated?(val)
37
37
  set_localization
38
38
  return false if localization_has_changed?
39
- translated_value =
40
- localization.read_attribute_before_type_cast('translated_value')
41
- if localization.is_changed? && !translated_value.nil?
42
- translated_value == val
43
- else
44
- localization.read_attribute_before_type_cast('default_value') == val
45
- end
39
+
40
+ localization.translation == val
46
41
  end
47
42
 
48
43
  private
49
44
 
50
45
  def set_localization
51
46
  return if locale.blank? || localization_key.blank?
52
- self.localization = localization_key.localizations
53
- .find_by(locale_id: locale_id)
47
+ self.localization = localization_key.localizations.find_by(locale_id: locale_id)
54
48
  end
55
49
 
56
50
  def localization_has_changed?
57
- localization.blank? ||
58
- localization.is_deleted != localization_key_is_deleted
51
+ localization.blank? || localization.is_deleted != localization_key_is_deleted
59
52
  end
60
53
 
61
54
  def update_existing_localization_data
62
- localization.update!(
63
- translated_value: translated_value,
64
- is_changed: true
65
- )
55
+ localization.update!(translated_value: translated_value, is_changed: true)
66
56
  end
67
57
 
68
58
  def update_existing_localization_key_data
69
- localization_key.update!(
70
- is_deleted: localization_key_is_deleted
71
- )
59
+ localization_key.update!(is_deleted: localization_key_is_deleted)
72
60
  end
73
61
 
74
62
  def assign_new_localization_data
@@ -83,25 +71,23 @@ module Lit
83
71
 
84
72
  def assign_new_localization_key
85
73
  self.localization_key =
86
- Lit::LocalizationKey.where(
87
- localization_key: localization_key_str,
88
- is_deleted: localization_key_is_deleted
89
- ).first_or_create!
74
+ Lit::LocalizationKey.where(localization_key: localization_key_str, is_deleted: localization_key_is_deleted)
75
+ .first_or_create!
90
76
  end
91
77
 
92
78
  def assign_new_localization
93
79
  self.localization =
94
- Lit::Localization.where(localization_key_id: localization_key.id)
95
- .where(locale_id: locale.id)
96
- .first_or_initialize
80
+ Lit::Localization
81
+ .where(localization_key_id: localization_key.id)
82
+ .where(locale_id: locale.id)
83
+ .first_or_initialize
97
84
  localization.translated_value = translated_value
98
85
  localization.is_changed = true
99
86
  localization.save!
100
87
  end
101
88
 
102
89
  def update_cache
103
- Lit.init.cache.update_cache localization.full_key,
104
- localization.translation
90
+ Lit.init.cache.update_cache localization.full_key, localization.translation
105
91
  end
106
92
  end
107
93
  end
@@ -6,19 +6,14 @@ module Lit
6
6
  ## SCOPES
7
7
  scope :changed, -> { where is_changed: true }
8
8
  scope :not_changed, -> { where is_changed: false }
9
+
9
10
  # @HACK: dirty, find a way to round date to full second
10
- scope :after, lambda { |dt|
11
- where('updated_at >= ?', dt + 1.second)
12
- .where(is_changed: true)
13
- }
14
- scope :active, lambda {
15
- joins(:localization_key)
16
- .where(Lit::LocalizationKey.table_name => { is_deleted: false })
17
- }
11
+ scope :after, lambda { |dt| where('updated_at >= ?', dt + 1.second).where(is_changed: true) }
12
+ scope :active, lambda { joins(:localization_key).where(Lit::LocalizationKey.table_name => { is_deleted: false }) }
18
13
 
19
14
  ## ASSOCIATIONS
20
- belongs_to :locale
21
- belongs_to :localization_key, touch: true
15
+ belongs_to :locale, required: true
16
+ belongs_to :localization_key, touch: true, required: true
22
17
  has_many :localization_versions, dependent: :destroy
23
18
  has_many :versions, class_name: '::Lit::LocalizationVersion'
24
19
 
@@ -31,9 +26,7 @@ module Lit
31
26
  ## ACCESSORS
32
27
  attr_accessor :full_key_str
33
28
 
34
- unless defined?(::ActionController::StrongParameters)
35
- attr_accessible :translated_value, :locale_id
36
- end
29
+ attr_accessible :translated_value, :locale_id unless defined?(::ActionController::StrongParameters)
37
30
 
38
31
  ## BEFORE & AFTER
39
32
  with_options if: :translated_value_changed? do |o|
@@ -94,6 +87,5 @@ module Lit
94
87
  translated_value = translated_value_was || default_value
95
88
  localization_versions.new(translated_value: translated_value)
96
89
  end
97
-
98
90
  end
99
91
  end
@@ -27,7 +27,7 @@ class RemoteInteractorService
27
27
 
28
28
  def initialize_request(uri)
29
29
  req = Net::HTTP::Get.new(uri.request_uri)
30
- req.add_field('Authorization', %(Token token="#{@source.api_key}"))
30
+ req.add_field('Authorization', "Token token=\"#{@source.api_key}\"")
31
31
  req
32
32
  end
33
33
 
@@ -41,6 +41,7 @@ class RemoteInteractorService
41
41
  def perform_request(connection, request)
42
42
  res = connection.start { |http| http.request(request) }
43
43
  return res unless res.is_a?(Net::HTTPSuccess)
44
+
44
45
  JSON.parse(res.body)
45
46
  end
46
47
  end
@@ -12,8 +12,7 @@ class SynchronizeSourceService
12
12
 
13
13
  def synchronize_localizations
14
14
  after_date = @source.last_updated_at&.to_s(:db)
15
- result = interactor.send_request Lit::Source::LOCALIZATIONS_PATH,
16
- after: after_date
15
+ result = interactor.send_request Lit::Source::LOCALIZATIONS_PATH, after: after_date
17
16
  return unless result&.is_a?(Array)
18
17
  result.each { |loc| synchronize_localization loc }
19
18
  end
@@ -28,19 +27,16 @@ class SynchronizeSourceService
28
27
  inc_loc.localization_key = find_localization_key(inc_loc)
29
28
  inc_loc.translated_value = loc['value']
30
29
  return if inc_loc.duplicated?(loc['value'])
30
+
31
31
  inc_loc.save!
32
32
  end
33
33
 
34
34
  def find_incomming_localization(localization)
35
- Lit::IncommingLocalization.find_or_initialize_by(
36
- incomming_id: localization['id']
37
- )
35
+ Lit::IncommingLocalization.find_or_initialize_by(incomming_id: localization['id'])
38
36
  end
39
37
 
40
38
  def find_localization_key(inc_loc)
41
- Lit::LocalizationKey.find_by(
42
- localization_key: inc_loc.localization_key_str
43
- )
39
+ Lit::LocalizationKey.find_by(localization_key: inc_loc.localization_key_str)
44
40
  end
45
41
 
46
42
  def localization_key_deleted?(loc)
@@ -1,8 +1,12 @@
1
- <table class="table">
1
+ <div class="col-12 text-right">
2
+ <%= link_to "batch touch", lit.batch_touch_localization_keys_path(key: params[:key], key_prefix: params[:key_prefix]), method: :post, remote: true, class: 'btn btn-sm btn-primary', data: { confirm: 'This will "touch" all search results making them subject of synchronization. Proceed?'} %>
3
+ </div>
4
+ <table class="table mt-1">
2
5
  <%- @localization_keys.each do |lk| %>
3
6
  <tr class="localization_key_row" data-id="<%= lk.id %>">
4
7
  <td>
5
8
  <strong><%= lk.localization_key %></strong>
9
+ <i class="fa fa-clipboard js-copy_to_clipboard copy_to_clipboard_btn " data-key="<%= lk.localization_key %>"></i>
6
10
  <span class="badge"><%= Lit.init.cache.get_global_hits_counter(lk.localization_key) %></span>
7
11
  <div class="localization_keys_options">
8
12
  <% if Lit.store_request_info %>
@@ -30,26 +34,30 @@
30
34
  <div class="detail_wrapper">
31
35
  <table class="table table-bordered table-striped">
32
36
  <tr>
33
- <th class="col-md-8">Translation</th>
34
- <th class="col-md-2 text-center">Locale</th>
37
+ <th class="col-md-1"></th>
38
+ <th class="col-md-9">Translation</th>
39
+ <th class="col-md-1 text-center">Locale</th>
35
40
  <% unless lk.is_deleted? %>
36
- <th class="col-md-2 text-center">Completed</th>
41
+ <th class="col-md-1 text-center">Completed</th>
37
42
  <% end %>
38
43
  </tr>
39
44
  <%- available_locales.each do |locale| %>
40
45
  <%- localization = localization_for(locale, lk) %>
41
46
  <tr>
47
+ <td>
48
+ <% if localization %>
49
+ <%= draw_icon 'clock-o', title: "Last updated at #{localization.updated_at.to_s(:db)}" %>
50
+ <%= link_to lit.previous_versions_localization_key_localization_path(lk, localization, format: :js), class: "js-show_prev_versions #{'hidden' unless versions?(localization)}", remote: true do %>
51
+ <%= draw_icon 'random', title: I18n.t('lit.common.previous_versions', default: 'Previous versions') %>
52
+ <% end %>
53
+ <% end %>
54
+ </td>
42
55
  <td class="localization_row" data-id="<%= localization.id%>" data-edit="<%= edit_localization_key_localization_path(lk, localization, format: :js) %>" data-editing=0 data-content="">
43
56
  <%= render partial: 'localization_row', locals: {localization: Lit.init.cache["#{locale}.#{lk.localization_key}"]} %>
44
57
  </td>
45
58
  <td class="locale_row text-center">
46
59
  <%= EmojiFlag.new(locale) %>
47
60
  <%= locale %>
48
- <% if localization %>
49
- <%= link_to lit.previous_versions_localization_key_localization_path(lk, localization, format: :js), class: "show_prev_versions #{'hidden' unless versions?(localization)}", remote: true do %>
50
- <%= draw_icon 'random', title: I18n.t('lit.common.previous_versions', default: 'Previous versions') %>
51
- <% end %>
52
- <% end %>
53
61
  </td>
54
62
  <% unless lk.is_deleted? %>
55
63
  <td class="text-center">
@@ -65,7 +73,7 @@
65
73
  <% end %>
66
74
  <% if Lit.store_request_info %>
67
75
  <tr class="hidden request_info_row">
68
- <td colspan="2">
76
+ <td colspan="3">
69
77
  <strong>Translation key recently displayed on following pages:</strong>
70
78
  <ul>
71
79
  <% Lit.init.cache.get_request_info(lk.localization_key).split(' ').reverse.each do |l| %>
@@ -0,0 +1 @@
1
+ alert('All of search results have been marked as updated now, please retry synchronizing now');
@@ -1,6 +1,6 @@
1
1
  var $row = $('td.localization_row[data-id="<%= @localization.id %>"]');
2
2
  $row.data('editing', 0);
3
3
  $row.html("<%= ejs render(:partial=>"/lit/localization_keys/localization_row", formats: ['html'], :locals=>{:localization=>@localization.translated_value }) %>");
4
- $row.siblings().find('.show_prev_versions').removeClass('hidden');
4
+ $row.siblings().find('.js-show_prev_versions').removeClass('hidden');
5
5
  $('a.change_completed_<%= @localization.id %> input[type=checkbox]').prop("checked", true);
6
6
 
data/config/routes.rb CHANGED
@@ -25,6 +25,7 @@ Lit::Engine.routes.draw do
25
25
  get :find_localization
26
26
  get :not_translated
27
27
  get :visited_again
28
+ post :batch_touch
28
29
  end
29
30
  resources :localizations, only: [:edit, :update, :show] do
30
31
  member do
@@ -3,7 +3,7 @@
3
3
  # `nil` will let everyone in.
4
4
  Lit.authentication_function = <%= @authentication_function || 'nil' %>
5
5
 
6
- # Which authentication verification function to use (ie. :user_signed_in)?
6
+ # Which authentication verification function to use (ie. :user_signed_in?)?
7
7
  # This is used together with frontend translation module to determine, if
8
8
  # helper button should be applied and translations wrapped in custom span
9
9
  Lit.authentication_verification = <%= @authentication_verification || 'nil' %>
@@ -53,6 +53,11 @@ Lit.set_last_updated_at_upon_creation = true
53
53
  # For more information please check the README.md
54
54
  Lit.store_request_info = false
55
55
 
56
+ # Store request keys - this will store all keys used to render a page. Useful
57
+ # only when combined with Lit::RequestKeysStore concern to view those keys in
58
+ # frontend
59
+ Lit.store_request_keys = false
60
+
56
61
  # Initialize lit
57
62
  Lit.init
58
63
 
@@ -11,24 +11,22 @@ module Lit
11
11
  desc 'Automates Lit installation'
12
12
 
13
13
  def set_authentication_function
14
- @authentication_function = options['authentication-function'].presence ||
15
- ask("What's the authentication function, ie. :authenticate_user! :").presence ||
16
- nil
14
+ @authentication_function =
15
+ options['authentication-function'].presence ||
16
+ ask("What's the authentication function, ie. :authenticate_user! :").presence || nil
17
17
  end
18
18
 
19
19
  def set_key_value_engine
20
- @key_value_engine = options['key-value-engine'].presence ||
21
- ask("What's the key value engine? ([hash] OR redis):").presence ||
22
- :hash
20
+ @key_value_engine =
21
+ options['key-value-engine'].presence || ask("What's the key value engine? ([hash] OR redis):").presence ||
22
+ :hash
23
23
  end
24
24
 
25
25
  def add_redis_dependency
26
26
  if @key_value_engine == 'redis'
27
27
  puts 'Adding redis dependency'
28
28
  gem 'redis'
29
- Bundler.with_clean_env do
30
- run 'bundle install'
31
- end
29
+ Bundler.with_clean_env { run 'bundle install' }
32
30
  end
33
31
  end
34
32
 
@@ -42,7 +40,7 @@ module Lit
42
40
  puts 'Skipping config/initializers/lit.rb creation, file already exists!'
43
41
  else
44
42
  puts 'Adding lit initializer (config/initializers/lit.rb)...'
45
- template 'initializer.rb', path
43
+ template 'initializer.erb', path
46
44
  end
47
45
  end
48
46
 
@@ -1,4 +1,4 @@
1
- module Lit
1
+ module Lit::Adapters
2
2
  class HashStorage < Hash
3
3
  def incr(key)
4
4
  self[key] ||= 0
@@ -25,7 +25,7 @@ module Lit
25
25
 
26
26
  cache_localizations = form_cache_localizations(keys_of_subtree)
27
27
 
28
- full_subtree = Lit::LocalizationKeysToHashService.call(cache_localizations)
28
+ full_subtree = Lit::Services::LocalizationKeysToHashService.call(cache_localizations)
29
29
  requested_part = full_subtree.dig(*key.split('.'))
30
30
  return nil if requested_part.blank?
31
31
  return requested_part if requested_part.is_a?(String)
@@ -13,7 +13,7 @@ module Lit
13
13
  Lit.redis_url || ENV[ENV['REDIS_PROVIDER'] || 'REDIS_URL']
14
14
  end
15
15
 
16
- class RedisStorage
16
+ class Adapters::RedisStorage
17
17
  def initialize
18
18
  Lit.redis
19
19
  end
@@ -22,14 +22,15 @@ module Lit
22
22
  def exists?(key)
23
23
  # Use recommended binary-returning method create [with this redis-rb commit](https://github.com/redis/redis-rb/commit/bf42fc9e0db4a1719d9b1ecc65aeb20425d44427).
24
24
  return Lit.redis.exists?(key) if Lit.redis.respond_to?(:exists?)
25
+
25
26
  # Fall back with older gem
26
27
  Lit.redis.exists(key)
27
28
  end
28
29
 
29
30
  def [](key)
30
- if self.exists?(_prefixed_key_for_array(key))
31
+ if exists?(_prefixed_key_for_array(key))
31
32
  Lit.redis.lrange(_prefixed_key(key), 0, -1)
32
- elsif self.exists?(_prefixed_key_for_nil(key))
33
+ elsif exists?(_prefixed_key_for_nil(key))
33
34
  nil
34
35
  else
35
36
  val = Lit.redis.get(_prefixed_key(key))
@@ -43,9 +44,7 @@ module Lit
43
44
  delete(k)
44
45
  if v.is_a?(Array)
45
46
  Lit.redis.set(_prefixed_key_for_array(k), '1')
46
- v.each do |ve|
47
- Lit.redis.rpush(_prefixed_key(k), ve.to_s)
48
- end
47
+ v.each { |ve| Lit.redis.rpush(_prefixed_key(k), ve.to_s) }
49
48
  elsif v.nil?
50
49
  Lit.redis.set(_prefixed_key_for_nil(k), '1')
51
50
  Lit.redis.set(_prefixed_key(k), '')
@@ -69,7 +68,7 @@ module Lit
69
68
  end
70
69
 
71
70
  def has_key?(key)
72
- self.exists?(_prefixed_key(key))
71
+ exists?(_prefixed_key(key))
73
72
  end
74
73
  alias key? has_key?
75
74
 
@@ -78,9 +77,7 @@ module Lit
78
77
  end
79
78
 
80
79
  def sort
81
- Lit.redis.keys.sort.map do |k|
82
- [k, self.[](k)]
83
- end
80
+ Lit.redis.keys.sort.map { |k| [k, self.[](k)] }
84
81
  end
85
82
 
86
83
  def prefix
@@ -118,7 +115,7 @@ module Lit
118
115
  values_of_subtree = Lit.redis.mget(keys_of_subtree)
119
116
  cache_localizations = form_cache_localizations(keys_of_subtree, values_of_subtree)
120
117
 
121
- full_subtree = Lit::LocalizationKeysToHashService.call(cache_localizations)
118
+ full_subtree = Lit::Services::LocalizationKeysToHashService.call(cache_localizations)
122
119
  requested_part = full_subtree.dig(*key.split('.'))
123
120
  return nil if requested_part.blank?
124
121
  return requested_part if requested_part.is_a?(String)
@@ -0,0 +1,2 @@
1
+ module Lit::Adapters
2
+ end
data/lib/lit/cache.rb CHANGED
@@ -18,6 +18,9 @@ end
18
18
 
19
19
  module Lit
20
20
  class Cache
21
+ @@last_update = nil
22
+ @@localization_cache = {}
23
+
21
24
  def initialize
22
25
  @hits_counter = Lit.get_key_value_engine
23
26
  @request_info_store = Lit.get_key_value_engine
@@ -32,6 +35,7 @@ module Lit
32
35
 
33
36
  def [](key)
34
37
  value = nil
38
+ check_and_clean_if_required
35
39
  unless localization_cache.key?(key)
36
40
  value = localizations[key]
37
41
  localization_cache[key] = value
@@ -81,6 +85,7 @@ module Lit
81
85
  locale = find_locale(locale_key)
82
86
  localization = find_localization(locale, key_without_locale, value: value, force_array: force_array, update_value: true)
83
87
  return localization.translation if startup_process && localization.is_changed?
88
+
84
89
  localizations[key] = localization.translation if localization
85
90
  localization_cache[key] = localizations[key]
86
91
  end
@@ -120,6 +125,7 @@ module Lit
120
125
  @localization_object_cache.delete(key)
121
126
  localization = find_localization(locale, key_without_locale, default_fallback: true)
122
127
  localizations[key] = localization.translation if localization
128
+ clear_localization_cache
123
129
  end
124
130
 
125
131
  def delete_key(key)
@@ -175,11 +181,26 @@ module Lit
175
181
  end
176
182
 
177
183
  def localization_cache
178
- Thread.current[:lit_thread_cache] ||= {}
184
+ # Thread.current[:lit_thread_cache] ||= {}
185
+ @@localization_cache ||= {}
179
186
  end
180
187
 
181
188
  def clear_localization_cache
182
- Thread.current[:lit_thread_cache] = {}
189
+ # Thread.current[:lit_thread_cache] = {}
190
+ @@localization_cache = {}
191
+ end
192
+
193
+ def check_and_clean_if_required
194
+ # we want this method to run only once during a request or background job
195
+ return if Thread.current[:localization_cache_valid]
196
+
197
+ # get most recently updated translation and compare with what we store in class var
198
+ last_up = Lit::Localization.order(updated_at: :desc).limit(1).pluck(:updated_at).first.to_i
199
+ if last_up != @@last_update
200
+ clear_localization_cache
201
+ @@last_update = last_up
202
+ end
203
+ Thread.current[:localization_cache_valid] = true
183
204
  end
184
205
 
185
206
  private