How to Have Constants Available Throughout App?

I discovered that you can add these modules under /lib with constants in
them and call these constants like so :

module AdminConstants

email =‘[email protected]
phone =“123456789”

end

and then access them using AdminConstants::email

sometimes I can put in a require “adminConstants” in order to access
them. I am thinknig of putting them all over the place. Whereever there
is any value hardcoded. Can I use this in development.rb?

How about if I wanted different set of constants for different
environments?

like x=1;y=2 in development but x=2,y=3 in production?

Can I have the right files load during startup? Is it possible to
initialize the constants in the lib modules with values from the
database?

Why not add these to a YAML config file? Check out this Railscast

Best.
Mike

You could put the constants in config/environment.rb:

if RAILS_ENV == “development”
EMAIL = “[email protected]
PASSWORD = “1234567890”
else
EMAIL = “[email protected]
PASSWORD = “0987654321”
end

And then just access them as EMAIL and PASSWORD in your application.

Ather S. wrote:

How about if I wanted different set of constants for different
environments?

like x=1;y=2 in development but x=2,y=3 in production?

Can I have the right files load during startup? Is it possible to
initialize the constants in the lib modules with values from the
database?

Here’s what I do:

I have a file config/app_config.rb in which I have all of my settings in
a hash. It looks like this (formatting might be messed up some):

=========================================

=begin
NOTE: Changes to these settings will not take effect until the server
is bounced.

Description of configuration settings
admin_email: where all of the administrative emails go
load_google_analytics: disable/enable Google Analytics
=end

SHARED contains settings that are shared across both environments and

are unlikely to

be different. A good example of this is the google_urchin_path. When

loading the urchin.js

from google’s server, it would be the same for both environments

(whether it should be loaded

in development is another matter). However, if a SHARED value needs to

be overridden, just add

the :key => value pair to the appropriate environment.

SHARED = {
:local_urchin_path => nil,
:google_urchin_path => ‘http://www.google-analytics.com/urchin.js’,
:use_local_urchin => false,
:default_time_zone => ‘Central Time (US & Canada)’
}

CONFIG = {
:development => {
:admin_email => ‘[email protected]’,
:load_google_analytics => false
},
:production => {
:admin_email => ‘[email protected]’,
:load_google_analytics => true
}
}

write the specific environment configuration to a single hash that can

be

accessed easier in the application. With this, we can do

APP_CONFIG[:admin_email]

instead of

CONFIG[RAILS_ENV.to_sym][:admin_email]

APP_CONFIG = SHARED.merge(CONFIG[RAILS_ENV.to_sym])

============================================

In config/environment.rb, I do

require ‘app_config’

which means the constant hash APP_CONFIG will be available anywhere I
need it. So I can do stuff like

class AdminMailer < ActionMailer::Base
def contact_us_email(params)
@subject = “Contact Us: #{params[:contact_subject].strip}”
@recipients = APP_CONFIG[:admin_email]
@cc = params[:contact_email].strip if params[:contact_copy_self]
@from = “#{params[:contact_name].strip}
<#{params[:contact_email].strip}>”
@sent_on = TimeZone.new(APP_CONFIG[:default_time_zone]).now
@headers = {}
@body = params[:contact_body].strip
end
end

This approach works extremely well for me.

Peace,
Phillip