Bullet

Public
Bullet help to kill N+1 queries and unused eager loading.
Icons/chart bar
Used 54 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/z5Os2Z"
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 Bullet to kill N+1 queries and unused eager loading' "
end

say "Applying Bullet to kill N+1 queries and unused eager loading..."
inject_into_file 'Gemfile', after: 'group :development, :test do' do
  <<-RUBY
  
  # Help to kill N+1 queries and unused eager loading.
  gem "bullet", "~> 7.0", ">= 7.0.1", require: false
  RUBY
end

do_bundle

create_file "config/initializers/bullet.rb" do 
  <<~EOF
  if defined? Bullet
    Rails.application.configure do
      config.after_initialize do
        # watch your queries while you develop your application and notify you
        # when you should add eager loading (N+1 queries),
        # when you use eager loading that is not necessary - and
        # when you should use counter caching
        Bullet.enable = true
        Bullet.console = false
        Bullet.rails_logger = false
        Bullet.bullet_logger = true
        Bullet.alert = false
        Bullet.add_footer = false
        Bullet.raise = false
        # Detect N+1 queries.
        Bullet.n_plus_one_query_enable = true
        # Detect eager-loaded associations which are not used. Warning: very slow!
        Bullet.unused_eager_loading_enable = true
        # Detect unnecessary COUNT queries which could be avoided with a counter_cache
        Bullet.counter_cache_enable = true
      end
    end
  end
EOF
end

# inject_into_file 'app/jobs/application_job.rb', after: "class ApplicationJob < ActiveJob::Base\n" do 
#   <<-'RUBY'
#   if defined? Bullet
#     around_perform do |_job, block|
#       Bullet.profile do
#         block.call
#       end
#     end
#   end
#   RUBY
# end

say "\nAdding documentation for developers..."
create_file "doc/development.md", "# Readme for Developers" unless File.exist? "doc/development.md"
append_file "doc/development.md" do <<~EOF

  ## Performance
  EOF
end
append_file "doc/development.md" do <<~EOF  
  
  ### Bullet

  [Bullet](https://rubygems.org/gems/bullet) helps to kill N+1 queries and unused eager loading.
  EOF
end

do_commit

print_green "\nAdded Bullet successfully!"
Comments

Sign up or Login to leave a comment.