My Rails (3.2.21) app sends a lot of email, and it's frequently tested in dev and staging environments. As a result, whenever there's a URL in the email body, the host name needs to match the environment. Example:
- Dev: http://localhost:3000/something
- Staging: http://ift.tt/1S7iZQu
- Production: http://ift.tt/1eobKFP
Currently, I have an initializer in initializers/setup_email.rb
that sets the ActionMailer::Base.default_url_options[:host]
variable depending on the environment (this initializer also sets up other email settings fwiw). Staging for instance is ActionMailer::Base.default_url_options[:host] = "example.staging.com"
.
The dev conditional block however has a :host
AND :port
, so it looks like this:
ActionMailer::Base.default_url_options[:host] = "localhost"
ActionMailer::Base.default_url_options[:port] = 3000
In my mailer classes, I have these ugly conditionals everywhere there's a URL to display, since I need to account for port in dev. Like this:
if Rails.env.production? || Rails.env.staging?
@url = "http://#{ActionMailer::Base.default_url_options[:host]}/something"
elsif Rails.env.development?
@url = "http://#{ActionMailer::Base.default_url_options[:host]}:#{ActionMailer::Base.default_url_options[:port]}/something"
end
What best practice am I missing here? Should I just have the above conditional statement once atop my mailer class before any methods, so I set a @host
variable once and forget it?
Aucun commentaire:
Enregistrer un commentaire