Remove duplication between staging.rb and production.rb
ruby, ruby-on-rails, ruby-on-rails-3
Solution
What I've done in the past is have a file that contains the shared settings and then require this in the production and staging environment files. This has worked well because it allows you to define the common settings in one place, and then define the unique settings in the individual files:
config/environments/shared_production.rb
MyApp::Application.configure do
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
end
config/environments/production.rb
require Rails.root.join('config/environments/shared_production')
MyApp::Application.configure do
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :logger
# Url to be used in mailer links
config.action_mailer.default_url_options = { :host => "production.com" }
end
config/environments/staging.rb
require Rails.root.join('config/environments/shared_production')
MyApp::Application.configure do
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Url to be used in mailer links
config.action_mailer.default_url_options = { :host => "mysite-dev.com" }
end
Problem
I have a staging and production environment for my Rails application (running on Heroku). At the moment, there is a lot of stuff in staging.rb and production.rb that I'm having to define separately in each file, e.g.: ``` # Code is not reloaded between requests config.cache_classes = true # Full error reports are disabled and caching is turned on config.consider_all_requests_local = false config.action_controller.perform_caching = true # Disable Rails's static asset server (Apache or nginx will already do this) config.serve_static_assets = false # Compress JavaScripts and CSS config.assets.compress = true # Don't fallback to assets pipeline if a precompiled asset is missed config.assets.compile = false # Generate digests for assets URLs config.assets.digest = true ``` This is not `DRY`. Is there an elegant way that I can effectively import the settings from production.rb into staging.rb and then just override the settings which I wish to change for the staging environment?