组织 Rails 初始化程序的更好方法

发布于 2024-12-09 13:03:55 字数 1065 浏览 0 评论 0原文

在我当前的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

为人所爱 2024-12-16 13:03:55

将环境特定的初始化程序放在 /config/environments/initializers/[env] 下,例如 /config/environments/initializers/development 并将类似的内容添加到 config /application.rb

module YourApp
  class Application < Rails::Application
    # Load environment specific initializers from "config/environments/initializers/[current_env]".
    initializer 'load_environment_initializers', after: :load_config_initializers do |app|
      Dir[File.join(File.dirname(__FILE__), 'environments', 'initializers', Rails.env.to_s, '**', '*.rb')].each {|file| require file }
    end

    ...

 end
end

在完成加载所有常规初始化程序后,它将需要(加载)来自 /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 to config/application.rb:

module YourApp
  class Application < Rails::Application
    # Load environment specific initializers from "config/environments/initializers/[current_env]".
    initializer 'load_environment_initializers', after: :load_config_initializers do |app|
      Dir[File.join(File.dirname(__FILE__), 'environments', 'initializers', Rails.env.to_s, '**', '*.rb')].each {|file| require file }
    end

    ...

 end
end

It will require (load) all files from /config/environments/initializers/[env] and its subdirectories just after it has finished loading all regular initializers.

む无字情书 2024-12-16 13:03:55

您必须进入另一个目录伙伴,初始化程序文件夹中的所有内容都将在启动时包含在内。

如果你把上面的内容放入说..rails_root

/config/env_init_files/developmentrails_root

/config/env_init_files/Production

那么你可以做这样的事情..

#at the end of your environment.rb        
Dir["#{Rails.root}/config/env_init_files/#{Rails.env}/**/*"].each { |initializer| require initializer }

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..

#at the end of your environment.rb        
Dir["#{Rails.root}/config/env_init_files/#{Rails.env}/**/*"].each { |initializer| require initializer }
拧巴小姐 2024-12-16 13:03:55

您可以使用 dotenv gem 并使用环境变量来跨环境更改配置。

You can use dotenv gem and use environment variables to change the config across environemnts.

避讳 2024-12-16 13:03:55

我有一个 Capistrano 食谱可以做到这一点。凭证存储在存储库外部(在名为 /var/secure/ 的文件夹中),并在部署时符号链接到 config/initializers/ 中。

namespace :local do
  desc "Symlink all files in /var/secure into config/initializers.  This is how we get production keys into our apps without hard-coding them.  If they're production-only, great.  If there are development and production credentials, put the development credentials in the repo, and they will be overwritten during deploy."
  task :symlink_secure_initializers, :roles => [:app,:api] do
    run "for l in `ls /var/secure/*.rb`; do 
           rm -f #{release_path}/config/initializers/$(basename $l)
           ln -s /var/secure/$(basename $l) #{release_path}/config/initializers/$(basename $l)
         done"
  end
end

after "deploy:update_code", "local:symlink_secure_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.

namespace :local do
  desc "Symlink all files in /var/secure into config/initializers.  This is how we get production keys into our apps without hard-coding them.  If they're production-only, great.  If there are development and production credentials, put the development credentials in the repo, and they will be overwritten during deploy."
  task :symlink_secure_initializers, :roles => [:app,:api] do
    run "for l in `ls /var/secure/*.rb`; do 
           rm -f #{release_path}/config/initializers/$(basename $l)
           ln -s /var/secure/$(basename $l) #{release_path}/config/initializers/$(basename $l)
         done"
  end
end

after "deploy:update_code", "local:symlink_secure_initializers"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文