I'd like the code in one of my initializers (in config/initializers/
) to be run only for the :development
environment, but not :test
or :production
. What's the best way to do that?
Pasting it into config/environments/test.rb
seems unclean, and I don't quite like wrapping the entire initializer file in an if Rails.env == 'development' do ... end
statement. Is there some canonical way to do this?
(Background: To speed up test load times, I'm trying to move the Barista gem into the :development
group of my Gemfile
, but config/initializers/barista_config.rb
calls Barista.configure
, so now it chokes on that in test (and production) mode.)
An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.
In the environment. rb file you configure these run-levels. For example, you could use it to have some special settings for your development stage, which are usefull for debugging. The purpose of this file is to configure things for the whole application like encoding.
In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The configuration file config/application. rb and environment-specific configuration files (such as config/environments/production.
Railtie is the core of the Rails Framework and provides several hooks to extend Rails and/or modify the initialization process.
I'm pretty sure your only two options are putting the configuration code in config/environments/development.rb
or wrapping your initializer code with your if
block. You can tighten up your second option by doing if Rails.env.development?
, though.
I don't know if this is a good idea, but it's a different idea.
You could create a config/initializers/development
directory (or config/environments/development/initializers
), put your barista_config.rb
in that directory, and then include anything in that directory from config/environments/development.rb
.
I don't know if that's a good idea or not but it's at least a third option...just in case you're still thinking about this problem three and a half years after asking the question.
Barista has a config setting for this:
Barista.configure do |c|
c.env = :development
end
This will only recompile coffescript into js in dev mode and should speed up your tests.
Make sure you run:
rake barista:brew
before checking your code in.
https://github.com/Sutto/barista
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With