counter_cache_update 0.0.3 → 0.0.4

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: 5a21fc1b22472205f939ea1c6518e5aacd6bb128
4
- data.tar.gz: dd2e5334684f63c5d985779ef5223f4eccb1bdb5
3
+ metadata.gz: da60d0659cc80bc69235f6c6d8e3da19e9ff9b8d
4
+ data.tar.gz: efe7c55d60d096aa73dfe7df01bb02ed0fba690c
5
5
  SHA512:
6
- metadata.gz: 56a49fa503c5e5405de2da18ed7e5f8ad0bc9217f8c27a5b91097857cb9733d78052becf25c30f5a740464cf6f54a5119394263a9ea96b95d919f3dac093baf1
7
- data.tar.gz: 759891b5030e227a1038d4d9d105335031d10580efb23146b562b9895df65681f7f4060bbc10fc45b4eeebfb779d095ed0f3169f43e42449371ad8d948f27fc2
6
+ metadata.gz: 8d7cb3a66131cea09114c11025503d6471914fcca61bb0e88c499dae815fdd8bd9523dda9f13e272c22817a63198a29ba39d245dae96a09b07c30d8c9335798e
7
+ data.tar.gz: a4ec4d8c17afd0c1b0f572eb3b6978d7c0c34c9582f5f517b680bf3ba98c8fccde36e4d28a9fb876a867c113ed35c1af65bcf0678dcd53eeb5eff33f06053968
@@ -0,0 +1,21 @@
1
+ class CounterCacheUpdate::PolymorphicUpdatorService < ServicePattern::Service
2
+ def initialize(reflection:)
3
+ @class = reflection.active_record
4
+ @reflection = reflection
5
+ end
6
+
7
+ def execute!
8
+ foreign_types.each do |foreign_type|
9
+ CounterCacheUpdate::TableUpdatorService.execute!(reflection: @reflection, model_class: foreign_type.constantize)
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ # Returns the class names of those records that has the given polymorphic relationship
16
+ def foreign_types
17
+ @reflection.active_record
18
+ .group(@reflection.foreign_type)
19
+ .pluck(@reflection.foreign_type)
20
+ end
21
+ end
@@ -1,9 +1,11 @@
1
1
  class CounterCacheUpdate::TableUpdatorService < ServicePattern::Service
2
2
  attr_reader :model_class, :reflection
3
3
 
4
- def initialize(reflection:)
4
+ def initialize(reflection:, model_class: nil)
5
5
  @reflection = reflection
6
- @model_class = @reflection.class_name.constantize
6
+
7
+ @model_class = model_class
8
+ @model_class ||= @reflection.class_name.constantize
7
9
  end
8
10
 
9
11
  def execute!
@@ -24,7 +26,9 @@ private
24
26
  end
25
27
 
26
28
  def count_sql
27
- "SELECT COUNT(*) FROM #{reflection_table_name} WHERE #{reflection_table_name}.#{relation_foreign_key} = #{table_name}.#{primary_key}"
29
+ sql = "SELECT COUNT(*) FROM #{reflection_table_name} WHERE #{reflection_table_name}.#{relation_foreign_key} = #{table_name}.#{primary_key}"
30
+ sql << " AND #{reflection.foreign_type} = #{ActiveRecord::Base.connection.quote(model_class.name)}" if reflection.options[:polymorphic]
31
+ sql
28
32
  end
29
33
 
30
34
  def primary_key
@@ -26,8 +26,12 @@ private
26
26
  model_class.reflections.each do |_name, reflection|
27
27
  next unless reflection.macro.to_sym == :belongs_to
28
28
  next unless reflection.options[:counter_cache]
29
- next if reflection.options[:polymorphic]
30
- CounterCacheUpdate::TableUpdatorService.execute!(reflection: reflection)
29
+
30
+ if reflection.options[:polymorphic]
31
+ CounterCacheUpdate::PolymorphicUpdatorService.execute!(reflection: reflection)
32
+ else
33
+ CounterCacheUpdate::TableUpdatorService.execute!(reflection: reflection)
34
+ end
31
35
  end
32
36
  end
33
37
  end
@@ -1,3 +1,3 @@
1
1
  module CounterCacheUpdate
2
- VERSION = "0.0.3".freeze
2
+ VERSION = "0.0.4".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter_cache_update
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.0.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.3.6
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.6
41
55
  description: Scans all your models and updates all counter caches with optimised SQL.
42
56
  email:
43
57
  - kaspernj@gmail.com
@@ -56,6 +70,7 @@ files:
56
70
  - app/jobs/counter_cache_update/application_job.rb
57
71
  - app/mailers/counter_cache_update/application_mailer.rb
58
72
  - app/models/counter_cache_update/application_record.rb
73
+ - app/services/counter_cache_update/polymorphic_updator_service.rb
59
74
  - app/services/counter_cache_update/table_updator_service.rb
60
75
  - app/services/counter_cache_update/update_service.rb
61
76
  - app/views/layouts/counter_cache_update/application.html.erb