thermos 0.4.1 → 0.5.0

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: e8a6fdb2b9baf6513ed750de28354540a73231dad66cf99d6de39ccfd74debc7
4
- data.tar.gz: 0a5de531cb59a1c4b9b86e3875fffd78df0b1bb242fc2ec39ac1b740772cc360
3
+ metadata.gz: 87ceed3d0f1b54b2cd9e9b0afde15e90252fcbc9f575c5bf11a66e414045c5d5
4
+ data.tar.gz: dc3bc76cd808f474b80676c5f2d1f2e50681c6028c09412e12eb565b4494cfc4
5
5
  SHA512:
6
- metadata.gz: b1b7efff2c5be5ed8f93c5a7649f4804c8f3fca74a8a77e6a64065017545bec2f22ade70ebdbe41bb548c419179b3e6755877341a172a078ed8466ca3dcf7dd2
7
- data.tar.gz: 2c09712783766bdfb3960660af31ee5a9806e707e49080fc4b677c3d8566cb1e01f582b37cd263023c9b0dcf4be7b2641248f9d6a8cb43450545a00fd177ca7a
6
+ metadata.gz: d86ada3f6a3b07ed8bbf35c481e73ac4563a0aced184f6e47a986847287f0bf4a3c02c42e5d7600db1f5d8c2358f5ba4975fd20c4ccceee8fc3ec0cfcaae7811
7
+ data.tar.gz: 51f90cb754d381d81293e433261e09cb4e184dc1dcddd05fbfcbf7baacb577330f6a4cc6a0621cc8cd9964d7677e499554a8dd533d65762adb421d56cef58458
@@ -9,9 +9,7 @@ module Thermos
9
9
  @model = model
10
10
  @lookup_key = lookup_key || :id
11
11
  @filter = filter || nil
12
- @deps = deps.map do |dep|
13
- Dependency.new(model: model, association: dep)
14
- end
12
+ @deps = generate_deps(model, deps)
15
13
  @action = action
16
14
 
17
15
  set_observers
@@ -21,7 +19,7 @@ module Thermos
21
19
  @deps.select do |dep|
22
20
  dep.klass == dep_model.class
23
21
  end.flat_map do |dep|
24
- @model.joins(dep.association)
22
+ @model.joins(dep.path)
25
23
  .where(dep.table => { id: dep_model.id })
26
24
  .pluck(@lookup_key)
27
25
  end.uniq
@@ -41,5 +39,34 @@ module Thermos
41
39
  def observe(model)
42
40
  model.include(Notifier) unless model.included_modules.include?(Notifier)
43
41
  end
42
+
43
+ def generate_deps(model, deps, root = nil, path = nil)
44
+ deps.reduce([]) do |acc, dep|
45
+ if dep.is_a? Symbol
46
+ acc << Dependency.new(
47
+ model: root || model,
48
+ ref: model.reflect_on_association(dep),
49
+ path: path || dep)
50
+ elsif dep.is_a? Array
51
+ dep.each do |d|
52
+ acc << Dependency.new(
53
+ model: root || model,
54
+ ref: model.reflect_on_association(d),
55
+ path: path || d)
56
+ end
57
+ elsif dep.is_a? Hash
58
+ dep.each do |k,v|
59
+ ref = model.reflect_on_association(k)
60
+ acc << Dependency.new(
61
+ model: root || model,
62
+ ref: ref,
63
+ path: path || k
64
+ )
65
+ acc.concat(generate_deps(ref.class_name.constantize, v, model, dep))
66
+ end
67
+ end
68
+ acc
69
+ end
70
+ end
44
71
  end
45
72
  end
@@ -2,14 +2,13 @@
2
2
 
3
3
  module Thermos
4
4
  class Dependency
5
- attr_reader :model, :association, :klass, :table
5
+ attr_reader :model, :path, :klass, :table
6
6
 
7
- def initialize(model:, association:)
7
+ def initialize(model:, ref:, path: nil)
8
8
  @model = model
9
- @association = association
10
- reflection = @model.reflections[@association.to_s]
11
- @table = reflection.table_name
12
- @klass = reflection.class_name.constantize
9
+ @path = path
10
+ @table = ref.table_name
11
+ @klass = ref.class_name.constantize
13
12
  end
14
13
  end
15
14
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Thermos
4
- VERSION = '0.4.1'
4
+ VERSION = '0.5.0'
5
5
  end
data/test/thermos_test.rb CHANGED
@@ -331,6 +331,30 @@ class ThermosTest < ActiveSupport::TestCase
331
331
  assert_raises(MockExpectationError) { mock.verify }
332
332
  end
333
333
 
334
+ test "handles indirect associations" do
335
+ mock = Minitest::Mock.new
336
+ category = categories(:baseball)
337
+ store = category.store
338
+
339
+ Thermos.fill(key: "key", model: Store, deps: [categories: [:products]]) do |id|
340
+ mock.call(id)
341
+ end
342
+
343
+ mock.expect(:call, 1, [store.id])
344
+ category.update!(name: "foo")
345
+ mock.verify
346
+
347
+ mock.expect(:call, 2, [store.id])
348
+ assert_equal 1, Thermos.drink(key: "key", id: store.id)
349
+ assert_raises(MockExpectationError) { mock.verify }
350
+ Product.create!(categories: [category])
351
+ mock.verify
352
+
353
+ mock.expect(:call, 3, [store.id])
354
+ assert_equal 2, Thermos.drink(key: "key", id: store.id)
355
+ assert_raises(MockExpectationError) { mock.verify }
356
+ end
357
+
334
358
  test "only rebuilds cache for stated dependencies, even if another cache has an associated model of the primary" do
335
359
  category_mock = Minitest::Mock.new
336
360
  product_mock = Minitest::Mock.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thermos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Thal