在rails中存储应用程序特定配置的最佳方法是什么?

时间:2023-01-14 20:07:18

I need to store app specific configuration in rails. But it has to be:

我需要在rails中存储特定于应用程序的配置。但它必须是:

  • reachable in any file (model, view, helpers and controllers
  • 可在任何文件(模型,视图,帮助器和控制器)中访问

  • environment specified (or not), that means each environment can overwrite the configs specified in environment.rb
  • 指定(或不指定)环境,这意味着每个环境都可以覆盖environment.rb中指定的配置

I've tried to use environment.rb and put something like

我试过使用environment.rb并添加类似的东西

USE_USER_APP = true

that worked to me but when trying to overwrite it in a specific environment it wont work because production.rb, for instance, seems to be inside the Rails:Initializer.run block.

这对我有用,但是当试图在特定环境中覆盖它时它不会工作,因为例如,production.rb似乎在Rails:Initializer.run块中。

So, anyone?

6 个解决方案

#1


4  

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

我帮助一位朋友设置了里卡多昨天提到的解决方案。我们通过加载类似于此的YAML文件(从此处的内存中)来进行一些攻击:

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 the config, like in Rhtml:

这允许他在配置中嵌入Ruby代码,就像在Rhtml中一样:

development:
  path_to_something: <%= RAILS_ROOT %>/config/something.yml

#2


11  

Look at Configatron: http://github.com/markbates/configatron/tree/master

看看Configatron:http://github.com/markbates/configatron/tree/master

I have yet to use it, but he's actively developing it now, and looks quite nice.

我还没有使用它,但他现在正积极开发它,看起来很不错。

#3


4  

The most basic thing to do is to set a class variable from your environment.rb. I've done this for Google Analytics. Essentially I want a different key depending on which environment I'm in so development or staging don't skew the metrics.

最基本的事情是从environment.rb设置一个类变量。我已经为Google Analytics做了这个。基本上我想要一个不同的密钥,具体取决于我所处的环境,因此开发或暂存不会偏差。

This 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 any of the other environment files:

然后在environment.rb或environment / production.rb或任何其他环境文件中:

Analytics::GoogleAnalytics.account_id = "xxxxxxxxx"

Then anywhere you ned to reference, say the default layout with the Google Analytics JavaScript, it you just call Analytics::GoogleAnalytics.account_id.

然后,您需要引用的任何地方,比如Google Analytics JavaScript的默认布局,您只需调用Analytics :: GoogleAnalytics.account_id即可。

#4


0  

I found a good way here

我在这里找到了一个好方法

#5


0  

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

使用环境变量。 Heroku使用这个。请记住,如果您在代码库中保留配置,任何有权访问代码的人都可以访问任何秘密配置(aws api密钥,网关API密钥等)。

daemontool's envdir is a good tool for setting configuration, I'm pretty sure that's what Heroku uses to give application their environment variables.

daemontool的envdir是一个设置配置的好工具,我很确定Heroku使用它来为应用程序提供环境变量。

#6


0  

I have used Rails Settings Cached.

我使用了Rails设置缓存。

It is very simple to use, keeps your configuration values cached and allows you to change them dynamically.

它使用起来非常简单,可以缓存配置值并允许您动态更改它们。

#1


4  

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

我帮助一位朋友设置了里卡多昨天提到的解决方案。我们通过加载类似于此的YAML文件(从此处的内存中)来进行一些攻击:

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 the config, like in Rhtml:

这允许他在配置中嵌入Ruby代码,就像在Rhtml中一样:

development:
  path_to_something: <%= RAILS_ROOT %>/config/something.yml

#2


11  

Look at Configatron: http://github.com/markbates/configatron/tree/master

看看Configatron:http://github.com/markbates/configatron/tree/master

I have yet to use it, but he's actively developing it now, and looks quite nice.

我还没有使用它,但他现在正积极开发它,看起来很不错。

#3


4  

The most basic thing to do is to set a class variable from your environment.rb. I've done this for Google Analytics. Essentially I want a different key depending on which environment I'm in so development or staging don't skew the metrics.

最基本的事情是从environment.rb设置一个类变量。我已经为Google Analytics做了这个。基本上我想要一个不同的密钥,具体取决于我所处的环境,因此开发或暂存不会偏差。

This 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 any of the other environment files:

然后在environment.rb或environment / production.rb或任何其他环境文件中:

Analytics::GoogleAnalytics.account_id = "xxxxxxxxx"

Then anywhere you ned to reference, say the default layout with the Google Analytics JavaScript, it you just call Analytics::GoogleAnalytics.account_id.

然后,您需要引用的任何地方,比如Google Analytics JavaScript的默认布局,您只需调用Analytics :: GoogleAnalytics.account_id即可。

#4


0  

I found a good way here

我在这里找到了一个好方法

#5


0  

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

使用环境变量。 Heroku使用这个。请记住,如果您在代码库中保留配置,任何有权访问代码的人都可以访问任何秘密配置(aws api密钥,网关API密钥等)。

daemontool's envdir is a good tool for setting configuration, I'm pretty sure that's what Heroku uses to give application their environment variables.

daemontool的envdir是一个设置配置的好工具,我很确定Heroku使用它来为应用程序提供环境变量。

#6


0  

I have used Rails Settings Cached.

我使用了Rails设置缓存。

It is very simple to use, keeps your configuration values cached and allows you to change them dynamically.

它使用起来非常简单,可以缓存配置值并允许您动态更改它们。