Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Per-environment initializers?

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.)

like image 342
Jo Liss Avatar asked Jan 27 '11 19:01

Jo Liss


People also ask

What are Initializers Rails?

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.

What is the purpose of environment RB and application RB file?

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.

What are Rails configurations?

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.

What are Railties?

Railtie is the core of the Rails Framework and provides several hooks to extend Rails and/or modify the initialization process.


3 Answers

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.

like image 84
Dylan Markow Avatar answered Oct 05 '22 04:10

Dylan Markow


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.

like image 34
Jason Swett Avatar answered Oct 05 '22 06:10

Jason Swett


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

like image 22
shawn42 Avatar answered Oct 05 '22 06:10

shawn42