forked from radiant/radiant-multi-site-extension
/
site.rb
43 lines (36 loc) · 1.18 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
35
36
37
38
39
40
41
42
43
class Site < ActiveRecord::Base
has_many :users
has_many :layouts
acts_as_list
order_by "position ASC"
class << self
def find_for_host(hostname = '')
default, normal = find(:all).partition {|s| s.domain.blank? }
matching = normal.find do |site|
hostname == site.base_domain || hostname =~ Regexp.compile(site.domain)
end
matching || default.first
end
end
belongs_to :homepage, :class_name => "Page", :foreign_key => "homepage_id"
validates_presence_of :name, :base_domain
validates_uniqueness_of :domain
after_create :create_homepage
def url(path = "/")
uri = URI.join("http://#{self.base_domain}", path)
uri.to_s
end
def dev_url(path = "/")
uri = URI.join("http://#{Radiant::Config['dev.host']|| 'dev'}.#{self.base_domain}", path)
uri.to_s
end
def create_homepage
if self.homepage_id.blank?
self.homepage = self.build_homepage(:title => "#{self.name} Homepage",
:slug => "#{self.name.slugify}", :breadcrumb => "Home",
:status => Status[:draft])
self.homepage.parts << PagePart.new(:name => "body", :content => "")
save
end
end
end