Jo Liss
Jo Liss

Reputation: 32945

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

Upvotes: 49

Views: 19866

Answers (3)

Jason Swett
Jason Swett

Reputation: 45074

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.

Upvotes: 4

shawn42
shawn42

Reputation: 439

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

Upvotes: 1

Dylan Markow
Dylan Markow

Reputation: 124419

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.

Upvotes: 54

Related Questions