Fix improperly configured host #7415

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+30 −0
Split
@@ -84,4 +84,14 @@ def test_signed_up_with_url
delivered.message_id = '<123@456>'
assert_equal expected.encoded, delivered.encoded
end
+
+ def test_improperly_assigned_options_raise_an_error
+ original_host = UrlTestMailer.default_url_options[:host]
+ UrlTestMailer.default_url_options = {host: 'http://www.basecamphq.com'}
+
+ assert_equal UrlTestMailer.default_url_options[:host], 'www.basecamphq.com'
+ assert_equal UrlTestMailer.default_url_options[:protocol], 'http://'
+ ensure
+ UrlTestMailer.default_url_options[:host] = original_host
+ end
end
View
@@ -1,4 +1,12 @@
## Rails 4.0.0 (unreleased) ##
+
+* Allow action mailer default url options to accept host with protocol such as `http://`
+
+ config.action_mailer.default_url_options = { :host => "http://mydomain.com" }
+
+
+ *Richard Schneeman*
+
* Add :if / :unless conditions to fragment cache:
<%= cache @model, if: some_condition(@model) do %>
@@ -93,6 +93,18 @@ module UrlFor
mattr_writer :default_url_options
end
+ instance_eval do
+ alias :original_default_url_options= :default_url_options=
+
+ # check for improperly formatted options in default_url_options on assignment
+ def default_url_options=(options = {})
+ if options[:host] && match = options[:host].match(/(^.*:\/\/)(.*)/)
+ options[:protocol] ||= match[1]
+ options[:host] = match[2]
+ end
+ self.original_default_url_options = options
+ end
+ end
self.default_url_options = {}
end