2010-06-01 00:06:46 +00:00
|
|
|
class Site
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
include Locomotive::Mongoid::Document
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
## fields ##
|
|
|
|
field :name
|
2010-06-10 14:50:50 +00:00
|
|
|
field :subdomain
|
2010-04-09 09:23:41 +00:00
|
|
|
field :domains, :type => Array, :default => []
|
2010-06-16 14:43:29 +00:00
|
|
|
field :meta_keywords
|
|
|
|
field :meta_description
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## associations ##
|
2010-07-31 02:15:24 +00:00
|
|
|
references_many :pages
|
2010-08-20 15:56:15 +00:00
|
|
|
references_many :snippets, :dependent => :destroy
|
|
|
|
references_many :theme_assets, :dependent => :destroy
|
|
|
|
references_many :asset_collections, :dependent => :destroy
|
|
|
|
references_many :content_types, :dependent => :destroy
|
2010-05-10 22:39:52 +00:00
|
|
|
embeds_many :memberships
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-09-15 16:09:07 +00:00
|
|
|
## indexes
|
|
|
|
index :domains
|
|
|
|
|
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
|
2010-07-22 00:18:14 +00:00
|
|
|
validate :domains_must_be_valid_and_unique
|
|
|
|
|
2010-04-10 15:25:07 +00:00
|
|
|
## 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-08-20 15:56:15 +00:00
|
|
|
after_destroy :destroy_pages
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-10 15:25:07 +00:00
|
|
|
## named scopes ##
|
2010-08-20 15:56:15 +00:00
|
|
|
scope :match_domain, lambda { |domain| { :any_in => { :domains => [*domain] } } }
|
|
|
|
scope :match_domain_with_exclusion_of, lambda { |domain, site|
|
|
|
|
{ :any_in => { :domains => [*domain] }, :where => { :_id.ne => site.id } }
|
|
|
|
}
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
## methods ##
|
2010-04-13 13:24:12 +00:00
|
|
|
|
2011-01-02 22:58:06 +00:00
|
|
|
def all_pages_in_once
|
|
|
|
Page.quick_tree(self)
|
|
|
|
end
|
|
|
|
|
2010-12-14 17:00:12 +00:00
|
|
|
def domains=(array)
|
|
|
|
array = [] if array.blank?; super(array)
|
|
|
|
end
|
|
|
|
|
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-07-22 00:18:14 +00:00
|
|
|
|
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-07-22 00:18:14 +00:00
|
|
|
|
2010-04-10 15:25:07 +00:00
|
|
|
def add_subdomain_to_domains
|
2010-07-22 00:18:14 +00:00
|
|
|
self.domains ||= []
|
2010-10-29 14:19:41 +00:00
|
|
|
(self.domains << self.full_subdomain).uniq!
|
2010-04-10 15:25:07 +00:00
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-10 15:25:07 +00:00
|
|
|
def domains_without_subdomain
|
2010-10-29 14:19:41 +00:00
|
|
|
(self.domains || []) - [self.full_subdomain]
|
2010-04-09 09:23:41 +00:00
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def domains_with_subdomain
|
2010-10-29 14:19:41 +00:00
|
|
|
((self.domains || []) + [self.full_subdomain]).uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_subdomain
|
|
|
|
"#{self.subdomain}.#{Locomotive.config.default_domain}"
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
|
|
|
def to_liquid
|
|
|
|
Locomotive::Liquid::Drops::Site.new(self)
|
|
|
|
end
|
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
protected
|
2010-07-22 00:18:14 +00:00
|
|
|
|
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?
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
self.domains_without_subdomain.each do |domain|
|
2010-08-20 15:56:15 +00:00
|
|
|
if self.class.match_domain_with_exclusion_of(domain, self).any?
|
2010-04-10 15:25:07 +00:00
|
|
|
self.errors.add(:domains, :domain_taken, :value => domain)
|
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-10 15:25:07 +00:00
|
|
|
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-07-22 00:18:14 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def create_default_pages!
|
|
|
|
%w{index 404}.each do |slug|
|
|
|
|
self.pages.create({
|
2010-07-22 00:18:14 +00:00
|
|
|
:slug => slug,
|
|
|
|
:title => I18n.t("attributes.defaults.pages.#{slug}.title"),
|
2010-08-21 22:48:24 +00:00
|
|
|
:raw_template => I18n.t("attributes.defaults.pages.#{slug}.body"),
|
2010-07-31 02:18:01 +00:00
|
|
|
:published => true
|
2010-05-10 22:39:52 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-08-20 15:56:15 +00:00
|
|
|
def destroy_pages
|
|
|
|
# pages is a tree so we just need to delete the root (as well as the page not found page)
|
2011-01-27 09:49:42 +00:00
|
|
|
self.pages.root.first.try(:destroy) && self.pages.not_found.first.try(:destroy)
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|