LoginSignup
25
22

More than 5 years have passed since last update.

Production環境のRails ConsoleをKAIZENする

Last updated at Posted at 2014-06-28

production環境とdevelopment環境で複数のターミナルを開いている時に、誤ってproduction側でUser.delete_allなんてやってしまった日には、泣くしかありません。
そうならないために、pryのpromptを拡張して自分が今どの環境のコンソールを触っているかわかるようにする。

以下をGemfileに書く。

Gemfile
gem 'pry', require: false
gem 'pry-rails', require: 'pry-rails/console'

稼働しているサイト側でもpryを読み込んでしまうと無駄なメモリを使用してしまうので、require: falseを必ず付ける。
pry-debuggerなどのpry拡張はproductionでは不要のため、group :developmentの中に入れておく。

config/initializers/pry.rb
if defined?(Rails::Console)
  require 'pry'
  env = if Rails.env.production?
    Pry::Helpers::Text.red(Rails.env)
  elsif Rails.env.development?
    Rails.env
  else
    Pry::Helpers::Text.purple(Rails.env)
  end
  Pry.config.prompt = [
    proc {|target_self, nest_level, pry|
      nested = (nest_level.zero?) ? '' : ":#{nest_level}"
      "[#{pry.input_array.size}] #{env} #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}(#{Pry.view_clip(target_self)})#{nested}> "
    },
    proc {|target_self, nest_level, pry|
      nested = (nest_level.zero?) ?  '' : ":#{nest_level}"
      "[#{pry.input_array.size}] #{env} #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}(#{Pry.view_clip(target_self)})#{nested}* "
    }
  ]
end

これで、以下のようにプロンプトが表示され、誤操作の心配がなくなります!

792ac18c-fc77-11e3-957e-0261677f8489.png

25
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
22