Skip to content
spanner edited this page Sep 13, 2010 · 34 revisions

This is a fork of the multi_site extension. The main addition is a scoped finder that can be included in any model by calling is_site_scoped, but there are a few other minor changes: the site detection mechanism is shifted to ApplicationController so that a current_site method can be made available alongside current_user, and a couple of useful but temporary email fields have been added to the site model while I work out what to do with site ownership.

Spec stories are included for all the added machinery.


class Forum < ActiveRecord::Base
  is_site_scoped

  # er
  # that's it
end

and has three main effects, all quite simple:

  • a site association is declared and validated
  • an alias_method_chain means that Forum.find(:all) is effectively the same as current_site.forums.find(:all)
  • a before_validation filter sets forum.site ||= current_site

Where current_site is a class method that returns Page.current_site, which has been set in the usual way by the controller’s before_filter :set_current_site (which I’ve moved but not changed).

(In fact the main alias is not :find but :find_every, through which I think all find operations travel, including find_by_ids and all the the attribute-based finders.)

As is usual, if Model.find(...) fails we raise a ActiveRecord::RecordNotFound but Model.find_by_name(...) finds nothing it just returns nil. If a site can’t be found during any of these operations we raise a MultiSite::SiteNotFound exception. Your application probably wants to catch that and redirect to admin/sites. I haven’t really thought that part through yet.

The forum model in this example must have a site_id column, by the way, and if it validates_uniqueness_of anything then you probably want to add :scope => :site_id. Apart from that it just works.

Clone this wiki locally