问题
I have a Rails 3.2.x app with my default timezone set in application.rb as config.time_zone = 'Central Time (US & Canada)'
I will be spinning up new servers in other timezones and need to figure out how to read the timezone configuration from a company.yml file where I set certain company attributes while still using the same github repo for ease of management.
For each server I have the following file in 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))
Then I have a company.yml file which contains the specific settings for each server instance which I call through the app:
: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'
I want to be able to set a timezone configuration from the company.yml file or some configuration file so I can keep my github repo static. I tried the following
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)'
And in my config/application.rb file I attempted the following:
config.time_zone = Company[:company_timezone]
But when I start my server I get:
Invalid Timezone: Company[:company_timezone] (ArgumentError)
So I tried the following in config/application.rb:
config.time_zone = "#{Company[:company_timezone]}"
When I start the server I get uninitialized constant Myapp::Application::Company (NameError)
So my question is, how can I use a company.yml file for each server in different timezones to set the config/application.rb timezone? Do I have syntax errors? Or is there another way to set the timezone from a configuration file into application.rb?
My only other thought is to create multiple git/github repos one for each instance and manually set the timezone in application.rb. That will create a nightmare of having to keep multiple repos going for the same app.
If my question and description is not clear, please let me know. To summarize, I'm trying to set config.time_zone dynamically from a company.yml file that is loaded with an initializer. But I seem to be hitting a wall.
Thanks in advance for any help you can offer or ideas you may have.
回答1:
I think I found a solution.
So in my application.rb file I require yaml and load the company.yml file like so
application.rb excerpt
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]}"
However, when I spin up my server I receive the following error:
config/initializers/company.rb:10: warning: already initialized constant Company
So since I'm loading the company.yml file inapplication.rbwould I need to remove theconfig/initializers/company.rb` ?
回答2:
application.rb is loaded before code in config/initializers. If you're going to load company.yml in application.rb, then you should remove the initializer in config/initializers.
来源:https://stackoverflow.com/questions/26689476/rails-configure-timezone-from-yml-file