-
Notifications
You must be signed in to change notification settings - Fork 9
/
site.rb
34 lines (30 loc) · 1.01 KB
/
site.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Site < ActiveRecord::Base
belongs_to :page
attr_accessible :name, :page_id, :stylesheet, :hostnames,
:hostnames_attributes
has_many :hostnames,
:dependent => :destroy
accepts_nested_attributes_for :hostnames, :allow_destroy => true
def self.find_by_hostname(hostname)
Site.joins(:hostnames).where(:hostnames=>{:hostname=>hostname}).first ||
Site.joins(:hostnames).where(:hostnames=>{:hostname=>'*'}).first
end
# Monkey-Patch the Page-Controller for loading the right root-Page
PagesController.class_eval do
def home
if (@site)
error_404 unless (@page = Page.find(@site.page_id)).present?
else
error_404 unless (@page = Page.where(:link_url => '/').first).present?
end
end
end
# Monkey-Patch the Application-Controller for loading the current site
ApplicationController.class_eval do
prepend_before_filter :load_site
protected
def load_site
@site = Site.find_by_hostname(request.host)
end
end
end