I'm just about to publish a Rails project on GitHub that requires a user renaming and configuring a few YML files or setting environmental variables. The application has a few custom initializers and I'm wondering what the best way to enforce the presence of the above is? Currently I'm using the following snippet in one of my initializers:
# config/initializers/omniauth.rb
config = YAML::load(File.read("#{Rails.root}/config/omniauh.yml"))[Rails.env] rescue {}
config['twitter' ] ||= {}
config['facebook'] ||= {}
twitter_key = config['twitter' ]['key'] || ENV['OMNIAUTH_TWITTER_KEY' ]
twitter_secret = config['twitter' ]['secret'] || ENV['OMNIAUTH_TWITTER_SECRET' ]
facebook_key = config['facebook']['key'] || ENV['OMNIAUTH_FACEBOOK_KEY' ]
facebook_secret = config['facebook']['secret'] || ENV['OMNIAUTH_FACEBOOK_SECRET']
[twitter_key, twitter_secret, facebook_key, facebook_secret].each do |parameter|
throw "Rename and configure 'omniauth.yml.sample'." if parameter.nil? or parameter.empty?
end
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter , twitter_key , twitter_secret
provid开发者_如何转开发er :facebook, facebook_key, facebook_secret
end
Is it standard practice to throw
from initializers or does Rails support a better way of doing this? Thanks!
That's fine. But you may also want to add a call to Rails.logger.fatal
.
That seems perfectly acceptable to me, and is how I'd go about it (except I'd use raise
instead of throw
;P)
精彩评论