Skip to content

Commit ef58fd0

Browse files
committed
rubocop omakase
1 parent c21a6d3 commit ef58fd0

23 files changed

+481
-390
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ jobs:
2727
- name: Install dependencies
2828
run: bundle install
2929
- name: Run tests
30-
run:
31-
bundle exec rspec
30+
run: bundle exec rspec
31+
- name: Rubocop
32+
run: bundle exec rubocop --parallel

.rubocop.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Omakase Ruby styling for Rails
2+
inherit_gem: { rubocop-rails-omakase: rubocop.yml }
3+
4+
AllCops:
5+
TargetRubyVersion: 3.1
6+
7+
Layout/AssignmentIndentation:
8+
Enabled: true
9+
10+
Layout/DefEndAlignment:
11+
Enabled: true
12+
EnforcedStyleAlignWith: def
13+
AutoCorrect: true
14+
15+
Layout/EmptyLineBetweenDefs:
16+
Enabled: true
17+
NumberOfEmptyLines: 1
18+
19+
Layout/FirstArrayElementIndentation:
20+
Enabled: true
21+
EnforcedStyle: consistent
22+
23+
Layout/HeredocIndentation:
24+
Enabled: true
25+
26+
Layout/IndentationConsistency:
27+
Enabled: true
28+
EnforcedStyle: normal
29+
30+
Layout/IndentationStyle:
31+
Enabled: true
32+
33+
Layout/IndentationWidth:
34+
Enabled: true
35+
Width: 2
36+
37+
Style/ClassAndModuleChildren:
38+
Enabled: true
39+
EnforcedStyle: nested
40+
41+
Lint/Debugger:
42+
Enabled: true
43+
44+
Style/FrozenStringLiteralComment:
45+
Enabled: true
46+
47+
Style/StringLiterals:
48+
Enabled: true
49+
EnforcedStyle: single_quotes

Gemfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

3-
gem "activerecord", ">=7"
4-
gem "sqlite3", "~> 1.4"
5+
gem 'activerecord', '>=7'
6+
gem 'sqlite3', '~> 1.4'
7+
gem 'rubocop-rails-omakase', '~> 1.1', require: false
58
# Specify your gem's dependencies in jit_preloader.gemspec
69
gemspec

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ This gem will publish an `n_plus_one_query` event via ActiveSupport::Notificatio
9090

9191
You could implement some basic tracking. This will let you measure the extent of the N+1 query problems in your app:
9292
```ruby
93-
ActiveSupport::Notifications.subscribe("n_plus_one_query") do |event, data|
93+
ActiveSupport::Notifications.subscribe('n_plus_one_query') do |event, data|
9494
statsd.increment "web.#{Rails.env}.n_plus_one_queries.global"
9595
end
9696
```
9797

9898
You could log the N+1 queries. In your development environment, you could throw N+1 queries into the logs along with a stack trace:
9999
```ruby
100-
ActiveSupport::Notifications.subscribe("n_plus_one_query") do |event, data|
100+
ActiveSupport::Notifications.subscribe('n_plus_one_query') do |event, data|
101101
message = "N+1 Query detected: #{data[:association]} on #{data[:source].class}"
102-
backtrace = caller.select{|r| r.starts_with?(Rails.root.to_s) }
102+
backtrace = caller.select { |r| r.starts_with?(Rails.root.to_s) }
103103
Rails.logger.debug("\n\n#{message}\n#{backtrace.join("\n")}\n".red)
104104
end
105105
```
@@ -113,7 +113,7 @@ config.around(:each) do |example|
113113
raise QueryError.new(message)
114114
end
115115
end
116-
ActiveSupport::Notifications.subscribed(callback, "n_plus_one_query") do
116+
ActiveSupport::Notifications.subscribed(callback, 'n_plus_one_query') do
117117
example.run
118118
end
119119
end
@@ -144,7 +144,7 @@ There is now a `has_many_aggregate` method available for ActiveRecord::Base. Thi
144144
```ruby
145145
# old
146146
Contact.all.each do |contact|
147-
contact.addresses.maximum("LENGTH(street)")
147+
contact.addresses.maximum('LENGTH(street)')
148148
contact.addresses.count
149149
end
150150
# SELECT * FROM contacts
@@ -159,8 +159,8 @@ end
159159
#new
160160
class Contact < ActiveRecord::Base
161161
has_many :addresses
162-
has_many_aggregate :addresses, :max_street_length, :maximum, "LENGTH(street)", default: nil
163-
has_many_aggregate :addresses, :count_all, :count, "*"
162+
has_many_aggregate :addresses, :max_street_length, :maximum, 'LENGTH(street)', default: nil
163+
has_many_aggregate :addresses, :count_all, :count, '*'
164164
end
165165

166166
Contact.jit_preload.each do |contact|
@@ -177,7 +177,7 @@ Furthermore, there is an argument `max_ids_per_query` setting max ids per query.
177177
```ruby
178178
class Contact < ActiveRecord::Base
179179
has_many :addresses
180-
has_many_aggregate :addresses, :count_all, :count, "*", max_ids_per_query: 10
180+
has_many_aggregate :addresses, :count_all, :count, '*', max_ids_per_query: 10
181181
end
182182

183183
Contact.jit_preload.each do |contact|
@@ -197,26 +197,26 @@ This is a method `preload_scoped_relation` that is available that can handle thi
197197
#old
198198
class Contact < ActiveRecord::Base
199199
has_many :addresses
200-
has_many :usa_addresses, ->{ where(country: Country.find_by_name("USA")) }
200+
has_many :usa_addresses, -> { where(country: Country.find_by_name('USA')) }
201201
end
202202

203203
Contact.jit_preload.all.each do |contact|
204204
# This will preload the association as expected, but it must be defined as an association in advance
205205
contact.usa_addresses
206206

207207
# This will preload as the entire addresses association, and filters it in memory
208-
contact.addresses.select{|address| address.country == Country.find_by_name("USA") }
208+
contact.addresses.select { |address| address.country == Country.find_by_name('USA') }
209209

210210
# This is an N+1 query
211-
contact.addresses.where(country: Country.find_by_name("USA"))
211+
contact.addresses.where(country: Country.find_by_name('USA'))
212212
end
213213

214214
# New
215215
Contact.jit_preload.all.each do |contact|
216216
contact.preload_scoped_relation(
217-
name: "USA Addresses",
217+
name: 'USA Addresses',
218218
base_association: :addresses,
219-
preload_scope: Address.where(country: Country.find_by_name("USA"))
219+
preload_scope: Address.where(country: Country.find_by_name('USA'))
220220
)
221221
end
222222
# SELECT * FROM contacts
@@ -236,14 +236,14 @@ JitPreloader.globally_enabled = true
236236
# Can also be given anything that responds to `call`.
237237
# You could build a kill switch with Redis (or whatever you'd like)
238238
# so that you can turn it on or off dynamically.
239-
JitPreloader.globally_enabled = ->{ $redis.get('always_jit_preload') == 'on' }
239+
JitPreloader.globally_enabled = -> { $redis.get('always_jit_preload') == 'on' }
240240

241241
# Setting global max ids constraint on all aggregation methods.
242242
JitPreloader.max_ids_per_query = 10
243243

244244
class Contact < ActiveRecord::Base
245245
has_many :emails
246-
has_many_aggregate :emails, :count_all, :count, "*"
246+
has_many_aggregate :emails, :count_all, :count, '*'
247247
end
248248

249249
# When enabled globally, this would not generate an N+1 query.

Rakefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
require "bundler/gem_tasks"
1+
# frozen_string_literal: true
22

3+
require 'bundler/gem_tasks'

jit_preloader.gemspec

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
# coding: utf-8
2+
# frozen_string_literal: true
3+
24
lib = File.expand_path('../lib', __FILE__)
35
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
46
require 'jit_preloader/version'
57

68
Gem::Specification.new do |spec|
7-
spec.name = "jit_preloader"
9+
spec.name = 'jit_preloader'
810
spec.version = JitPreloader::VERSION
9-
spec.authors = ["Kyle d'Oliveira"]
10-
spec.email = ["[email protected]"]
11-
spec.summary = %q{Tool to understand N+1 queries and to remove them}
12-
spec.description = %q{The JitPreloader has the ability to send notifications when N+1 queries occur to help guage how problematic they are for your code base and a way to remove all of the commons explicitly or automatically}
13-
spec.homepage = "https://github.com/clio/jit_preloader"
14-
spec.metadata["homepage_uri"] = spec.homepage
15-
spec.metadata["source_code_uri"] = spec.homepage
11+
spec.authors = [ "Kyle d'Oliveira" ]
12+
spec.email = [ '[email protected]' ]
13+
spec.summary = 'Tool to understand N+1 queries and to remove them'
14+
spec.description = 'The JitPreloader has the ability to send notifications when N+1 queries occur to help guage how problematic they are for your code base and a way to remove all of the commons explicitly or automatically'
15+
spec.homepage = 'https://github.com/clio/jit_preloader'
16+
spec.metadata['homepage_uri'] = spec.homepage
17+
spec.metadata['source_code_uri'] = spec.homepage
18+
spec.required_ruby_version = '>= 3.0.0'
1619

17-
spec.license = "MIT"
20+
spec.license = 'MIT'
1821

19-
spec.files = `git ls-files -z`.split("\x0")
22+
spec.files = Dir.glob('lib/**/*.rb') + [ File.basename(__FILE__) ]
2023
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
2124
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22-
spec.require_paths = ["lib"]
25+
spec.require_paths = [ 'lib' ]
2326

24-
spec.add_dependency "activerecord", "< 8"
25-
spec.add_dependency "activesupport"
27+
spec.add_dependency 'activerecord', '< 8'
28+
spec.add_dependency 'activesupport'
2629

27-
spec.add_development_dependency "bundler"
28-
spec.add_development_dependency "rake", "~> 13.0"
29-
spec.add_development_dependency "rspec"
30-
spec.add_development_dependency "database_cleaner"
31-
spec.add_development_dependency "sqlite3"
32-
spec.add_development_dependency "byebug"
33-
spec.add_development_dependency "db-query-matchers"
30+
spec.add_development_dependency 'bundler'
31+
spec.add_development_dependency 'rake', '~> 13.0'
32+
spec.add_development_dependency 'rspec'
33+
spec.add_development_dependency 'database_cleaner'
34+
spec.add_development_dependency 'sqlite3'
35+
spec.add_development_dependency 'rubocop-rails_config'
36+
spec.add_development_dependency 'byebug'
37+
spec.add_development_dependency 'db-query-matchers'
3438
end

lib/jit_preloader.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
# frozen_string_literal: true
2+
13
require 'active_support/concern'
24
require 'active_support/core_ext/module/delegation'
35
require 'active_support/notifications'
46
require 'active_record'
57

6-
require "jit_preloader/version"
8+
require 'jit_preloader/version'
79
require 'jit_preloader/active_record/base'
810
require 'jit_preloader/active_record/relation'
911
require 'jit_preloader/active_record/associations/collection_association'
1012
require 'jit_preloader/active_record/associations/singular_association'
11-
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("7.0.0")
13+
if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('7.0.0')
1214
require 'jit_preloader/active_record/associations/preloader/ar7_association'
1315
require 'jit_preloader/active_record/associations/preloader/ar7_branch'
14-
elsif Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("6.1.0")
16+
elsif Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('6.1.0')
1517
require 'jit_preloader/active_record/associations/preloader/ar6_association'
1618
else
1719
require 'jit_preloader/active_record/associations/preloader/collection_association'
@@ -41,5 +43,4 @@ def self.globally_enabled?
4143
@enabled
4244
end
4345
end
44-
4546
end

lib/jit_preloader/active_record/associations/collection_association.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# frozen_string_literal: true
2+
13
module JitPreloader
24
module ActiveRecordAssociationsCollectionAssociation
3-
45
def load_target
56
was_loaded = loaded?
67

@@ -17,10 +18,10 @@ def load_target
1718
JitPreloader::Preloader.attach(records) if records.any? && !jit_loaded && JitPreloader.globally_enabled?
1819

1920
# If the records were not pre_loaded
20-
records.each{ |record| record.jit_n_plus_one_tracking = true }
21+
records.each { |record| record.jit_n_plus_one_tracking = true }
2122

2223
if !jit_loaded && owner.jit_n_plus_one_tracking
23-
ActiveSupport::Notifications.publish("n_plus_one_query",
24+
ActiveSupport::Notifications.publish('n_plus_one_query',
2425
source: owner, association: reflection.name)
2526
end
2627
end

lib/jit_preloader/active_record/associations/preloader/ar6_association.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# frozen_string_literal: true
2+
13
module JitPreloader
24
module PreloaderAssociation
3-
45
# A monkey patch to ActiveRecord. The old method looked like the snippet
56
# below. Our changes here are that we remove records that are already
67
# part of the target, then attach all of the records to a new jit preloader.

lib/jit_preloader/active_record/associations/preloader/ar7_association.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# frozen_string_literal: true
2+
13
module JitPreloader
24
module PreloaderAssociation
3-
45
# A monkey patch to ActiveRecord. The old method looked like the snippet
56
# below. Our changes here are that we remove records that are already
67
# part of the target, then attach all of the records to a new jit preloader.

lib/jit_preloader/active_record/associations/preloader/ar7_branch.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# frozen_string_literal: true
2+
13
module JitPreloader
24
module PreloaderBranch
3-
"""
5+
''"
46
ActiveRecord version >= 7.x.x introduced an improvement for preloading associations in batches:
57
https://github.com/rails/rails/blob/main/activerecord/lib/active_record/associations/preloader.rb#L121
68
@@ -9,7 +11,7 @@ module PreloaderBranch
911
But this change breaks that behaviour because now Batch is calling `klass.base_class` (a method defined by ActiveRecord::Base)
1012
before we have a chance to filter out the non-AR classes.
1113
This patch for AR 7.x makes the Branch class ignore any association loaders that aren't for ActiveRecord::Base subclasses.
12-
"""
14+
"''
1315

1416
def loaders
1517
@loaders = super.find_all do |loader|

0 commit comments

Comments
 (0)