Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added AccountLocation for helping with HowToUseSubdomainsAsAccountKey…

…s [DHH]
  • Loading branch information...
commit a03839f0eaa050e94a0e3e436941de95f10f8d9a 0 parents
David Heinemeier Hansson authored October 29, 2005
42  README
... ...
@@ -0,0 +1,42 @@
  1
+Account Location
  2
+================
  3
+
  4
+Account location is a set of protected methods that supports the account-key-as-subdomain 
  5
+way of identifying the current scope. These methods allow you to easily produce URLs that
  6
+match this style and to get the current account key from the subdomain.
  7
+
  8
+The methods are: account_url, account_host, and account_domain.
  9
+
  10
+Example:
  11
+
  12
+  class ApplicationController < ActiveRecord::Base
  13
+    include AccountLocation
  14
+  end
  15
+
  16
+  class AccountController < ApplicationController
  17
+    def new
  18
+      @new_account = Account.create(params[:new_account])
  19
+      redirect_to :host => account_host(@new_account.username), :controller => "weblog"
  20
+    end
  21
+    
  22
+    def authenticate
  23
+      session[account_domain] = :authenticated
  24
+      redirect_to :controller => "weblog"
  25
+    end
  26
+    
  27
+    protected
  28
+      def authenticated?
  29
+        session[account_domain] == :authenticated
  30
+      end
  31
+  end
  32
+  
  33
+  # The view:
  34
+  
  35
+  Your domain: <%= account_url %>
  36
+
  37
+By default, all the methods will query for @account.username as the account key, but you can
  38
+specialize that by overwriting default_account_subdomain. You can of course also pass it in
  39
+as the first argument to all the methods.
  40
+
  41
+
  42
+Copyright (c) 2005 David Heinemeier Hansson, released under the MIT license
46  lib/account_location.rb
... ...
@@ -0,0 +1,46 @@
  1
+# Copyright (c) 2005 David Heinemeier Hansson
  2
+#
  3
+# Permission is hereby granted, free of charge, to any person obtaining
  4
+# a copy of this software and associated documentation files (the
  5
+# "Software"), to deal in the Software without restriction, including
  6
+# without limitation the rights to use, copy, modify, merge, publish,
  7
+# distribute, sublicense, and/or sell copies of the Software, and to
  8
+# permit persons to whom the Software is furnished to do so, subject to
  9
+# the following conditions:
  10
+#
  11
+# The above copyright notice and this permission notice shall be
  12
+# included in all copies or substantial portions of the Software.
  13
+#
  14
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21
+module AccountLocation
  22
+  def self.included(controller)
  23
+    controller.helper_method(:account_domain, :account_host, :account_url)
  24
+  end
  25
+
  26
+  protected
  27
+    def default_account_subdomain
  28
+      @account.username if @account && @account.respond_to?(:username)
  29
+    end
  30
+  
  31
+    def account_url(account_subdomain = default_account_subdomain, use_ssl = request.ssl?)
  32
+      (use_ssl ? "https://" : "http://") + account_host(account_subdomain)
  33
+    end
  34
+
  35
+    def account_host(account_subdomain = default_account_subdomain)
  36
+      account_host = ""
  37
+      account_host << account_subdomain + "."
  38
+      account_host << account_domain
  39
+    end
  40
+
  41
+    def account_domain
  42
+      account_domain = ""
  43
+      account_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1
  44
+      account_domain << request.domain + request.port_string
  45
+    end
  46
+end
7  test/account_location_test.rb
... ...
@@ -0,0 +1,7 @@
  1
+require 'test/unit'
  2
+
  3
+class AccountLocationTest < Test::Unit::TestCase
  4
+  def test_truth
  5
+    assert true
  6
+  end
  7
+end

0 notes on commit a03839f

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