views:

220

answers:

2

I have the following code in /config/initializers/chargify.rb

Chargify.configure do |c|
  c.subdomain = 'example'
  c.api_key   = '123xyz'
end

But I have different settings for development and production.

So, how would I have a different set of variables values based on environment?

+2  A: 

If you're going to need different settings for different environments, it's best to put them in the respective environment file, like config/environments/development.rb.

If you absolutely insist on putting them in an initializer (but please don't, that's what the environment files are for), you can use a case statement and inspect the value of Rails.env, which returns the name of the current environment as a string.

ryeguy
+3  A: 

I would create an config file for this (config/chargify.yml):

development:
  sudomain: example
  api_key: 123abc
production:
  subdomain: production_domain
  api_key: 890xyz

And then change your Initializer like this:

chargify_config_file = File.join(Rails.root,'config','chargify.yml')
raise "#{chargify_config_file} is missing!" unless File.exists? chargify_config_file
chargify_config = YAML.load_file(chargify_config_file)[Rails.env].symbolize_keys

Chargify.configure do |c|
  c.subdomain = chargify_config[:subdomain]
  c.api_key   = chargify_config[:api_key]
end
jigfox