Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gemify #1

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
account_location-*.gem
rails_account_location-*.gem
1 change: 1 addition & 0 deletions Gemfile
@@ -0,0 +1 @@
source :rubygems
30 changes: 0 additions & 30 deletions lib/account_location.rb → MIT-LICENSE
Expand Up @@ -18,33 +18,3 @@
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
module AccountLocation
def self.included(controller)
controller.helper_method(:account_domain, :account_subdomain, :account_host, :account_url)
end

protected
def default_account_subdomain
@account.username if @account && @account.respond_to?(:username)
end

def account_url(account_subdomain = default_account_subdomain, use_ssl = request.ssl?)
(use_ssl ? "https://" : "http://") + account_host(account_subdomain)
end

def account_host(account_subdomain = default_account_subdomain)
account_host = ""
account_host << account_subdomain + "."
account_host << account_domain
end

def account_domain
account_domain = ""
account_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1
account_domain << request.domain + request.port_string
end

def account_subdomain
request.subdomains.first
end
end
48 changes: 0 additions & 48 deletions README

This file was deleted.

57 changes: 57 additions & 0 deletions README.md
@@ -0,0 +1,57 @@
Account Location
================

Account location is a set of protected methods that supports the account-key-as-subdomain
way of identifying the current scope. These methods allow you to easily produce URLs that
match this style and to get the current account key from the subdomain.

The methods are: `account_url`, `account_host`, and `account_domain`.

current_account can be used to get and set the currently active account.


Example:

class ApplicationController < ActionController::Base
include AccountLocation
before_filter :find_account

protected
def find_account
self.current_account = Account.find_by_username(account_subdomain)
end
end

class UsersController < ApplicationController
def index
@users = current_account.users.all
end
end

class AccountController < ApplicationController
def new
@new_account = Account.create(params[:new_account])
redirect_to :host => account_host(@new_account.username), :controller => "weblog"
end

def authenticate
session[account_domain] = :authenticated
redirect_to :controller => "weblog"
end

protected
def authenticated?
session[account_domain] == :authenticated
end
end

And in your view:

Your domain: <%= account_url %>

By default, all the methods will query for current_account.username as the account key, but you can
specialize that by overwriting default_account_subdomain. You can of course also pass it in
as the first argument to all the methods.


Copyright (c) 2005 David Heinemeier Hansson, released under the MIT license
26 changes: 26 additions & 0 deletions account_location.gemspec
@@ -0,0 +1,26 @@
# -*- encoding: utf-8 -*-

$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)

version = "1.0.1"

Gem::Specification.new do |s|
s.name = "rails_account_location"
s.version = version
s.platform = Gem::Platform::RUBY
s.authors = ["David Heinemeier Hansson"]
s.email = "david@37signals.com"
s.homepage = "http://github.com/rails/account_location"
s.summary = "Module that supports account-key-as-subdomain pattern"
s.description = "Account location is a set of protected methods that supports the account-key-as-subdomain way of identifying the current scope."

s.rubygems_version = "1.3.7"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {spec}/*`.split("\n")
s.extra_rdoc_files = [ "README.md" ]
s.rdoc_options = ["--charset=UTF-8"]
s.require_path = "lib"

s.add_runtime_dependency "rails"
end
41 changes: 41 additions & 0 deletions lib/rails_account_location.rb
@@ -0,0 +1,41 @@
module AccountLocation
def self.included(controller)
controller.helper_method(:account_domain, :account_subdomain, :account_host, :account_url,
:current_account)
end

protected

def current_account
@current_account
end

def current_account=(account)
@current_account = account
end

def default_account_subdomain
account = current_account || @account
account.username if account && account.respond_to?(:username)
end

def account_url(account_subdomain = default_account_subdomain, use_ssl = request.ssl?)
(use_ssl ? "https://" : "http://") + account_host(account_subdomain)
end

def account_host(account_subdomain = default_account_subdomain)
account_host = ""
account_host << account_subdomain + "."
account_host << account_domain
end

def account_domain
account_domain = ""
account_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1
account_domain << request.domain + request.port_string
end

def account_subdomain
request.subdomains.first
end
end