Rails helper method that works differently in different environments

In the Ruby on Rails application, I have a controller where I would like some functions to be executed conditionally, where the condition depends on the environment in which the application runs. As a far-fetched example in development mode, I would like to do this:

if foo == 5:
    ...
end

And in production mode, I would like to:

if foo > 6:
    ...
end

The difference between the two conditions is more complex than one constant (5 or 6 in the above example).

What is the most idiomatic way to do this in Rails? Can I write helper methods directly in files environments/? Or add a method to an application controller that checks the current environment? Or something else?

+3
2

ENV['RAILS_ENV'] .

http://guides.rubyonrails.org/configuring.html#rails-environment-settings

:

if foo == 5 && ENV['RAILS_ENV'] == "development" then
    ...
elsif foo > 6 && ENV['RAILS_ENV'] == "production" then
    ...
end

, .

, application.rb :

def isDev
    ENV['RAILS_ENV'] == "development"
end

def isProd
    ENV['RAILS_ENV'] == "production"
end
+7

, . , . :

before_filter :setup_variables

def setup_variables
  @development = (ENV['RAILS_ENV'] == "development")
  @production  = (ENV['RAILS_ENV'] == "production")
end

, , .

. ( /), , . , ProductionLogic.

+1

Source: https://habr.com/ru/post/1781339/


All Articles