prosopite

N+1 auto-detection for Rails with zero false positives / false negatives
Icons/chart bar
Used 46 times
Created by
V Viktor Schmidt

Usage

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_original_env { run "bundle install" }
end

def print_green(heredoc)
  puts set_color heredoc, :green
end

def do_commit
  git :init
  git add: "."
  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", "~> 1.0", ">= 1.0.6"
  gem "pg_query", "~> 2.1", ">= 2.1.2"
  RUBY
end

do_bundle

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

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 "doc/performance.md", "# Performance\n" unless File.exist? "doc/performance.md"
append_file "doc/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

do_commit

print_green "\nAdded Prosopite successfully!"
Comments

Sign up or Login to leave a comment.