2010-04-09 09:23:41 +00:00
|
|
|
class Site
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
|
|
|
|
## fields ##
|
|
|
|
field :name
|
2010-04-10 15:25:07 +00:00
|
|
|
field :subdomain, :type => String
|
2010-04-09 09:23:41 +00:00
|
|
|
field :domains, :type => Array, :default => []
|
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## associations ##
|
|
|
|
has_many_related :pages
|
2010-05-02 23:33:17 +00:00
|
|
|
has_many_related :layouts
|
|
|
|
has_many_related :snippets
|
2010-05-11 21:38:52 +00:00
|
|
|
has_many_related :theme_assets
|
2010-05-12 00:16:39 +00:00
|
|
|
has_many_related :asset_collections
|
2010-05-10 22:39:52 +00:00
|
|
|
embeds_many :memberships
|
2010-04-25 00:33:38 +00:00
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
## validations ##
|
2010-04-10 15:25:07 +00:00
|
|
|
validates_presence_of :name, :subdomain
|
|
|
|
validates_uniqueness_of :subdomain
|
2010-04-11 23:59:18 +00:00
|
|
|
validates_exclusion_of :subdomain, :in => Locomotive.config.reserved_subdomains
|
2010-04-10 15:25:07 +00:00
|
|
|
validates_format_of :subdomain, :with => Locomotive::Regexps::SUBDOMAIN, :allow_blank => true
|
|
|
|
validate :domains_must_be_valid_and_unique
|
|
|
|
|
|
|
|
## callbacks ##
|
2010-05-10 22:39:52 +00:00
|
|
|
after_create :create_default_pages!
|
2010-04-10 15:25:07 +00:00
|
|
|
before_save :add_subdomain_to_domains
|
2010-05-10 22:39:52 +00:00
|
|
|
after_destroy :destroy_in_cascade!
|
2010-04-10 15:25:07 +00:00
|
|
|
|
|
|
|
## named scopes ##
|
|
|
|
named_scope :match_domain, lambda { |domain| { :where => { :domains => domain } } }
|
2010-05-10 22:39:52 +00:00
|
|
|
named_scope :match_domain_with_exclusion_of, lambda { |domain, site| { :where => { :domains => domain, :_id.ne => site.id } } }
|
2010-04-09 09:23:41 +00:00
|
|
|
|
|
|
|
## behaviours ##
|
|
|
|
|
|
|
|
## methods ##
|
2010-04-13 13:24:12 +00:00
|
|
|
|
|
|
|
def accounts
|
2010-05-10 22:39:52 +00:00
|
|
|
Account.criteria.in(:_id => self.memberships.collect(&:account_id))
|
2010-04-13 13:24:12 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def admin_memberships
|
|
|
|
self.memberships.find_all { |m| m.admin? }
|
2010-04-13 13:24:12 +00:00
|
|
|
end
|
2010-04-09 09:23:41 +00:00
|
|
|
|
2010-04-10 15:25:07 +00:00
|
|
|
def add_subdomain_to_domains
|
|
|
|
self.domains ||= []
|
2010-04-11 23:59:18 +00:00
|
|
|
(self.domains << "#{self.subdomain}.#{Locomotive.config.default_domain}").uniq!
|
2010-04-10 15:25:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def domains_without_subdomain
|
2010-04-11 23:59:18 +00:00
|
|
|
(self.domains || []) - ["#{self.subdomain}.#{Locomotive.config.default_domain}"]
|
2010-04-09 09:23:41 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def domains_with_subdomain
|
|
|
|
((self.domains || []) + ["#{self.subdomain}.#{Locomotive.config.default_domain}"]).uniq
|
|
|
|
end
|
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
protected
|
|
|
|
|
2010-04-10 15:25:07 +00:00
|
|
|
def domains_must_be_valid_and_unique
|
2010-05-10 22:39:52 +00:00
|
|
|
return if self.domains.empty?
|
|
|
|
|
|
|
|
self.domains_without_subdomain.each do |domain|
|
|
|
|
if not self.class.match_domain_with_exclusion_of(domain, self).empty?
|
2010-04-10 15:25:07 +00:00
|
|
|
self.errors.add(:domains, :domain_taken, :value => domain)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not domain =~ Locomotive::Regexps::DOMAIN
|
|
|
|
self.errors.add(:domains, :invalid_domain, :value => domain)
|
|
|
|
end
|
|
|
|
end
|
2010-04-09 09:23:41 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def create_default_pages!
|
|
|
|
%w{index 404}.each do |slug|
|
|
|
|
self.pages.create({
|
|
|
|
:slug => slug,
|
|
|
|
:title => I18n.t("attributes.defaults.pages.#{slug}.title"),
|
|
|
|
:body => I18n.t("attributes.defaults.pages.#{slug}.body")
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_in_cascade!
|
2010-05-12 00:16:39 +00:00
|
|
|
%w{pages layouts snippets theme_assets asset_collections}.each do |association|
|
2010-05-10 22:39:52 +00:00
|
|
|
self.send(association).destroy_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
end
|