views:

316

answers:

4

If I have a constant in my Rails environment.rb, for example...

SOME_CONSTANT = 3

Is it possible to access this in my capistrano deploy.rb somehow? It seems simple but I can't figure out how.

A: 

Why not define these constants in a file in lib/ and then require the file in both your Rails app and your Capfile?

tadman
A: 

You should access it via the ENV[] hash (this is a Ruby thing), here is an example using the TERM environmental variable.

puts "Your Terminal is #{ENV['TERM']}"

If you need a ruby constant, from your rails environment, you should load it:

require 'config/environment'

Beware that this will load your whole application environment, you should think to use something like AppConfig, or SimpleConfig (insert other tool here) to store configurations, then you need only load the tool, which processes your config files.

Beaks
+2  A: 

This ended up working:

created a file config/initializers/my_constant.rb

put my constant in there (rails automatically loads files there so I can use the constant in my app)

then in deploy.rb added load 'config/initializers/my_constant' so it could be used there as well.

Brian Armstrong
A: 

As the value is not only used by the rails app, I would probably store such configuration information in a language agnostic format (yaml, json, ini, xml) which can be easily parsed by different tools without fear of possible side effects.

tosh