counter_cache_update 0.0.1 → 0.0.6

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: 612e3322b4f6660aee4c90461cb98dc2a3b7d9d5
4
- data.tar.gz: 83f8c0cec5da605ff621ce17b7a5220660d1d4a5
2
+ SHA256:
3
+ metadata.gz: 3232bf33791a0d62bd54567cc7b4ba04e38e41e373a0b29b37be33858c8b2c21
4
+ data.tar.gz: 18d47f91a50f9a16ecfc3a0e795e83600ae5bc0812f04fb60732122467bc7762
5
5
  SHA512:
6
- metadata.gz: fba1046c4769cf685c3c57d9420cfd28f5ebea989900760304eb859ee4e8bc094f31b0697329ccc1a54acff6f865248eb520e81c68790aa240ddb31229283209
7
- data.tar.gz: e55553d1d386bc0ef45091d6fb23815950f0e92475001c8e67d8445ff24742d37a61f66698a27ec1461382dcf1889dfdd3623883edf53d0cde6125067a7589ec
6
+ metadata.gz: 7a3990b4d98619ae51a57c05095a84f03154970a200d47b6bb5d916eff6dc2eb5bdb67b83853ea2a75399c609286926169c7f99feca9d3bdb2d88752c5072098
7
+ data.tar.gz: ca5b97db68cd097c97b44aa538acf58706d7b1377a4e2187c4bc16ed6199fa0df554b20ab611a0769db8582922676431bdf59f4f50124aeed5e7b4087d94203c
@@ -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,7 +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
- 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
30
35
  end
31
36
  end
32
37
  end
@@ -1,3 +1,3 @@
1
1
  module CounterCacheUpdate
2
- VERSION = "0.0.1".freeze
2
+ VERSION = "0.0.6".freeze
3
3
  end
metadata CHANGED
@@ -1,43 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter_cache_update
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-25 00:00:00.000000000 Z
11
+ date: 2021-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 5.1.5
19
+ version: 5.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 5.1.5
26
+ version: 5.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: service_pattern
29
29
  requirement: !ruby/object:Gem::Requirement
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.