Shelvacu
Shelvacu

Reputation: 4362

How to set default_url_options in a DRY manner?

I want to set some of the options on config.action_mailer.default_url_options in an initializer. However, the problem is that this value may be nil, or it may be a hash with other options.

Example of the problem when using the reccomended method:

# config/application.rb
config.action_mailer.default_url_options = {protocol: 'https'}

# config/environments/production.rb
config.action_mailer.default_url_options = {host: "example.com"}

# config/environments/development.rb
config.action_mailer.default_url_options = {host: "staging.example.com"}

Now default_url_options doesn't have ssl: true set.

The only way to accommodate both possibilities as far as I can tell is to set it to an empty hash if it's null, then add to the hash like so:

config.action_mailer.default_url_options ||= {}
config.action_mailer.default_url_options[:host] = 'example.com'

This is not dry, and has to be done anywhere you wish to set this option unless you are absolutely sure none of the options have been set before and nobody will attempt to set one of the options in the future in a file which is loaded before this one.

Is there a better way to do this?

Upvotes: 1

Views: 300

Answers (1)

Babar Al-Amin
Babar Al-Amin

Reputation: 3984

You can do something like in env file.

config.action_mailer.default_url_options.merge(host: "staging.example.com")

Upvotes: 1

Related Questions