prosopite
N+1 auto-detection for Rails with zero false positives / false negatives
Used 68 times
V
Viktor Schmidt
Usage
RubyGems: https://rubygems.org/gems/prosopite
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/xkjsdG"
Template Source
Review the code before running this template on your machine.
def do_bundle
Bundler.with_unbundled_env { run "bundle install" }
end
def print_green(heredoc)
puts set_color heredoc, :green
end
def do_commit
git :init
git add: "."
Bundler.with_unbundled_env { git commit: " -m 'Add Prosopite for N+1 auto-detection' " }
end
say "Applying Prosopite for N+1 auto-detection..."
inject_into_file 'Gemfile', after: 'group :development, :test do' do
<<-RUBY
# N+1 auto-detection for Rails with zero false positives / false negatives
gem "prosopite"
gem "pg_query"
RUBY
end
do_bundle
create_file "config/initializers/prosopite.rb" do
<<~RUBY
# frozen_string_literal: true
if defined? Prosopite
Rails.application.configure do
config.after_initialize do
Prosopite.prosopite_logger = true
Prosopite.rails_logger = false
Prosopite.stderr_logger = false
Prosopite.raise = Rails.env.test?
end
end
end
RUBY
end
create_file "app/controllers/concerns/n_plus_one_detectable.rb" do
<<~RUBY
# frozen_string_literal: true
# N+1 auto-detection for Rails with zero false positives / false negatives
# Depends on Prosopite gem
module NPlusOneDetectable
extend ActiveSupport::Concern
# Do not use n_plus_one_detection in production, because it slows down requests
if Rails.env.development? || Rails.env.test?
included do
around_action :n_plus_one_detection
def n_plus_one_detection
Prosopite.scan
yield
ensure
Prosopite.finish
end
end
end
end
RUBY
end
inject_into_class "app/controllers/application_controller.rb", "ApplicationController" do
<<-RUBY
include NPlusOneDetectable # Depends on Prosopite gem
RUBY
end
say "\nAdding documentation for developers..."
create_file "docs/performance.md", "# Performance\n" unless File.exist? "docs/performance.md"
append_file "docs/performance.md" do
<<~EOF
## Prosopite
[Prosopite](https://rubygems.org/gems/prosopite) feature N+1 auto-detection
for Rails with zero false positives / false negatives.
EOF
end
run "bin/rubocop -AS"
do_commit
print_green "\nAdded Prosopite successfully!"