5

Rails.env.local? in Rails 7.1

 11 months ago
source link: https://blog.saeloun.com/2023/06/26/rails-env-local/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rails.env.local? in Rails 7.1

Jun 26, 2023

authorImgVipul A M

authorImg

Vipul A M

I am an active member of Ruby community. I have been consistently contributing to Ruby on Rails for a number of years and now am one of the top 30 contributors to Ruby on Rails. I also help as co-editor for the This week in Rails newsletter. Besides Ruby on Rails I have also contributed to many other notable open source projects including Sinatra, Devise and Rake. I am a seasoned speaker an have spoken at many conferences around the world including Gogaruco in San Francisco, RedDotRubyConf in Singapore, RubyConfIndia in Goa, India MadisonPlusRuby in Madison, Wisconsin, RubyConfBrazil in Suo Paulo, Brazil, and RubyConf Philippines in Manilla, Philippines. I am organizer of Deccan Ruby Conference and used to run RubyIndia Podcast. During my early days of open source as part of "Google summer of code" I contributed to the krypt-project project. Later I helped mentor in the JRuby and currently mentor in the Ruby on Rails organization for Google summer of code. When not working on Ruby, I am mostly working on Reactjs. I have authored the book Building Modern Web Applications with React.js which is published by PACKT. I have produced a number of screencasts on the topic of Learn React.js.

1 minute read

Rails 7.1 introduced a new method Rails.env.local? which returns true if the environment is development or test. This is useful when we want to run some code only in development or test environment.

Before

In Rails Applications, we have many code paths that want to run only in development or test environment. We maybe have many different enviroments like development, test, staging, production etc. Or multiple staging and review applications.

In such cases, we have to check for the environment name in the code. For example:

unless Rails.env.development? || Rails.env.test?
  # call some external API
  ExternalAPI.call 
end

if Rails.env.development? || Rails.env.test?
  # create some test data for development or test environment  
end

After

With the introduction of Rails.env.local?, we can now write the above code as:

unless Rails.env.local?
  # call some external API
  ExternalAPI.call 
end

if Rails.env.local?
  # create some test data for development or test environment  
end

IRB and Rails Console auto-completion

Rails 7.1 also introduced a feature to disable auto-completion in Rails console by default on production.

This is useful when we are working with a large Rails application and the auto-completion is slow especially when connecting remotely to the console.

Previously, this auto-completion was disabled with Rails.env.production? check to not run only in production.

But that meant it was still enabled and made things slower or introduced issues with auto-completion in staging or other environments.

With this change, auto-completion is disabled by default, only if the environment is development or test, leveraging the new Rails.env.local? method.

Summary

Rails.env.local? shorthands the check for Rails.env.development? || Rails.env.test? and is useful when we want to run some code only in development or test environment.

Share this post!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK