counter_cache_update 0.0.3 → 0.0.7

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
- SHA1:
3
- metadata.gz: 5a21fc1b22472205f939ea1c6518e5aacd6bb128
4
- data.tar.gz: dd2e5334684f63c5d985779ef5223f4eccb1bdb5
2
+ SHA256:
3
+ metadata.gz: f727cafaa635682ea718968a48a16181fbd331be75b2721435bc1c54afba595d
4
+ data.tar.gz: 0223c953148013b3b34099cda00b6fe2522810e1f51b38b1b54bde09fba11dce
5
5
  SHA512:
6
- metadata.gz: 56a49fa503c5e5405de2da18ed7e5f8ad0bc9217f8c27a5b91097857cb9733d78052becf25c30f5a740464cf6f54a5119394263a9ea96b95d919f3dac093baf1
7
- data.tar.gz: 759891b5030e227a1038d4d9d105335031d10580efb23146b562b9895df65681f7f4060bbc10fc45b4eeebfb779d095ed0f3169f43e42449371ad8d948f27fc2
6
+ metadata.gz: 5ba5596de6ebe289ca4d46919516005dc6fe3fe12f4032de4bd89065f49ab6a4c0d99b3b27ce04206efb9b207767883fbd5a006909c01cc3ad140bbbd920284e
7
+ data.tar.gz: 6c59c8f4d63629c14396cca7b4ed02098fef3aca63f70ae3460c96ae70874c0f690f3f0dd0d4a428e411d7b933631a9461e603796a7404e66127466016fefa1d
@@ -0,0 +1,23 @@
1
+ class CounterCacheUpdate::PolymorphicUpdatorService < ServicePattern::Service
2
+ def initialize(reflection:)
3
+ @class = reflection.active_record
4
+ @reflection = reflection
5
+ end
6
+
7
+ def perform
8
+ foreign_types.each do |foreign_type|
9
+ CounterCacheUpdate::TableUpdatorService.execute!(reflection: @reflection, model_class: foreign_type.constantize)
10
+ end
11
+
12
+ succeed!
13
+ end
14
+
15
+ private
16
+
17
+ # Returns the class names of those records that has the given polymorphic relationship
18
+ def foreign_types
19
+ @reflection.active_record
20
+ .group(@reflection.foreign_type)
21
+ .pluck(@reflection.foreign_type)
22
+ end
23
+ end
@@ -1,14 +1,16 @@
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
- def execute!
11
+ def perform
10
12
  model_class.connection.execute(sql)
11
- ServicePattern::Response.new(success: true)
13
+ succeed!
12
14
  end
13
15
 
14
16
  private
@@ -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
@@ -5,7 +5,7 @@ class CounterCacheUpdate::UpdateService < ServicePattern::Service
5
5
  @use_progress_bar = use_progress_bar
6
6
  end
7
7
 
8
- def execute!
8
+ def perform
9
9
  Rails.application.eager_load!
10
10
 
11
11
  if use_progress_bar
@@ -14,7 +14,7 @@ class CounterCacheUpdate::UpdateService < ServicePattern::Service
14
14
  end
15
15
 
16
16
  update_relationships!
17
- ServicePattern::Response.new(success: true)
17
+ succeed!
18
18
  end
19
19
 
20
20
  private
@@ -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.7".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
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.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-17 00:00:00.000000000 Z
11
+ date: 2021-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -30,14 +30,42 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.0.3
33
+ version: 1.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.0.3
40
+ version: 1.0.0
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'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tzinfo-data
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description: Scans all your models and updates all counter caches with optimised SQL.
42
70
  email:
43
71
  - kaspernj@gmail.com
@@ -56,6 +84,7 @@ files:
56
84
  - app/jobs/counter_cache_update/application_job.rb
57
85
  - app/mailers/counter_cache_update/application_mailer.rb
58
86
  - app/models/counter_cache_update/application_record.rb
87
+ - app/services/counter_cache_update/polymorphic_updator_service.rb
59
88
  - app/services/counter_cache_update/table_updator_service.rb
60
89
  - app/services/counter_cache_update/update_service.rb
61
90
  - app/views/layouts/counter_cache_update/application.html.erb
@@ -76,15 +105,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
105
  requirements:
77
106
  - - ">="
78
107
  - !ruby/object:Gem::Version
79
- version: '0'
108
+ version: '2.5'
80
109
  required_rubygems_version: !ruby/object:Gem::Requirement
81
110
  requirements:
82
111
  - - ">="
83
112
  - !ruby/object:Gem::Version
84
113
  version: '0'
85
114
  requirements: []
86
- rubyforge_project:
87
- rubygems_version: 2.6.13
115
+ rubygems_version: 3.1.6
88
116
  signing_key:
89
117
  specification_version: 4
90
118
  summary: Scans all your models and updates all counter caches with optimised SQL.