bulletmark_repairer 0.1.1 → 0.1.3

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: 5a5858e9552c2cf6259b9102c1f0980607baab37030962e55b56e83b5fd61d36
4
- data.tar.gz: ebcd21ae7ab79f0113e53862fff5ef5700d24385d1eea98eb3bb731e533aff9d
3
+ metadata.gz: a7110364b32065ccdce57c9220481a6c3ab36265a1fbc1fcb0d40a5d6a92ceeb
4
+ data.tar.gz: e69d9ae29d6a2f0e013e8d974ac5634e98750c31cf57eacbbbee1ed48778f5ce
5
5
  SHA512:
6
- metadata.gz: 5251a08f17df69a9d8df9c6770d823b90a4bf99949f5ce241ee78f124365e63cd655fe68b9eb6d10b6a7bc048ac4d7de87f010593fc2c492a80abadf164d1d54
7
- data.tar.gz: b5f4c055095c3372ffffa0b9ae87b64c56d024b27fd177ddb99854063215cabae75692fb3d603bdaac77f01c97753a4b8e6aac7e90fc0af8905b142e3c48064d
6
+ metadata.gz: 4eb6a1e8d2274100f04d72f544c0dce8588b758216bfbe08968947fc535d7fe3e0fba8f7d722600737c18e52cbe468e0ed2482e48e9d9372ca18c3f66e1c05fa
7
+ data.tar.gz: fe6a169678171236abebdff07cf2adfef7af32066c07656ad327fbb33d692a0b9171c5b418932c653ae4a8c0eb95be823c8d29d2f17423f7fe7d8be663fd5aa2
data/.codespellignore ADDED
File without changes
data/CHANGELOG.md CHANGED
@@ -1,7 +1,19 @@
1
+ ## [0.1.3] - 2023-10-18
2
+
3
+ - Fix a redundant auto-correct for multiple tests with n+1 queries when running RSpec [#6](https://github.com/makicamel/bulletmark_repairer/pull/6) ([@ydah])
4
+
5
+ ## [0.1.2] - 2023-10-16
6
+
7
+ - Reduce dependencies [#1](https://github.com/makicamel/bulletmark_repairer/pull/1) ([@tricknotes])
8
+ - Stop using class instance variables for thread safe [024f6c5](https://github.com/makicamel/bulletmark_repairer/commit/024f6c53f82b182a998c1e43de48d8c6c9ce5bf3)
9
+
1
10
  ## [0.1.1] - 2023-10-11
2
11
 
3
- - Support Ruby 3.0 dbdf27c6c9a7259ad9474153d2394da5bac45b43
12
+ - Support Ruby 3.0 [dbdf27c](https://github.com/makicamel/bulletmark_repairer/commit/dbdf27c6c9a7259ad9474153d2394da5bac45b43)
4
13
 
5
14
  ## [0.1.0] - 2023-10-10
6
15
 
7
16
  - Initial release
17
+
18
+ [@tricknotes]: https://github.com/tricknotes
19
+ [@ydah]: https://github.com/ydah
data/Gemfile CHANGED
@@ -8,7 +8,6 @@ gemspec
8
8
  gem 'rake', '~> 13.0'
9
9
 
10
10
  gem 'factory_bot_rails'
11
- gem 'rails'
12
11
  gem 'rspec', '~> 3.0'
13
12
  gem 'rspec-rails', '~> 3.0'
14
13
  gem 'sqlite3'
data/README.md CHANGED
@@ -7,7 +7,7 @@ BulletmarkRepairer is an auto corrector for N+1 queries detected at runtime on R
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'bulletmark_repairer', group :development, :test
10
+ gem 'bulletmark_repairer', group: %w(development test)
11
11
  ```
12
12
 
13
13
  ## Usage
@@ -1,18 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BulletmarkRepairer
4
- def self.associations
5
- @associations ||= ApplicationAssociations.new
6
- end
7
-
8
- def self.key(target_klass_name, base_klass_name, candidates)
9
- associations.key(target_klass_name, base_klass_name, candidates)
10
- end
11
-
12
- def self.reset_associations
13
- @associations = nil
14
- end
15
-
16
4
  class ApplicationAssociations
17
5
  def key(target_klass_name, base_klass_name, candidates)
18
6
  key = target_klass_name.underscore
@@ -10,13 +10,19 @@ module BulletmarkRepairer
10
10
  if associations[marker.index]
11
11
  associations[marker.index].add(marker)
12
12
  else
13
- associations[marker.index] = Associations.new(marker)
13
+ associations[marker.index] = Associations.new(marker, @application_associations)
14
14
  end
15
15
  end
16
16
 
17
17
  def associations
18
18
  @associations ||= {}
19
19
  end
20
+
21
+ private
22
+
23
+ def initialize
24
+ @application_associations = BulletmarkRepairer::ApplicationAssociations.new
25
+ end
20
26
  end
21
27
 
22
28
  class Associations
@@ -38,9 +44,10 @@ module BulletmarkRepairer
38
44
 
39
45
  private
40
46
 
41
- def initialize(marker)
47
+ def initialize(marker, application_associations)
42
48
  @marker = marker
43
49
  @associations = { base: marker.associations }
50
+ @application_associations = application_associations
44
51
  end
45
52
 
46
53
  # @return [Hash, nil]
@@ -66,12 +73,12 @@ module BulletmarkRepairer
66
73
  def formed_key(marker:, associations:)
67
74
  case associations
68
75
  when Hash
69
- BulletmarkRepairer.key(marker.base_class, @marker.base_class, associations.keys) ||
70
- BulletmarkRepairer.key(marker.base_class, @marker.base_class, associations.values.flatten)
76
+ @application_associations.key(marker.base_class, @marker.base_class, associations.keys) ||
77
+ @application_associations.key(marker.base_class, @marker.base_class, associations.values.flatten)
71
78
  when Array
72
- BulletmarkRepairer.key(marker.base_class, @marker.base_class, associations)
79
+ @application_associations.key(marker.base_class, @marker.base_class, associations)
73
80
  else # Symbol, String
74
- BulletmarkRepairer.key(marker.base_class, @marker.base_class, [associations])
81
+ @application_associations.key(marker.base_class, @marker.base_class, [associations])
75
82
  end
76
83
  end
77
84
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BulletmarkRepairer
4
- class Configration
4
+ class Configuration
5
5
  attr_accessor :skip_file_list, :logger
6
6
  attr_writer :debug
7
7
 
@@ -22,7 +22,7 @@ module BulletmarkRepairer
22
22
  end
23
23
 
24
24
  def config
25
- @config ||= Configration.new
25
+ @config ||= Configuration.new
26
26
  end
27
27
  end
28
28
  end
@@ -46,10 +46,12 @@ class ControllerCorrector < Parser::TreeRewriter
46
46
  return unless node.respond_to?(:to_sexp_array)
47
47
 
48
48
  type, identifier = node.to_sexp_array.take(2)
49
-
50
49
  if type == :ivasgn && identifier == instance_variable_name
51
- insert_after node.children.last.location.expression, ".includes(#{associations})"
52
- @patched = true
50
+ inserted = ".includes(#{associations})"
51
+ unless node.location.expression.source.include?(inserted)
52
+ insert_after node.children.last.location.expression, inserted
53
+ @patched = true
54
+ end
53
55
  else
54
56
  node
55
57
  .children
@@ -5,7 +5,7 @@ require 'securerandom'
5
5
  require 'parser/runner/ruby_rewrite'
6
6
 
7
7
  module BulletmarkRepairer
8
- class Pathcer
8
+ class Patcher
9
9
  def self.execute(notifications:, controller:, action:)
10
10
  new(notifications: notifications, controller: controller, action: action).execute
11
11
  end
@@ -7,12 +7,11 @@ module BulletmarkRepairer
7
7
  end
8
8
 
9
9
  def call(env)
10
- BulletmarkRepairer.reset_associations
11
10
  @app.call(env)
12
11
  ensure
13
12
  begin
14
13
  if Thread.current[:bullet_notification_collector].notifications_present?
15
- BulletmarkRepairer::Pathcer.execute(
14
+ BulletmarkRepairer::Patcher.execute(
16
15
  notifications: Thread.current[:bullet_notification_collector],
17
16
  controller: env['action_dispatch.request.parameters']['controller'],
18
17
  action: env['action_dispatch.request.parameters']['action']
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BulletmarkRepairer
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.3'
5
5
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'bulletmark_repairer/version'
4
4
  require 'bulletmark_repairer/railtie' if ENV['REPAIR']
5
- require 'bulletmark_repairer/bulletmark_repairer'
5
+ require 'bulletmark_repairer/application_associations'
6
6
  require 'bulletmark_repairer/associations_builder'
7
7
  require 'bulletmark_repairer/configuration'
8
8
  require 'bulletmark_repairer/corrector_builder'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulletmark_repairer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - makicamel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-10 00:00:00.000000000 Z
11
+ date: 2023-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bullet
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +53,7 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: rails
56
+ name: railties
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
@@ -59,6 +73,7 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
76
+ - ".codespellignore"
62
77
  - ".rspec"
63
78
  - ".rubocop.yml"
64
79
  - CHANGELOG.md
@@ -68,11 +83,10 @@ files:
68
83
  - README.md
69
84
  - Rakefile
70
85
  - lib/bulletmark_repairer.rb
86
+ - lib/bulletmark_repairer/application_associations.rb
71
87
  - lib/bulletmark_repairer/associations_builder.rb
72
- - lib/bulletmark_repairer/bulletmark_repairer.rb
73
88
  - lib/bulletmark_repairer/configuration.rb
74
89
  - lib/bulletmark_repairer/controller_corrector.rb
75
- - lib/bulletmark_repairer/corrector.rb
76
90
  - lib/bulletmark_repairer/corrector_builder.rb
77
91
  - lib/bulletmark_repairer/markers.rb
78
92
  - lib/bulletmark_repairer/patcher.rb
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Corrector < Parser::TreeRewriter
4
- def on_def(node)
5
- return if patched?
6
-
7
- node.children.each { |child_node| insert_includes(node: child_node) }
8
- return if patched?
9
-
10
- node.children.each { |child_node| insert_includes_for_vasgn(node: child_node, type: :ivasgn) }
11
- return if patched?
12
-
13
- node.children.each { |child_node| insert_includes_for_vasgn(node: child_node, type: :lvasgn) }
14
- end
15
-
16
- private
17
-
18
- def patched?
19
- @patched ||= false
20
- end
21
-
22
- def insert_includes(node:)
23
- return if patched?
24
- return if !node.respond_to?(:children) || node.children.empty?
25
- return unless node.location.expression.line <= line_no && line_no <= node.location.expression.last_line
26
-
27
- # TODO: Patch Enumerable methods other than each and map
28
- if node.children.last.in?(%i[each map])
29
- insert_after node.children[0].location.expression, ".includes(#{associations})"
30
- @patched = true
31
- else
32
- node.children.each { |child_node| insert_includes(node: child_node) }
33
- end
34
- end
35
-
36
- def insert_includes_for_vasgn(node:, type:)
37
- return if patched?
38
- return if !node.respond_to?(:children) || node.children.empty?
39
- return unless node.location.expression.line <= line_no && line_no <= node.location.expression.last_line
40
-
41
- if node.type == type
42
- insert_after node.children.last.location.expression, ".includes(#{associations})"
43
- @patched = true
44
- else
45
- node.children.each { |child_node| insert_includes_for_vasgn(node: child_node, type: type) }
46
- end
47
- end
48
-
49
- def line_no
50
- __EMBEDDED_LINE_NO__
51
- end
52
-
53
- def associations
54
- '__EMBEDDED_ASSOCIATIONS__'
55
- end
56
- end