Passing subdomain: '' to url_for removes the subdomain (instead of adding a leading .)

Adding a boolean route constraint checks for presence/absence of request property
This commit is contained in:
Derek Watson 2013-04-11 22:12:19 -04:00 committed by Andrew White
parent 296830ed0f
commit 6183e1a460
4 changed files with 32 additions and 1 deletions

@ -100,7 +100,7 @@ def host_or_subdomain_and_domain(options)
tld_length = options[:tld_length] || @@tld_length tld_length = options[:tld_length] || @@tld_length
host = "" host = ""
unless options[:subdomain] == false unless options[:subdomain] == false || options[:subdomain] == ''
host << (options[:subdomain] || extract_subdomain(options[:host], tld_length)).to_param host << (options[:subdomain] || extract_subdomain(options[:host], tld_length)).to_param
host << "." host << "."
end end

@ -102,6 +102,10 @@ def matches?(request)
value === request.send(method).to_s value === request.send(method).to_s
when Array when Array
value.include?(request.send(method)) value.include?(request.send(method))
when TrueClass
request.send(method).present?
when FalseClass
request.send(method).blank?
else else
value === request.send(method) value === request.send(method)
end end

@ -89,6 +89,13 @@ def test_subdomain_may_be_removed
) )
end end
def test_subdomain_may_be_removed_with_blank_string
W.default_url_options[:host] = 'api.basecamphq.com'
assert_equal('http://basecamphq.com/c/a/i',
W.new.url_for(:subdomain => '', :controller => 'c', :action => 'a', :id => 'i')
)
end
def test_multiple_subdomains_may_be_removed def test_multiple_subdomains_may_be_removed
W.default_url_options[:host] = 'mobile.www.api.basecamphq.com' W.default_url_options[:host] = 'mobile.www.api.basecamphq.com'
assert_equal('http://basecamphq.com/c/a/i', assert_equal('http://basecamphq.com/c/a/i',

@ -3322,6 +3322,10 @@ class TestUrlConstraints < ActionDispatch::IntegrationTest
end end
get '/' => ok, :as => :alternate_root, :constraints => { :port => 8080 } get '/' => ok, :as => :alternate_root, :constraints => { :port => 8080 }
get '/search' => ok, :constraints => { :subdomain => false }
get '/logs' => ok, :constraints => { :subdomain => true }
end end
end end
@ -3348,6 +3352,22 @@ def app; Routes end
get 'http://www.example.com:8080/' get 'http://www.example.com:8080/'
assert_response :success assert_response :success
end end
test "false constraint expressions check for absence of values" do
get 'http://example.com/search'
assert_response :success
get 'http://api.example.com/search'
assert_response :not_found
end
test "true constraint expressions check for presence of values" do
get 'http://api.example.com/logs'
assert_response :success
get 'http://example.com/logs'
assert_response :not_found
end
end end
class TestInvalidUrls < ActionDispatch::IntegrationTest class TestInvalidUrls < ActionDispatch::IntegrationTest