Avoid environment based conditionals
Itโs common to query the current Rails environment when configuring code such an API client.
This can lead to duplicated conditional logic in various places so itโs difficult to manage application configuration as itโs scattered throughout the code base.
Instead we can manage custom application configuration, the Rails way. This has the added benefit of supporting inheritence and allowing the overriding of configuration.
Example ๐
Bad ๐
hostname = if Rails.env.production?
"biggerpockets"
elsif Rails.env.in? %w(staging review)
ENV["HEROKU_APP_NAME"]
else
ENV["DEFAULT_HOST"]
end
Good ๐
hostname = Rails.configuration.x.hostname
# application.rb
config.x.hostname = ENV["DEFAULT_HOST"]
# production.rb
config.x.hostname = "biggerpockets"
# staging.rb
config.x.hostname = ENV["HEROKU_APP_NAME"]