aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG2
-rwxr-xr-xactionpack/lib/action_controller/request.rb14
-rw-r--r--actionpack/test/controller/request_test.rb26
3 files changed, 42 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index ab4edf3ba6..4ce21927eb 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added Request#domain (returns string) and Request#subdomains (returns array).
+
* Added POST support for the breakpoint retries, so form processing that raises an exception can be retried with the original request [Florian Gross]
diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb
index 9eb0b04ef0..e9ab3cad90 100755
--- a/actionpack/lib/action_controller/request.rb
+++ b/actionpack/lib/action_controller/request.rb
@@ -50,6 +50,20 @@ module ActionController
return env['REMOTE_ADDR']
end
+ # Returns the domain part of a host, such as rubyonrails.org in "www.rubyonrails.org". You can specify
+ # a different <tt>tld_length</tt>, such as 2 to catch rubyonrails.co.uk in "www.rubyonrails.co.uk".
+ def domain(tld_length = 1)
+ host.split(".").last(1 + tld_length).join(".")
+ end
+
+ # Returns all the subdomains as an array, so ["dev", "www"] would be returned for "dev.www.rubyonrails.org".
+ # You can specify a different <tt>tld_length</tt>, such as 2 to catch ["www"] instead of ["www", "rubyonrails"]
+ # in "www.rubyonrails.co.uk".
+ def subdomains(tld_length = 1)
+ parts = host.split(".")
+ parts - parts.last(1 + tld_length)
+ end
+
def request_uri
env["REQUEST_URI"]
end
diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb
new file mode 100644
index 0000000000..1f38d07a03
--- /dev/null
+++ b/actionpack/test/controller/request_test.rb
@@ -0,0 +1,26 @@
+require File.dirname(__FILE__) + '/../abstract_unit'
+
+class RequestTest < Test::Unit::TestCase
+ def setup
+ @request = ActionController::TestRequest.new
+ end
+
+ def test_domains
+ @request.host = "www.rubyonrails.org"
+ assert_equal "rubyonrails.org", @request.domain
+
+ @request.host = "www.rubyonrails.co.uk"
+ assert_equal "rubyonrails.co.uk", @request.domain(2)
+ end
+
+ def test_subdomains
+ @request.host = "www.rubyonrails.org"
+ assert_equal %w( www ), @request.subdomains
+
+ @request.host = "www.rubyonrails.co.uk"
+ assert_equal %w( www ), @request.subdomains(2)
+
+ @request.host = "dev.www.rubyonrails.co.uk"
+ assert_equal %w( dev www ), @request.subdomains(2)
+ end
+end \ No newline at end of file