简体   繁体   English

从DB中提取一些配置变量以在Rails环境中动态设置?

[英]Pull some configuration variables from the DB to set dynamically in Rails environment?

Is it possible to set some configuration variables in production.rb dynamically using values from the database? 是否可以使用数据库中的值动态设置production.rb一些配置变量?

I'm building a multi-tenant app and each tenant has some environment specific information that needs to be set dynamically. 我正在构建一个多租户应用程序,每个租户都有一些需要动态设置的特定于环境的信息。

For example: 例如:

// production.rb
config.action_mailer.default_url_options  = { :host => current_tenant.domain }

The current_tenant is a helper method is defined in ApplicationHelper so can't be accessed in production.rb current_tenant是一个辅助方法,在ApplicationHelper定义,因此无法在production.rb访问

Here is current_tenant : 这是current_tenant

// application_helper.rb
def current_tenant
  @current_tenant ||= Tenant.find_by(domain: Apartment::Tenant.current)
end

If not, is it possible to dynamically create secrets.yml using DB data? 如果没有,是否可以使用DB数据动态创建secrets.yml Since then I would be able to use ENV in production.rb 从那以后我就可以在production.rb使用ENV

Maybe you can try this instead: Create a helper method for your emails: 也许您可以尝试这样做:为您的电子邮件创建一个帮助方法:

def build_tenant_url(route_name, *args)
  opts = args.extract_options!
  opts.merge!(host: @tenant ? @tenant.domain : your_domain)
  #Here you can merge other options if you need to pass a token etc
  Rails.application.routes_url_helpers.send(:"#{route_name}_url", *args, opts)
end

In your mailer, define the @tenant 在邮件程序中,定义@tenant

Then in your email view use it 然后在您的电子邮件视图中使用它

= link_to 'Edit', build_tenant_url(:edit_tenant, @tenant) 

This setup runs when the app starts up, so if you want an option to be driven by some variable, that variable will need to be available to the Ruby code that runs at startup. 此应用程序在应用程序启动时运行,因此如果您希望某个选项由某个变量驱动,则该变量将需要可用于启动时运行的Ruby代码。 There's various ways this could be done 有各种方法可以做到这一点

  • the script (or whatever) which starts your rails server passes through a parameter 启动rails服务器的脚本(或其他)通过参数传递
  • a value is written into a text file by some other process and read out again by the startup script 某个其他进程将值写入文本文件,并由启动脚本再次读出
  • same again but with an environment variable 同样,但环境变量

For example, if you were giving each tenant (group of users) their own subdomain on your app, then the subdomain name could be used as a key for the startup code to read the database and pull out some details for that tenant. 例如,如果您在应用程序中为每个租户(用户组)提供了自己的子域,则子域名可以用作启动代码的键,以读取数据库并为该租户提取一些详细信息。 This subdomain would then be passed through to the app when you spin it up. 当你旋转它时,这个子域将被传递到应用程序。

EDIT - having said all this, i'm not sure if it's necessary for you to change the startup config at all. 编辑 - 说了这一切,我不确定你是否有必要改变启动配置。 Can't you just look up the appropriate domain name whenever you send an email, rather than changing the default set in the config? 您是否只能在发送电子邮件时查找相应的域名,而不是更改配置中的默认设置?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM