Skip to content

Commit 966ea31

Browse files
author
gabriel.d
committed
Move multisite into trunk
git-svn-id: https://rails-multisite.googlecode.com/svn/trunk@13 64db17d9-5632-0410-b012-75deaefb9678
1 parent 9a30851 commit 966ea31

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

multisite/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2007 Gabe da Silveira, based on code from theme_support by Matt McCray, based on code from Typo by Tobias Luetke
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

multisite/README

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
= Multisite for Rails Applications
2+
3+
This plugin provides basic support for hosting multiple sites on the same Rails code base. It is based on Matt McCray's theme_support plugin, but is essentially completely gutted and rewritten with a different focus.
4+
5+
The essential functionality of the plugin is to add a specific site's views to the view path. The concept of a view_path is new to Rails 2.0.
6+
7+
== Requirements
8+
9+
* This plugin is tested in Rails 2.0.2.
10+
* Template caching must be disabled (this includes both cache_template_loading and cache_template_extensions action_view config options)
11+
12+
== Differences from theme_support
13+
14+
If you are familiar with theme_support, a 'site' is equivalent to a 'theme' in the old system. The big difference is that this plugin doesn't make any arrangements for presenting public files. Although a public directory is created by the generator, this can just as easily be a symlink to another directory. The intended use is to configure Apache to provide access to static files for each domain, possibly with a rewrite rule falling back to the main Rails public directory. This approach was chosen because the static file caching in theme_support required long paths to static files that should be unnecessary when they are created on a per-domain basis. By letting Apache handle the specifics, static files are much simpler (no image URL coupling to the theme name, or cache expiration problems), and converting raw HTML mockups from designers tends to be much easier as well.
15+
16+
While theme_support only officially worked through Rails 1.1, and 1.2 with some 3rd party patches (Matt where are you?), this plugin requires Rails 2.0 for the Multiple View Path functionality (http://weblog.rubyonrails.com/2007/2/4/new-feature-for-rails-2-0-multiple-controller-view-paths). This addition to Rails makes much of the remaining complexity of the theme_support plugin disappear. One big bonus is that templates can now be created on a per-site basis without a parent template having to exist in the base directory. This is handy for creating extra content pages or breaking things down into partials.
17+
18+
== Usage
19+
20+
You can use the site_generator to create the file structure needed, or create it
21+
yourself.
22+
23+
It expects the following site folder structure.
24+
25+
$app_root
26+
sites/
27+
[site_name]
28+
views/ <- you can override application views
29+
public/ <- intended to be the DOCUMENT_ROOT of the site
30+
31+
You specify which site to use in your controller by using
32+
the declarative 'site' syntax. Although this could be automated based on domain
33+
name, the relationship between domains and sites is not necessarily 1:1, so you will
34+
need to develop a mechanism for choosing a 'site'.
35+
36+
class ApplicationController < ActionController::Base
37+
site 'blue_bird'
38+
39+
...
40+
end
41+
42+
You can also defer the theme lookup to a controller method:
43+
44+
class ApplicationController < ActionController::Base
45+
site :get_site
46+
47+
def get_site
48+
domain = Domain.find_by_name(request.domain)
49+
domain.theme
50+
end
51+
52+
...
53+
end
54+
55+
56+
Note: By setting the site in the ApplicationController you can set
57+
the site for the whole application.
58+
59+
== Changelog
60+
61+
1.0.0 - Initial release
62+
63+
64+
---
65+
Copyright (c) 2007-2008 Gabe da Silveira, based on code from theme_support by Matt McCray, based on code from Typo by Tobias Luetke
66+
released under the MIT license

multisite/generators/site/USAGE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
NAME
2+
site - Creates the folder structure for a new site using multisite
3+
4+
SYNOPSIS
5+
site [site_name]
6+
7+
DESCRIPTION
8+
This generator creates the folder structure for a new site. It creates one folder for views,
9+
and one for any public files. The public files can be setup as the DOCUMENT_ROOT in Apache
10+
with a redirect to the base public directory.
11+
12+
EXAMPLE
13+
./script/generate site domain
14+
15+
This will generate the file structure for a site named 'domain'.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class SiteGenerator < Rails::Generator::NamedBase
2+
3+
def manifest
4+
record do |m|
5+
# Site folder(s)
6+
m.directory File.join( "sites", file_name )
7+
# Site content folders
8+
m.directory File.join( "sites", file_name, "views" )
9+
m.directory File.join( "sites", file_name, "public" )
10+
end
11+
end
12+
end

multisite/init.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Initializes multisite support by adding a macro to ActionController::Base
2+
require 'patches/actioncontroller_ex'
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Extend the Base ActionController to support multiple site
2+
ActionController::Base.class_eval do
3+
4+
attr_accessor :current_site
5+
6+
# Use this in your controller just like the <tt>layout</tt> macro.
7+
# Example:
8+
#
9+
# site 'maybe_domain'
10+
#
11+
# -or-
12+
#
13+
# site :get_site
14+
#
15+
# def get_site
16+
# 'maybe_domain'
17+
# end
18+
def self.site(site_name)
19+
write_inheritable_attribute "site", site_name
20+
before_filter :add_multisite_path
21+
end
22+
23+
# Retrieves the current set site
24+
def current_site(passed_site=nil)
25+
site = passed_site || self.class.read_inheritable_attribute("site")
26+
27+
@active_site = case site
28+
when Symbol then send(site)
29+
when Proc then site.call(self)
30+
when String then site
31+
end
32+
end
33+
34+
protected
35+
def add_multisite_path
36+
if current_site
37+
raise "Multisite plugin is incompatible with template caching. You must set config.action_view.cache_template_loading to false in your environment." if ActionView::Base.cache_template_loading
38+
raise "Multisite plugin is incompatible with template extension caching. You must set config.action_view.cache_template_extensions to false in your environment." if ActionView::Base.cache_template_extensions
39+
new_path = File.join(RAILS_ROOT, 'sites', @active_site, 'views')
40+
@template.prepend_view_path(new_path)
41+
logger.info " Template View Paths: #{@template.view_paths.inspect}"
42+
end
43+
return true
44+
end
45+
end

0 commit comments

Comments
 (0)