thermos 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: caef41475321f2d1d3286a253267f0138396addb93dda96774f3ce29502e42d4
4
- data.tar.gz: 0b347ad89c7cbd0082c801537270d5efd140db676131985f0c65625d27f3669f
3
+ metadata.gz: 69d08a67a8ad98400b2b0eda1e63af9b22cb127e82d72117c6dbb8fecb71a808
4
+ data.tar.gz: 8a8369af0507aae7ef1c81ddf0bc7833adea0bd5941708c183c8c011b3e7db22
5
5
  SHA512:
6
- metadata.gz: b2fcf8764757cbc90257a1b2f1a2c96aa2d39b117702b399ac9d44527e62a9bf14cf394344ffe3300d3513d88b0a9a18063696d299e282794edbcadf5d2bac9d
7
- data.tar.gz: 2c20039b4fe427402c3ab8dbfacc83266530c0d63e169055f574ae6a433d69a772a9b4d8aa097988bced9ebb6f0c5c532963f7b0ef85f57003dbc7eb1c5e6bd7
6
+ metadata.gz: 0c9f06c52aa92a34a3beacd1384b8f4de92318484a536d657a1a8c3b5a5017ec09d42f6b99b95966b8e3572fdaf57d8c01dd2a0c8ec6b9eb99490ac413e1ca88
7
+ data.tar.gz: 3215b079c3890c74045ff902bcf259dc83d1d40e4ff5ee0c69be0833a959aa4e25dd7e92fbe9738fd18352223a6c050810d2065a9ab4bf25d45e0d87003d2889
@@ -19,9 +19,15 @@ module Thermos
19
19
  @deps.select do |dep|
20
20
  dep.klass == dep_model.class
21
21
  end.flat_map do |dep|
22
+ lookup_keys = []
23
+
22
24
  @model.joins(dep.path)
23
25
  .where(dep.table => { id: dep_model.id })
24
- .pluck(@lookup_key)
26
+ .find_each do |model|
27
+ lookup_keys << model.send(@lookup_key) if should_fill?(model)
28
+ end
29
+
30
+ lookup_keys
25
31
  end.uniq
26
32
  end
27
33
 
@@ -17,10 +17,8 @@ module Thermos
17
17
 
18
18
  def refill_dependency_caches(model)
19
19
  BeverageStorage.instance.beverages.each do |beverage|
20
- if beverage.should_fill?(model)
21
- beverage.lookup_keys_for_dep_model(model).each do |lookup_key|
22
- Thermos::RebuildCacheJob.perform_later(beverage.key, lookup_key)
23
- end
20
+ beverage.lookup_keys_for_dep_model(model).each do |lookup_key|
21
+ Thermos::RebuildCacheJob.perform_later(beverage.key, lookup_key)
24
22
  end
25
23
  end
26
24
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Thermos
4
- VERSION = '0.5.1'
4
+ VERSION = '0.5.2'
5
5
  end
@@ -3,6 +3,10 @@ class Category < ActiveRecord::Base
3
3
  has_many :products, through: :category_items
4
4
  belongs_to :store
5
5
 
6
+ def ball?
7
+ name.match("ball")
8
+ end
9
+
6
10
  def as_json(*args)
7
11
  {
8
12
  name: name,
@@ -1,2 +1,4 @@
1
1
  sports:
2
2
  name: sports store
3
+ supermarket:
4
+ name: supermarket store
data/test/thermos_test.rb CHANGED
@@ -161,6 +161,28 @@ class ThermosTest < ActiveSupport::TestCase
161
161
  assert_raises(MockExpectationError) { mock.verify }
162
162
  end
163
163
 
164
+ test "allows filtering based on the beverage when multiple beverages are configured and only one of them has a filter" do
165
+ mock = Minitest::Mock.new
166
+ store = stores(:supermarket)
167
+ category = categories(:baseball)
168
+ # filter method specific to one model
169
+ # store.ball? doesn't exist
170
+ filter = ->(model) { model.ball? }
171
+
172
+ Thermos.fill(key: "key", model: Category, lookup_key: "name", filter: filter) do |name|
173
+ mock.call(name)
174
+ end
175
+
176
+ Thermos.fill(key: "key_2", model: Store, lookup_key: "name") do |name|
177
+ mock.call(name)
178
+ end
179
+
180
+ mock.expect(:call, 1, ["groceries"])
181
+ store.update!(name: "groceries")
182
+ assert_equal 1, Thermos.drink(key: "key_2", id: "groceries")
183
+ mock.verify
184
+ end
185
+
164
186
  # has_many model changes
165
187
  test "rebuilds the cache on has_many model change" do
166
188
  mock = Minitest::Mock.new
@@ -223,6 +245,26 @@ class ThermosTest < ActiveSupport::TestCase
223
245
  assert_raises(MockExpectationError) { mock.verify }
224
246
  end
225
247
 
248
+ test "re-builds the cache for has_many record changes when filter condition is met" do
249
+ mock = Minitest::Mock.new
250
+ category = categories(:baseball)
251
+ filter = ->(model) { model.ball? }
252
+
253
+ Thermos.fill(key: "key", model: Category, deps: [:category_items], filter: filter) do |id|
254
+ mock.call(id)
255
+ end
256
+
257
+ mock.expect(:call, 1, [category.id])
258
+ CategoryItem.create!(category: category)
259
+ mock.verify
260
+
261
+ category.update!(name: "hockey")
262
+
263
+ mock.expect(:call, 1, [category.id])
264
+ CategoryItem.create!(category: category)
265
+ assert_raises(MockExpectationError) { mock.verify }
266
+ end
267
+
226
268
  # belongs_to model changes
227
269
  test "rebuilds the cache on belongs_to model change" do
228
270
  mock = Minitest::Mock.new
@@ -286,6 +328,27 @@ class ThermosTest < ActiveSupport::TestCase
286
328
  assert_raises(MockExpectationError) { mock.verify }
287
329
  end
288
330
 
331
+ test "re-builds the cache for belongs_to record changes when filter condition is met" do
332
+ mock = Minitest::Mock.new
333
+ category = categories(:baseball)
334
+ filter = ->(model) { model.ball? }
335
+
336
+ Thermos.fill(key: "key", model: Category, deps: [:store], filter: filter) do |id|
337
+ mock.call(id)
338
+ end
339
+
340
+ mock.expect(:call, 1, [category.id])
341
+ mock.expect(:call, 1, [category.id])
342
+ Store.create!(name: "foo", categories: [category])
343
+ mock.verify
344
+
345
+ category.update!(name: "hockey")
346
+
347
+ mock.expect(:call, 2, [category.id])
348
+ Store.create!(name: "bar", categories: [category])
349
+ assert_raises(MockExpectationError) { mock.verify }
350
+ end
351
+
289
352
  # has_many through model changes
290
353
  test "rebuilds the cache on has_many through model change" do
291
354
  mock = Minitest::Mock.new
@@ -348,6 +411,26 @@ class ThermosTest < ActiveSupport::TestCase
348
411
  assert_raises(MockExpectationError) { mock.verify }
349
412
  end
350
413
 
414
+ test "re-builds the cache for has_many through record changes when filter condition is met" do
415
+ mock = Minitest::Mock.new
416
+ category = categories(:baseball)
417
+ filter = ->(model) { model.ball? }
418
+
419
+ Thermos.fill(key: "key", model: Category, deps: [:products], filter: filter) do |id|
420
+ mock.call(id)
421
+ end
422
+
423
+ mock.expect(:call, 1, [category.id])
424
+ Product.create!(categories: [category])
425
+ mock.verify
426
+
427
+ category.update!(name: "hockey")
428
+
429
+ mock.expect(:call, 2, [category.id])
430
+ Product.create!(categories: [category])
431
+ assert_raises(MockExpectationError) { mock.verify }
432
+ end
433
+
351
434
  test "handles indirect associations" do
352
435
  mock = Minitest::Mock.new
353
436
  category = categories(:baseball)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thermos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Thal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-24 00:00:00.000000000 Z
11
+ date: 2021-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails