lit 0.4.0.pre.alpha.1 → 0.4.0.pre.alpha.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4df32a49da08696b7996f469dbd9041655750f22
4
- data.tar.gz: e642cef48888929647b2782be05bb96339eb8788
3
+ metadata.gz: 2593f6821f5b28b0ac65d0bd7b746fb117ceca9a
4
+ data.tar.gz: f5c0079a74026f528f1cab00f50e4cd52bd6d2ff
5
5
  SHA512:
6
- metadata.gz: c002db4e8d5262e15c4d62a55cbccb44a44c6580dd31a4d1feefdb3eec72b182894d6e72d8d13659cbf0e8e679bea203a1a8344818b841351590b40207e1b100
7
- data.tar.gz: 133089d93bc0f5da416480e4fe59a9f27a2779bbab8c859691d0e18644fb2e81fbf24e2341fa0e8c553cb9e056c7ea71afd50ea44b203d40cc533af7b2f7df56
6
+ metadata.gz: 0a916d4011baf37c64edc6f5f900b08eff704621f8ec32ea82b82fa005115341fdfcf8f5503d80714a36e0e1d417b52e51a239cc95259fbcc898754e3c323f1f
7
+ data.tar.gz: cbc1c7c864fc53dacf1909353cbfab6b087a1c7e9ef5a4dc417f491a911595f325796118c12cfe6dcede0bdeffe2c25b25853eb5fa7c48c3fbf836f21f56b0fe
@@ -15,7 +15,13 @@ module Lit
15
15
  end
16
16
 
17
17
  def update
18
- after_update_operations if @localization.update_attributes(clear_params)
18
+ # Wrapping it in a transaction leverages the fact that @localization's
19
+ # :after_commit callback is then executed AFTER whatever's done in
20
+ # #after_update_operations. So it'll first set :is_changed to true
21
+ # and then it will be properly read in the cache setting routine.
22
+ @localization.transaction do
23
+ after_update_operations if @localization.update_attributes(clear_params)
24
+ end
19
25
  respond_to do |f|
20
26
  f.js
21
27
  f.json do
@@ -0,0 +1,38 @@
1
+ class Lit::Base < ActiveRecord::Base
2
+ self.abstract_class = true
3
+
4
+ before_save :mark_for_retry_on_create, on: :create
5
+ before_save :mark_for_retry_on_update, on: :update
6
+
7
+ attr_accessor :retried_created, :retried_updated
8
+
9
+ def mark_for_retry_on_create
10
+ @will_retry_create = true
11
+ end
12
+
13
+ def mark_for_retry_on_update
14
+ @will_retry_update = true
15
+ end
16
+
17
+ after_rollback :retry_lit_model_save
18
+
19
+ private
20
+
21
+ def retry_lit_model_save
22
+ retry_on_create if instance_variable_defined?(:@will_retry_create) && @will_retry_create
23
+ retry_on_update if instance_variable_defined?(:@will_retry_update) && @will_retry_update
24
+ end
25
+
26
+ def retry_on_create
27
+ return if self.retried_created
28
+ self.retried_created = true
29
+ self.class.create! attributes.merge(retried_created: true)
30
+ end
31
+
32
+ def retry_on_update
33
+ return if self.retried_updated
34
+ self.retried_updated = true
35
+ update! attributes.merge(retried_updated: true)
36
+ end
37
+
38
+ end
@@ -1,5 +1,5 @@
1
1
  module Lit
2
- class Locale < ActiveRecord::Base
2
+ class Locale < Base
3
3
  ## SCOPES
4
4
  scope :ordered, -> { order(locale: :asc) }
5
5
  scope :visible, -> { where(is_hidden: false) }
@@ -1,5 +1,5 @@
1
1
  module Lit
2
- class Localization < ActiveRecord::Base
2
+ class Localization < Lit::Base
3
3
  serialize :translated_value
4
4
  serialize :default_value
5
5
 
@@ -26,9 +26,11 @@ module Lit
26
26
  delegate :is_deleted, to: :localization_key
27
27
 
28
28
  ## VALIDATIONS
29
- validates :locale, presence: true
29
+ validates :locale, :localization_key, presence: true
30
30
 
31
31
  ## ACCESSORS
32
+ attr_accessor :full_key_str
33
+
32
34
  unless defined?(::ActionController::StrongParameters)
33
35
  attr_accessible :translated_value, :locale_id
34
36
  end
@@ -44,7 +46,7 @@ module Lit
44
46
  end
45
47
 
46
48
  def full_key
47
- [locale.locale, localization_key.localization_key].join('.')
49
+ full_key_str || [locale.locale, localization_key.localization_key].join('.')
48
50
  end
49
51
 
50
52
  def translation
@@ -74,7 +76,7 @@ module Lit
74
76
  def update_default_value(value)
75
77
  return true if persisted? && default_value == value
76
78
  if persisted?
77
- update_column(:default_value, value)
79
+ update(default_value: value)
78
80
  else
79
81
  self.default_value = value
80
82
  save!
@@ -92,5 +94,6 @@ module Lit
92
94
  translated_value = translated_value_was || default_value
93
95
  localization_versions.new(translated_value: translated_value)
94
96
  end
97
+
95
98
  end
96
99
  end
@@ -1,5 +1,5 @@
1
1
  module Lit
2
- class LocalizationKey < ActiveRecord::Base
2
+ class LocalizationKey < Lit::Base
3
3
  attr_accessor :interpolated_key
4
4
 
5
5
  ## SCOPES
@@ -31,6 +31,7 @@ module Lit
31
31
 
32
32
  ## BEFORE AND AFTER
33
33
  after_commit :check_completed, on: :update
34
+ after_commit :remove_from_cache, on: :destroy
34
35
 
35
36
  def to_s
36
37
  localization_key
@@ -75,9 +76,13 @@ module Lit
75
76
  ActiveRecord::Base.transaction do
76
77
  update is_deleted: true
77
78
  change_all_completed
78
- I18n.backend.available_locales.each do |l|
79
- Lit.init.cache.delete_key "#{l}.#{localization_key}"
80
- end
79
+ remove_from_cache
80
+ end
81
+ end
82
+
83
+ def remove_from_cache
84
+ Lit::Locale.pluck(:locale).each do |l|
85
+ Lit.init.cache.delete_key "#{l}.#{localization_key}"
81
86
  end
82
87
  end
83
88
 
@@ -94,5 +99,9 @@ module Lit
94
99
  self.is_completed = localizations.changed.count == localizations.count
95
100
  save! if is_completed_changed?
96
101
  end
102
+
103
+ def lit_attribute_will_change
104
+ localization_key_will_change!
105
+ end
97
106
  end
98
107
  end
@@ -23,6 +23,8 @@ module Lit
23
23
  @request_info_store = Lit.get_key_value_engine
24
24
  @hits_counter_working = true
25
25
  @keys = nil
26
+ @localization_object_cache = {}
27
+ @localization_key_object_cache = {}
26
28
  end
27
29
 
28
30
  def [](key)
@@ -79,6 +81,8 @@ module Lit
79
81
  locale_key, key_without_locale = split_key(key)
80
82
  locale = find_locale(locale_key)
81
83
  delete_localization(locale, key_without_locale)
84
+ @localization_key_object_cache = {}
85
+ @localization_object_cache = {}
82
86
  end
83
87
 
84
88
  def load_all_translations
@@ -96,6 +100,7 @@ module Lit
96
100
  key = key.to_s
97
101
  locale_key, key_without_locale = split_key(key)
98
102
  locale = find_locale(locale_key)
103
+ @localization_object_cache.delete(key)
99
104
  localization = find_localization(locale, key_without_locale, default_fallback: true)
100
105
  localizations[key] = localization.translation if localization
101
106
  end
@@ -105,11 +110,15 @@ module Lit
105
110
  localizations.delete(key)
106
111
  key_without_locale = split_key(key).last
107
112
  localization_keys.delete(key_without_locale)
113
+ @localization_object_cache.delete(key)
114
+ @localization_key_object_cache.delete(key)
108
115
  I18n.backend.reload!
109
116
  end
110
117
 
111
118
  def reset
112
119
  @locale_cache = {}
120
+ @localization_key_object_cache = {}
121
+ @localization_object_cache = {}
113
122
  localizations.clear
114
123
  localization_keys.clear
115
124
  load_all_translations
@@ -154,9 +163,11 @@ module Lit
154
163
 
155
164
  def find_localization(locale, key_without_locale, value: nil, force_array: false, update_value: false, default_fallback: false)
156
165
  return nil if value.is_a?(Hash)
166
+ full_key = "#{locale}.#{key_without_locale}"
157
167
  ActiveRecord::Base.transaction do
158
168
  localization_key = find_localization_key(key_without_locale)
159
- localization =
169
+ localization = @localization_object_cache[full_key]
170
+ localization ||=
160
171
  Lit::Localization.active
161
172
  .where(locale_id: locale.id)
162
173
  .where(localization_key_id: localization_key.id)
@@ -176,11 +187,14 @@ module Lit
176
187
  # Prevent overwriting existing default value with nil.
177
188
  # However, if the localization record is #new_record?, we still need
178
189
  # to insert it with an empty default value.
190
+ localization.locale = locale
191
+ localization.localization_key = localization_key
192
+ localization.full_key_str = full_key
179
193
  localization.update_default_value(value) if localization.new_record? || value
194
+ @localization_object_cache[full_key] = localization
180
195
  end
181
- return localization
196
+ localization
182
197
  end
183
- nil
184
198
  end
185
199
 
186
200
  # fallback to translation in different locale
@@ -229,7 +243,8 @@ module Lit
229
243
  new_value = nil
230
244
  case v
231
245
  when Symbol then
232
- lk = Lit::LocalizationKey.where(localization_key: v.to_s).first
246
+ lk = @localization_key_object_cache[v.to_s] || \
247
+ Lit::LocalizationKey.where(localization_key: v.to_s).first
233
248
  if lk
234
249
  loca = Lit::Localization.active.where(locale_id: locale.id).
235
250
  where(localization_key_id: lk.id).first
@@ -258,16 +273,19 @@ module Lit
258
273
  end
259
274
 
260
275
  def find_localization_key(key_without_locale)
261
- if localization_keys.key?(key_without_locale)
262
- Lit::LocalizationKey.find_by(
263
- id: localization_keys[key_without_locale]
264
- ) || find_or_create_localization_key(key_without_locale)
265
- else
266
- find_or_create_localization_key(key_without_locale)
267
- end
276
+ return @localization_key_object_cache[key_without_locale] if @localization_key_object_cache.key?(key_without_locale)
277
+ @localization_key_object_cache[key_without_locale] = if localization_keys.key?(key_without_locale)
278
+ Lit::LocalizationKey.find_by(
279
+ id: localization_keys[key_without_locale]
280
+ ) || find_or_create_localization_key(key_without_locale)
281
+ else
282
+ find_or_create_localization_key(key_without_locale)
283
+ end
284
+ @localization_key_object_cache[key_without_locale]
268
285
  end
269
286
 
270
287
  def find_localization_key_for_delete(key_without_locale)
288
+ return @localization_key_object_cache[key_without_locale] if @localization_key_object_cache.key?(key_without_locale)
271
289
  lk = Lit::LocalizationKey.find_by(id: localization_keys[key_without_locale]) if localization_keys.has_key?(key_without_locale)
272
290
  lk || Lit::LocalizationKey.where(localization_key: key_without_locale).first
273
291
  end
@@ -69,11 +69,9 @@ module Lit
69
69
 
70
70
  parts = I18n.normalize_keys(locale, key, scope, options[:separator])
71
71
  key_with_locale = parts.join('.')
72
-
73
72
  # check in cache or in simple backend
74
73
  content = @cache[key_with_locale] || super
75
74
  return content if parts.size <= 1
76
-
77
75
  if content.nil? && should_cache?(key_with_locale, options)
78
76
  new_content = @cache.init_key_with_value(key_with_locale, content)
79
77
  content = new_content if content.nil? # Content can change when Lit.humanize is true for example
@@ -125,8 +123,8 @@ module Lit
125
123
  key = ([locale] + scope).join('.')
126
124
  if data.respond_to?(:to_hash)
127
125
  # ActiveRecord::Base.transaction do
128
- data.to_hash.each do |key, value|
129
- store_item(locale, value, scope + [key], startup_process)
126
+ data.to_hash.each do |k, value|
127
+ store_item(locale, value, scope + [k], startup_process)
130
128
  end
131
129
  # end
132
130
  elsif data.respond_to?(:to_str) || data.is_a?(Array)
@@ -1,3 +1,3 @@
1
1
  module Lit
2
- VERSION = '0.4.0-alpha.1'.freeze
2
+ VERSION = '0.4.0-alpha.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.pre.alpha.1
4
+ version: 0.4.0.pre.alpha.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Litwiniuk
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-11-12 00:00:00.000000000 Z
13
+ date: 2018-11-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: i18n
@@ -390,6 +390,7 @@ files:
390
390
  - app/helpers/lit/localizations_helper.rb
391
391
  - app/helpers/lit/sources_helper.rb
392
392
  - app/jobs/lit/synchronize_source_job.rb
393
+ - app/models/lit/base.rb
393
394
  - app/models/lit/incomming_localization.rb
394
395
  - app/models/lit/locale.rb
395
396
  - app/models/lit/localization.rb