16th July, 2024

display a warning when on a production rails console

I’m not sure where I stole this from, but I definitely got this from a blog post that I’ve entirely failed to find when looking again today.

Apologies to that original blog post for the blatant code theft, but I recently started a new project and found myself missing this handy little warning and had to go spelunking through old projects and thought it would be handy here.

# .irbrc
# Add color coding based on Rails environment for safety

if defined? Rails
  banner = if Rails.env.production?
             "\e[41;97;1m #{Rails.env} \e[0m "
           else
             "\e[42;97;1m #{Rails.env} \e[0m "
           end

  # build a custom prompt
  IRB.conf[:PROMPT][:CUSTOM] = IRB.conf[:PROMPT][:DEFAULT].merge(
    PROMPT_I: banner + IRB.conf[:PROMPT][:DEFAULT][:PROMPT_I],)

  # use custom prompt by default
  IRB.conf[:PROMPT_MODE] = :CUSTOM
end