diff --git a/README b/README index 2b819e4..399420d 100644 --- a/README +++ b/README @@ -7,6 +7,9 @@ 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 @@ -15,9 +18,15 @@ Example: protected def find_account - @account = Account.find_by_username(account_subdomain) + 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 @@ -40,7 +49,7 @@ Example: Your domain: <%= account_url %> -By default, all the methods will query for @account.username as the account key, but you can +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. diff --git a/lib/account_location.rb b/lib/account_location.rb index a02d220..13a7854 100644 --- a/lib/account_location.rb +++ b/lib/account_location.rb @@ -1,11 +1,22 @@ module AccountLocation def self.included(controller) - controller.helper_method(:account_domain, :account_subdomain, :account_host, :account_url) + 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.username if @account && @account.respond_to?(:username) + 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?)