What is the best way to store application configuration on rails?

I need to save the application configuration in rails. But it should be:

  • available in any file (model, view, helpers and controllers
  • the specified environment (or not), which means that each environment can overwrite the configurations specified in environment.rb

I tried using environment.rb and put something like

USE_USER_APP = true 

which worked for me, but when you try to overwrite it in a certain environment, it will not work, because production.rb, for example, is inside the Rails: Initializer.run block.

So, anyone?

+6
ruby-on-rails configuration
source share
6 answers

I helped a friend set up the solution mentioned by Ricardo yesterday. We hacked it a bit by downloading a YAML file with something similar to this (from memory here):

 require 'ostruct' require 'yaml' require 'erb' #config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml")) config = OpenStruct.new(YAML.load(ERB.new(File.read("#{RAILS_ROOT}/config/config.yml")).result)) env_config = config.send(RAILS_ENV) config.common.update(env_config) unless env_config.nil? ::AppConfig = OpenStruct.new(config.common) 

This allowed him to embed Ruby code in config, as in Rhtml:

 development: path_to_something: <%= RAILS_ROOT %>/config/something.yml 
+4
source share

Take a look at Configatron: http://github.com/markbates/configatron/tree/master

I still need to use it, but it is actively developing it now and it looks good.

+11
source share

The most basic thing to do is set the class variable from environment.rb. I did this for Google Analytics. Essentially, I need another key, depending on which environment I'm in such a development or stage without distorting the metrics.

Here is how I did it.

In lib/analytics/google_analytics.rb :

 module Analytics class GoogleAnalytics @@account_id = nil cattr_accessor :account_id end end 

And then in environment.rb or in environments/production.rb or in any other environment file:

 Analytics::GoogleAnalytics.account_id = "xxxxxxxxx" 

Then, wherever you reference, say, the default layout with the Google Analytics JavaScript, you simply call Analytics::GoogleAnalytics.account_id .

+4
source share

I found a good way here

0
source share

Use environment variables. Heroku uses this. Remember that if you save the configuration in the code base, anyone with access to the code has access to any secret configuration (aws-api keys, api server keys, etc.).

daemontool envdir is a good configuration tool, I'm sure that what Heroku uses to provide its environment variables to applications.

0
source share

I used Rails Settings Cached .

It is very easy to use, saves cached configuration values ​​and allows you to dynamically change them.

0
source share

All Articles