Rails从YML文件配置时区

时间:2014-11-01 12:51:30

标签: ruby-on-rails ruby-on-rails-3 git github timezone

我有一个Rails 3.2.x应用,其默认时区设置为application.rb config.time_zone = 'Central Time (US & Canada)'

我将在其他时区中启动新服务器,并且需要弄清楚如何从company.yml文件中读取时区配置,其中我设置了某些公司属性,同时仍然使用相同的github仓库以便于管理。

对于每个服务器,我在config/initializers/company.rb

中有以下文件
default_company = {
    company_name: "changeme",
    company_phone: "000-000-0000",
    company_email: "default_email@example.net",
    logo_path: "public/logo_changeme.png",
    no_reply_email: "noreply@example.com"
}

file_name = Rails.root.join('config', 'company.yml')
Company = default_company.merge(YAML.load_file(file_name))

然后我有一个company.yml文件,其中包含我通过应用程序调用的每个服务器实例的特定设置:

:company_name: 'John's Company'
:company_phone: '281-314-0000'
:logo_path: 'public/logo.png'
:company_email: 'company@example.net'
:no_reply_email: 'noreply@example.com'

我希望能够从company.yml文件或某个配置文件中设置时区配置,这样我就可以保持我的github repo静态。我尝试了以下

company.yml

:company_name: 'John's Company'
:company_phone: '281-314-0000'
:logo_path: 'public/logo.png'
:company_email: 'company@example.net'
:no_reply_email: 'noreply@example.com'
:company_timezone: 'Eastern Time (US & Canada)'

在我的config/application.rb文件中,我尝试了以下内容:

config.time_zone = Company[:company_timezone]

但是当我启动服务器时,我得到: Invalid Timezone: Company[:company_timezone] (ArgumentError)

所以我在config/application.rb中尝试了以下内容: config.time_zone = "#{Company[:company_timezone]}"

当我启动服务器时,我得到uninitialized constant Myapp::Application::Company (NameError)

所以我的问题是,如何为不同时区的每台服务器使用company.yml文件来设置config/application.rb时区?我有语法错误吗?或者是否有另一种方法将时区从配置文件设置为application.rb

我唯一的另一个想法是为每个实例创建多个git / github repos,并在application.rb中手动设置时区。这将造成一个噩梦,即必须为同一个应用程序保留多个回购。

如果我的问题和描述不明确,请告诉我。总而言之,我正在尝试从加载了初始化程序的config.time_zone文件中动态设置company.yml。但我似乎正在撞墙。

提前感谢您提供的任何帮助或想法。

2 个答案:

答案 0 :(得分:0)

我想我找到了解决方案。

因此,在我的application.rb文件中,我需要yaml并加载company.yml文件,如此

application.rb摘录

require File.expand_path('../boot', __FILE__)
require 'yaml'  
Company = YAML.load(File.read(File.expand_path('../company.yml', __FILE__)))  
require 'rails/all'

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module MyApp
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    # config.autoload_paths += %W(#{config.root}/extras)

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running.
    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
     config.time_zone = "#{Company[:company_timezone]}"

然而,当我启动服务器时,我收到以下错误:

config/initializers/company.rb:10: warning: already initialized constant Company

因为我正在加载company.yml file in application.rb would I need to remove the config / initializers / company.rb`?

答案 1 :(得分:0)

application.rb在config / initializers中的代码之前加载。如果您要在application.rb中加载company.yml,那么您应该删除config / initializers中的初始化程序。