penguincoder edited this page Sep 13, 2010 · 4 revisions
Clone this wiki locally


This is a plugin for separating data inside of the same application. What this really means is if you want something like BaseCamp where each ‘client’ or ‘site’ gets their own copy of data that is completely separate from each other while still residing in the same database, this is the plugin for you. It allows you to mix-and-match restricted and unrestricted models as you please and its hooks will allow for you to maintain your data all at once. You always access the site by something like http://subdomain.example.com and each access is then scoped to the subdomain.

Enables subdomain restrictions by adding a before_filter and helper to access the current subdomain through current_subdomain in the controller.


Takes two arguments: :through and :by. :through should be a class of the model used to represent the subdomain (defaults to Agency) and the :by should be the column name of the field containing the subdomain (defaults to :code).

Working Example

For example, the usage of Agency and :code will work out thusly:

In app/controllers/application.rb (or any other!) add:
use_restricted_subdomains :through => ‘Agency’, :by => :code

1. Request hits http://secksi.example.com/login
2. Subdomain becomes ‘secksi’
3. The corresponding ‘Agency’ with a ‘:code’ of ‘secksi’ becomes the current subdomain. If it’s not found, an ActiveRecord::RecordNotFound is thrown to automatically raise a 404 not found.


This plugin is very similar to the functionality of the account_location plugin written by DHH. There are three basic differences between them, though. This plugin allows for any model and any column, not just @account.username like account_plugin. I also wanted epic failure if a subdomain was not found, not just pretty “uh oh” or a default page. There should be no choice — just finished. The plugin also integrates with the model, you cannot access information outside of your domain for any model tagged with subdomain restrictions. If your users are limited to a subdomain, you cannot in any way access the users from another subdomain simply by typing User.find(params[:random_id]). It should also provide an epic failure.

This plugin provides that kind of separation. It was designed to provide separation of data in a medical application so as to run n different instances of an application in 1 instance of the application, with software restrictions that explicitly and implicitly forbid access outside of your natural subdomain.

Funny story: I actually completely finished this part of the plugin… Then i discovered that account_location existed and did pretty much the same thing without any meta-programming. Good times :)