Prev | Current Page 126 | Next

Brad Ediger

"Advanced Rails"

org Subversion repository. Most of
these plugins are used fairly commonly; many of them are used in 37signals applications.
Consider them the ???standard library??? of Rails. They are all available from http://
svn.rubyonrails.org/rails/plugins/.
Account Location
Plugins can be very simple in structure. For example, consider David Heinemeier
Hansson??™s account_location plugin. This plugin provides controller and helper
methods to support using part of the domain name as an account name (for example,
to support customers with domain names of customer1.example.com and
customer2.example.com, using customer1 and customer2 as keys to look up the
account information). To use the plugin, include AccountLocation in one or more of
your controllers, which adds the appropriate instance methods:
86 | Chapter 3: Rails Plugins
class ApplicationController < ActionController::Base
include AccountLocation
end
puts ApplicationController.instance_methods.grep /^account/
=> ["account_domain", "account_subdomain", "account_host", "account_url"]
Including the AccountLocation module in the controller allows you to access various
URL options from the controller and the view. For example, to set the @account variable
from the subdomain on each request:
class ApplicationController < ActionController::Base
include AccountLocation
before_filter :find_account
protected
def find_account
@account = Account.


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