├── Rakefile ├── .rspec ├── .travis.yml ├── lib ├── awesome_rails_console │ ├── version.rb │ ├── railtie.rb │ └── prompts.rb ├── generators │ └── awesome_rails_console │ │ └── install │ │ ├── templates │ │ └── .pryrc │ │ ├── USAGE │ │ └── install_generator.rb └── awesome_rails_console.rb ├── spec ├── spec_helper.rb ├── awesome_rails_console_spec.rb └── awesome_rails_console │ └── prompts_spec.rb ├── Gemfile ├── bin ├── setup └── console ├── .gitignore ├── LICENSE ├── CHANGELOG.md ├── awesome_rails_console.gemspec └── README.md /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.2.1 4 | -------------------------------------------------------------------------------- /lib/awesome_rails_console/version.rb: -------------------------------------------------------------------------------- 1 | module AwesomeRailsConsole 2 | VERSION = "0.4.5" 3 | end 4 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 2 | require 'awesome_rails_console' 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in awesome_rails_console.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | /awesome_rails_console-*.gem 11 | -------------------------------------------------------------------------------- /lib/generators/awesome_rails_console/install/templates/.pryrc: -------------------------------------------------------------------------------- 1 | if Rails.env.development? || Rails.env.test? 2 | # This introduces the `table` statement 3 | extend Hirb::Console 4 | end 5 | -------------------------------------------------------------------------------- /lib/awesome_rails_console.rb: -------------------------------------------------------------------------------- 1 | require "awesome_rails_console/version" 2 | if defined?(Rails) 3 | require "awesome_rails_console/prompts" 4 | require "awesome_rails_console/railtie" 5 | end 6 | -------------------------------------------------------------------------------- /lib/generators/awesome_rails_console/install/USAGE: -------------------------------------------------------------------------------- 1 | Description: 2 | Install hirb, pry-byebug, pry-stack_explorer to make your rails console even more awesome 3 | 4 | Example: 5 | rails generate awesome_rails_console:install 6 | -------------------------------------------------------------------------------- /spec/awesome_rails_console_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe AwesomeRailsConsole do 4 | it 'has a version number' do 5 | expect(AwesomeRailsConsole::VERSION).not_to be nil 6 | end 7 | 8 | # TODO: Add more test or remove rspec entirely 9 | end 10 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "awesome_rails_console" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /lib/generators/awesome_rails_console/install/install_generator.rb: -------------------------------------------------------------------------------- 1 | require 'rails/generators/base' 2 | 3 | class AwesomeRailsConsole::InstallGenerator < Rails::Generators::Base 4 | source_root File.expand_path('../templates', __FILE__) 5 | 6 | def update_gemfile 7 | gem_group :development, :test do 8 | gem 'hirb' 9 | gem 'hirb-unicode-steakknife', require: 'hirb-unicode' 10 | gem 'pry-byebug' 11 | gem 'pry-stack_explorer' 12 | end 13 | 14 | inject_into_file 'Gemfile', indication, before: gem_group_declaration 15 | end 16 | 17 | def write_to_pryrc_file 18 | copy_file '.pryrc', '.pryrc' 19 | end 20 | 21 | private 22 | 23 | def indication 24 | "# Please clean up duplicated gems if any.\n"\ 25 | "# Feel free to remove gems that you don't want to use or "\ 26 | "if they conflict with other gem dependencies. "\ 27 | "(you might need to update .pryrc also)\n" 28 | end 29 | 30 | def gem_group_declaration 31 | "group :development, :test do\n"\ 32 | " gem 'hirb'\n" 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 by Bruce Li 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/awesome_rails_console/railtie.rb: -------------------------------------------------------------------------------- 1 | require "pry-rails" 2 | require "awesome_print" 3 | 4 | module AwesomeRailsConsole 5 | class Railtie < Rails::Railtie 6 | initializer "awesome_rails_console.initialize" do |app| 7 | disables_pry_plugin_loading 8 | use_awesome_print_for_formatting 9 | set_prompt_name_to_project_name 10 | show_rails_env_name_before_prompt 11 | end 12 | 13 | private 14 | 15 | def disables_pry_plugin_loading 16 | Pry.config.should_load_plugins = false 17 | end 18 | 19 | def use_awesome_print_for_formatting 20 | AwesomePrint.pry! 21 | end 22 | 23 | def set_prompt_name_to_project_name 24 | # ref from https://github.com/rweng/pry-rails/blob/035d5c8203521f5eaac0001300152281c765df88/lib/pry-rails/prompt.rb 25 | Pry.config.prompt_name = 26 | if Rails::VERSION::MAJOR >= 6 27 | Rails.application.class.module_parent_name.underscore 28 | else 29 | Rails.application.class.parent_name.underscore 30 | end 31 | end 32 | 33 | def show_rails_env_name_before_prompt 34 | Prompts.choose_prompt_for_pry_version 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ## v0.4.5 4 | 5 | * Fix `undefined method [] for Pry::Prompt:Class (NoMethodError)` error when entering rails console 6 | 7 | ## v0.4.4 8 | 9 | * Fix deprecation warning `Module#parent_name` in Rails 6.x 10 | 11 | ## v0.4.3 12 | 13 | * Fix my stupid mistake on v0.4.2: Require hirb-unicode-steakknife as hirb-unicode 14 | 15 | ## v0.4.2 [DO NOT USE] 16 | 17 | * Require hirb-unicode-steakknife as unicode-steakknife 18 | * BUG: The require name was wrong 19 | 20 | ## v0.4.1 [DO NOT USE] 21 | 22 | * Replace `hirb-unicode` with `hirb-unicode-steakknife` in optional enhancement. (Run `rails generate awesome_rails_console:install` to install them) 23 | * BUG: It doesn't require properly 24 | 25 | ## v0.4.0 26 | 27 | * Add `pry-byebug`, `pry-stack_explorer` and `hirb` back as optional enhancement. (Run `rails generate awesome_rails_console:install` to install them) 28 | 29 | ## v0.3.0 30 | 31 | * Remove `pry-byebug`, `pry-stack_explorer` and `hirb` dependency. Please add them into your application `Gemfile` if you still want to use. 32 | * Remove color indicator from prompt. 33 | 34 | ## v0.2.1 35 | 36 | * No changes in functionality. Release for updating information on rubygems.org. 37 | 38 | ## v0.2.0 39 | 40 | * Remove Ruby 1.9 support. 41 | 42 | ## v0.1.0 43 | 44 | * First release. 45 | -------------------------------------------------------------------------------- /lib/awesome_rails_console/prompts.rb: -------------------------------------------------------------------------------- 1 | require "pry-rails" 2 | 3 | module AwesomeRailsConsole 4 | module Prompts 5 | def self.choose_prompt_for_pry_version 6 | old_prompt = Pry.config.prompt 7 | 8 | Pry.config.prompt = if Pry::VERSION && (Gem::Version.new(Pry::VERSION) >= Gem::Version.new('0.13.0')) 9 | post_pry_13_prompt(old_prompt) 10 | else 11 | pre_pry_13_prompt(old_prompt) 12 | end 13 | end 14 | 15 | # Define a custom prompt using pre pry-0.13.0 syntax 16 | def self.pre_pry_13_prompt(old_prompt) 17 | [ 18 | proc { |*a| "#{Rails.env.classify} #{old_prompt.first.call(*a)}" }, 19 | proc { |*a| "#{Rails.env.classify} #{old_prompt.second.call(*a)}" } 20 | ] 21 | end 22 | 23 | # Pry 0.13.0 introduces new syntax for registering a prompt. 24 | # https://github.com/pry/pry/wiki/Customization-and-configuration#Config_prompt 25 | def self.post_pry_13_prompt(old_prompt) 26 | Pry::Prompt.new( 27 | :awesome_rails_console, # name 28 | "awesome_rails_console default prompt", # description 29 | [ 30 | proc { |*a| # "main" prompt 31 | "#{Rails.env.classify} #{old_prompt.wait_proc.call(*a)}" 32 | }, 33 | proc { |*a| # "wait" prompt (multiline input continuation) 34 | "#{Rails.env.classify} #{old_prompt.incomplete_proc.call(*a)}" 35 | } 36 | ] 37 | ) 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /awesome_rails_console.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'awesome_rails_console/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "awesome_rails_console" 8 | spec.version = AwesomeRailsConsole::VERSION 9 | spec.authors = ["Bruce Li"] 10 | spec.email = ["ascendbruce@gmail.com"] 11 | 12 | spec.summary = %q{Simple and useful rails console enhancements} 13 | spec.description = %q{Enhance rails console by using awesome_print, pry and several pry plugins. And useful prompt tweaks. Makes rails console awesome by default.} 14 | spec.homepage = "https://github.com/ascendbruce/awesome_rails_console" 15 | 16 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 17 | spec.bindir = "exe" 18 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 19 | spec.require_paths = ["lib"] 20 | 21 | spec.required_ruby_version = ">= 2.0.0" 22 | spec.license = "MIT" 23 | 24 | # Runtime dependencies 25 | spec.add_dependency "railties" 26 | spec.add_dependency "pry-rails" 27 | spec.add_dependency "awesome_print" 28 | 29 | # gem development dependency 30 | spec.add_development_dependency "bundler", "~> 1.9" 31 | spec.add_development_dependency "rake", "~> 10.0" 32 | spec.add_development_dependency "rspec", "~> 3.2" 33 | end 34 | -------------------------------------------------------------------------------- /spec/awesome_rails_console/prompts_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | require 'awesome_rails_console/prompts' 4 | 5 | describe AwesomeRailsConsole::Prompts do 6 | let(:old_prompt) { double('Pry.config.prompt') } 7 | 8 | describe '.choose_prompt_for_pry_version' do 9 | before do 10 | expect(Pry.config).to receive(:prompt).and_return(old_prompt) 11 | end 12 | 13 | context 'pry version < 0.13.0' do 14 | before do 15 | stub_const("Pry::VERSION", '0.12.2') 16 | end 17 | 18 | it 'uses pre-0.13.0 prompt' do 19 | expect(described_class).to receive(:pre_pry_13_prompt).with(old_prompt) 20 | described_class.choose_prompt_for_pry_version 21 | end 22 | end 23 | 24 | context 'pry version >= 0.13.0' do 25 | before do 26 | stub_const("Pry::VERSION", '0.13.1') 27 | end 28 | 29 | it 'uses post-0.13.0 prompt' do 30 | expect(described_class).to receive(:post_pry_13_prompt).with(old_prompt) 31 | described_class.choose_prompt_for_pry_version 32 | end 33 | end 34 | end 35 | 36 | describe '.pre_pry_13_prompt' do 37 | subject { described_class.pre_pry_13_prompt(old_prompt) } 38 | 39 | it 'returns array of procs' do 40 | expect(subject).to be_a_kind_of(Array) 41 | expect(subject.all?{|p| p.is_a?(Proc)}).to be_truthy 42 | end 43 | end 44 | 45 | describe '.post_pry_13_prompt' do 46 | it 'returns instance of Pry::Prompt' do 47 | expect(described_class.post_pry_13_prompt(old_prompt)).to be_a_kind_of(Pry::Prompt) 48 | end 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Make Your Rails Console Awesome 2 | 3 | This gem was inspired by [Using pry in production](https://bugsnag.com/blog/production-pry), [jazz_hands](https://github.com/nixme/jazz_hands) and [jazz_fingers](https://github.com/plribeiro3000/jazz_fingers). 4 | 5 | The pros of `awesome_rails_console` are: 6 | 7 | * Less gem dependances (Only pry-rails and awesome_print other than rails. The rest are optional) 8 | * Simpler prompt modification (Similar to the default prompt you're already familiar with) 9 | * No need to worry about configuration (because there are not much options anyway) 10 | 11 | ## Installation 12 | 13 | Gemfile: 14 | 15 | ``` ruby 16 | gem 'awesome_rails_console' 17 | ``` 18 | 19 | In terminal: 20 | 21 | ``` sh 22 | bundle 23 | rails g awesome_rails_console:install # This will include dependency gems to the gemfile 24 | # you should review your Gemfile at this point (and adjust if needed) 25 | ``` 26 | 27 | ```sh 28 | bundle 29 | spring stop # to restart spring, if you are using it 30 | rails c 31 | ``` 32 | 33 | ## Features 34 | 35 | ### Show Rails env and project name in the prompt 36 | 37 | Prevents you from accidentally changing production data to the wrong project. 38 | 39 | ![](http://i.imgur.com/CKrJYqk.png) 40 | 41 | ### Beautiful formatting with pry and awesome_print 42 | 43 | Make it easy to read. Reduce the pain while debugging. 44 | 45 | ```ruby 46 | # Try following statements in rails console: 47 | [:apple, :orange, :banana] 48 | { a: 1, b: 2, c: 3 } 49 | 1.methods 50 | (1..100).to_a 51 | ap (1..100).to_a, limit: 5 52 | ``` 53 | 54 | ![](http://i.imgur.com/I1iV8n9.png) 55 | 56 | ### Print table in console 57 | 58 | With Hirb (optional enhancement) 59 | 60 | Very handy when you need to paste some data into issue tracking system. 61 | 62 | ![](http://i.imgur.com/9z3XDSU.png) 63 | 64 | ### Debugger 65 | 66 | With pry-byebug (optional enhancement) 67 | 68 | Insert `binding.pry` (break point) to start debugging. See [pry-byebug](https://github.com/deivid-rodriguez/pry-byebug) For detail. 69 | 70 | ![](http://i.imgur.com/mJbC24h.png) 71 | 72 | ## Gem development 73 | 74 | ### Debug 75 | 76 | ```rb 77 | # local 78 | gem 'awesome_rails_console', path: '/path/to/folder/awesome_rails_console' 79 | 80 | # github branch 81 | gem 'awesome_rails_console', github: 'ascendbruce/awesome_rails_console.git', branch: 'debug-branch' 82 | ``` 83 | 84 | ### Release new version 85 | 86 | Reference: 87 | 88 | ```sh 89 | gem build awesome_rails_console.gemspec 90 | gem push awesome_rails_console-?.?.?.gem 91 | ``` 92 | --------------------------------------------------------------------------------