LoginSignup
0
0

More than 1 year has passed since last update.

rails で `development?` を維持したまま新しい環境を追加したいとき

Last updated at Posted at 2021-09-07

新しく環境を追加したい場合(staging とか)はこちらの方法でできるけど、
https://qiita.com/yusabana/items/a1f4fe2c37b20db2a3f6

Rails.env.development? とか Rails.env.staging?method_missing で実装されてるので、
https://github.com/rails/rails/blob/v6.1.4/activesupport/lib/active_support/string_inquirer.rb#L28

例えば development と同等の環境を新しく development-foo-bar みたいに追加しようとしても、
Rails.env.development-foo-bar? とかやるとメソッド名にハイフンが使えないので syntax error になるし、
肝心の Rails.env.development?false のままになってしまうので期待通りにいかない。

ので、こんな感じで extend してやるといいかも。

config/initializers/00_force-env-development.rb
module ForceEnvDevelopment
  def development?
    true
  end
end

case Rails.env
when 'development-foo-bar' then
  Rails.env.extend ForceEnvDevelopment
end

っていう話。

0
0
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
0
0