Skip to content
This repository
Browse code

Merge pull request #5686 from arunagw/issue_4083

Issue 4083
  • Loading branch information...
commit 8c3ca29d45c7608047d7f2db524893756a60a1cb 2 parents 5cbb20d + c409d06
José Valim authored April 03, 2012
5  actionpack/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,10 @@
1 1
 ## Rails 3.1.4 (unreleased) ##
2 2
 
  3
+*   :subdomain can now be specified with a value of false in url_for, 
  4
+    allowing for subdomain(s) removal from the host during link generation. GH #4083
  5
+
  6
+    *Arun Agrawal*
  7
+
3 8
 *   Skip assets group in Gemfile and all assets configurations options
4 9
     when the application is generated with --skip-sprockets option.
5 10
 
10  actionpack/lib/action_dispatch/http/url.rb
@@ -64,14 +64,16 @@ def rewrite_authentication(options)
64 64
         end
65 65
 
66 66
         def host_or_subdomain_and_domain(options)
67  
-          return options[:host] unless (options[:subdomain] || options[:domain]) && named_host?(options[:host])
  67
+          return options[:host] if !named_host?(options[:host]) || (options[:subdomain].nil? && options[:domain].nil?)
68 68
 
69 69
           tld_length = options[:tld_length] || @@tld_length
70 70
 
71 71
           host = ""
72  
-          host << (options[:subdomain] || extract_subdomain(options[:host], tld_length))
73  
-          host << "."
74  
-          host << (options[:domain]    || extract_domain(options[:host], tld_length))
  72
+          unless options[:subdomain] == false
  73
+            host << (options[:subdomain] || extract_subdomain(options[:host], tld_length)).to_param
  74
+            host << "."
  75
+          end
  76
+          host << (options[:domain] || extract_domain(options[:host], tld_length))
75 77
           host
76 78
         end
77 79
       end
5  actionpack/lib/action_dispatch/routing/url_for.rb
@@ -116,9 +116,10 @@ def url_options
116 116
       #   If <tt>:only_path</tt> is false, this option must be
117 117
       #   provided either explicitly, or via +default_url_options+.
118 118
       # * <tt>:subdomain</tt> - Specifies the subdomain of the link, using the +tld_length+
119  
-      #   to split the domain from the host.
120  
-      # * <tt>:domain</tt> - Specifies the domain of the link, using the +tld_length+
121 119
       #   to split the subdomain from the host.
  120
+      #   If false, removes all subdomains from the host part of the link.
  121
+      # * <tt>:domain</tt> - Specifies the domain of the link, using the +tld_length+
  122
+      #   to split the domain from the host.
122 123
       # * <tt>:tld_length</tt> - Number of labels the TLD id composed of, only used if
123 124
       #   <tt>:subdomain</tt> or <tt>:domain</tt> are supplied. Defaults to
124 125
       #   <tt>ActionDispatch::Http::URL.tld_length</tt>, which in turn defaults to 1.
14  actionpack/test/controller/url_for_test.rb
@@ -71,6 +71,20 @@ def test_subdomain_may_be_changed
71 71
         )
72 72
       end
73 73
 
  74
+      def test_subdomain_may_be_removed
  75
+        add_host!
  76
+        assert_equal('http://basecamphq.com/c/a/i',
  77
+          W.new.url_for(:subdomain => false, :controller => 'c', :action => 'a', :id => 'i')
  78
+        )
  79
+      end
  80
+
  81
+      def test_multiple_subdomains_may_be_removed
  82
+        W.default_url_options[:host] = 'mobile.www.api.basecamphq.com'
  83
+        assert_equal('http://basecamphq.com/c/a/i',
  84
+          W.new.url_for(:subdomain => false, :controller => 'c', :action => 'a', :id => 'i')
  85
+        )
  86
+      end
  87
+
74 88
       def test_subdomain_may_be_accepted_with_numeric_host
75 89
         add_numeric_host!
76 90
         assert_equal('http://127.0.0.1/c/a/i',
1  actionpack/test/dispatch/request_test.rb
@@ -15,6 +15,7 @@ def url_for(options = {})
15 15
 
16 16
     assert_equal 'http://www.example.com',  url_for
17 17
     assert_equal 'http://api.example.com',  url_for(:subdomain => 'api')
  18
+    assert_equal 'http://example.com',      url_for(:subdomain => false)
18 19
     assert_equal 'http://www.ror.com',      url_for(:domain => 'ror.com')
19 20
     assert_equal 'http://api.ror.co.uk',    url_for(:host => 'www.ror.co.uk', :subdomain => 'api', :tld_length => 2)
20 21
     assert_equal 'http://www.example.com:8080',   url_for(:port => 8080)

0 notes on commit 8c3ca29

Please sign in to comment.
Something went wrong with that request. Please try again.