Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to organize Rails initializers

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.

like image 226
Mario Uher Avatar asked Oct 14 '25 13:10

Mario Uher


2 Answers

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.

like image 158
schneikai Avatar answered Oct 17 '25 03:10

schneikai


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 }
like image 22
2potatocakes Avatar answered Oct 17 '25 03:10

2potatocakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!