组织 Rails 初始化程序的更好方法
在我当前的 Rails 项目中,我最终得到了许多特定于环境的初始化程序,例如我的 rierwave.rb
:
对于开发,我使用类似的东西:
CarrierWave.configure do |config|
config.cache_dir = Rails.root.join('tmp', 'carrierwave')
config.storage = :file
end
对于生产,我通过 fog
使用 S3 code>:
CarrierWave.configure do |config|
config.cache_dir = Rails.root.join('tmp', 'carrierwave')
config.storage = :fog
config.fog_public = false
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: '...',
aws_secret_access_key: '...'
}
end
我不想使用大量 Rails.env.development?
调用来在配置之间切换,并且我不想将此初始化程序存储在我的 environment/* 中.rb
文件。有没有办法,例如在 initializers
目录下为我的每个环境创建一个目录?
initializers
├── development
│ └── carrierwave.rb
├── production
│ └── carrierwave.rb
└── test
└── carrierwave.rb
根据 Rails 指南,问题如下:
如果您愿意,您可以使用子文件夹来组织初始化程序,因为 Rails 将从初始化程序文件夹向下查看整个文件层次结构。
in my current Rails project I ended up with a lot of environment-specific initializers, for example my carrierwave.rb
:
For development I use something like:
CarrierWave.configure do |config|
config.cache_dir = Rails.root.join('tmp', 'carrierwave')
config.storage = :file
end
For production I use S3 through fog
:
CarrierWave.configure do |config|
config.cache_dir = Rails.root.join('tmp', 'carrierwave')
config.storage = :fog
config.fog_public = false
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: '...',
aws_secret_access_key: '...'
}
end
I don't want to use lots of Rails.env.development?
calls to switch between the configs, and I don't want to store this initializers inside my environment/*.rb
files. Is there a way, for example to create a directory for each of my environments under the initializers
directory?
initializers
├── development
│ └── carrierwave.rb
├── production
│ └── carrierwave.rb
└── test
└── carrierwave.rb
The problem according to the Rails guides is following:
You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the initializers folder on down.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将环境特定的初始化程序放在
/config/environments/initializers/[env]
下,例如/config/environments/initializers/development
并将类似的内容添加到config /application.rb
:在完成加载所有常规初始化程序后,它将需要(加载)来自
/config/environments/initializers/[env]
及其子目录的所有文件。Put your environment specific initializers under
/config/environments/initializers/[env]
for example/config/environments/initializers/development
and add something like this toconfig/application.rb
:It will require (load) all files from
/config/environments/initializers/[env]
and its subdirectories just after it has finished loading all regular initializers.您必须进入另一个目录伙伴,初始化程序文件夹中的所有内容都将在启动时包含在内。
如果你把上面的内容放入说..rails_root
/config/env_init_files/developmentrails_root
/config/env_init_files/Production
那么你可以做这样的事情..
You'll have to move into into another directory mate, everything in the initializers folder will get included at boot time.
If you put the above instead into say..
rails_root/config/env_init_files/development
rails_root/config/env_init_files/production
Then you could do something like this..
您可以使用 dotenv gem 并使用环境变量来跨环境更改配置。
You can use dotenv gem and use environment variables to change the config across environemnts.
我有一个 Capistrano 食谱可以做到这一点。凭证存储在存储库外部(在名为 /var/secure/ 的文件夹中),并在部署时符号链接到 config/initializers/ 中。
I have a Capistrano recipe that does this. Credentials are stored outside the repo (in a folder called /var/secure/) and are symlinked into config/initializers/ on deployment.