Pry

Use Pry as your rails console
Icons/chart bar
Used 80 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/Xo5sbm"
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 Pry as your rails console' "
end

def has_pry_prompt_in_pryrc?(pryrc_path)
  @has_pry_prompt_in_pryrc ||= begin
    File.readlines(pryrc_path).any? { |line| line.include?("Pry.config.prompt") }
  end
end

say "\nApplying Pry as your rails console..."
inject_into_file 'Gemfile', after: 'group :development, :test do' do
  <<-RUBY 

  # Use Pry as your rails console
  gem "pry-rails"
  gem "pry-byebug", platform: :ruby
  RUBY
end

do_bundle

pryrc_path = File.expand_path('~/.pryrc')
create_file pryrc_path, "\n" unless File.exist?(pryrc_path)

unless has_pry_prompt_in_pryrc?(pryrc_path)
  prepend_file pryrc_path do
    <<~EOF 
      Pry.config.prompt = PryRails::RAILS_PROMPT if defined?(PryRails::RAILS_PROMPT)
    EOF
  end
end

gitignore_path = File.expand_path('~/.gitignore')
create_file gitignore_path, "\n" unless File.exist? gitignore_path
append_file gitignore_path do
  <<~EOF 

  # Ignore IRB config and command history files.
  .irbrc
  .irb_history
  .pryrc

  # Ignore byebug command history file.
  .byebug_history
  EOF
end

say "\nAdding documentation for developers..."
create_file "doc/miscellaneous.md", "# Miscellaneous\n" unless File.exist? "doc/miscellaneous.md"
append_file "doc/miscellaneous.md" do
  <<~EOF  
  
  ## IRB console

  Add to your `~/.irbrc`:

  ```ruby
  require 'irb/completion'
  
  IRB.conf[:PROMPT_MODE] = :SIMPLE
  IRB.conf[:AUTO_INDENT_MODE] = false
  IRB.conf[:SAVE_HISTORY] = 2000
  ```
  
  ### Pry for Rails console

  [Pry](https://rubygems.org/gems/pry) is a runtime developer console and IRB
  alternative with powerful introspection capabilities. Pry aims to be more than
  an IRB replacement. It is an attempt to bring [REPL driven programming](https://pry.github.io/)
  to the Ruby language.

  [byebug](https://rubygems.org/gems/pry-byebug) adds 'step', 'next', 'finish', 'continue'
  and 'break' commands to control execution.

  Add to your `~/.pryrc`:

  ```ruby
  Pry.config.prompt = PryRails::RAILS_PROMPT if defined?(PryRails::RAILS_PROMPT)
  ```
  EOF
end

do_commit

print_green "\nAdded Pry successfully!"
Comments

Sign up or Login to leave a comment.