DEV Community

Josua Schmid
Josua Schmid

Posted on

Customize the Rails Console

You can configure what happens when running rails console with the console block in application.rb. Here I register a custom descendant of the Rails IRB console to change coloring:

console do
  require "rails/commands/console/irb_console.rb"

  class CloudyConsole < Rails::Console::IRBConsole
    def colorized_env
      if Rails.env.production?
        if ENV.fetch("HEROKU_APP_NAME", "")[/-develop|-staging/]
          return IRB::Color.colorize("prod", [:YELLOW])
        end

        if `hostname`[/develop-/]
          return IRB::Color.colorize("prod", [:YELLOW])
        end
      end

      super
    end
  end

  config.console = CloudyConsole.new(Rails.application)
end
Enter fullscreen mode Exit fullscreen mode

Top comments (0)