Prev | Current Page 127 | Next

Brad Ediger

"Advanced Rails"

find_by_username(account_subdomain)
end
end
The account_location plugin has no init.rb; nothing needs to be set up on load, as all
functionality is encapsulated in the AccountLocation module. Here is the implementation,
in lib/account_location.rb (minus some license text):
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
Plugin Examples | 87
def account_subdomain
request.subdomains.first
end
end
The self.included method is a standard idiom for plugins; it is triggered after the
module is included in a class. In this case, that method marks the included instance
methods as Rails helper methods, so they can be used from a view.
Finally, remember that Dependencies.


Pages:
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139