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
|
|
|
|
2011-04-03 23:59:41 +00:00
|
|
|
## Extensions ##
|
|
|
|
extend Extensions::Site::SubdomainDomains
|
2011-04-05 00:18:17 +00:00
|
|
|
extend Extensions::Site::FirstInstallation
|
2011-06-23 12:57:52 +00:00
|
|
|
include Extensions::Shared::Seo
|
2011-04-03 23:59:41 +00:00
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
## fields ##
|
|
|
|
field :name
|
2011-07-05 20:34:15 +00:00
|
|
|
field :robots_txt
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## associations ##
|
2011-04-27 08:09:19 +00:00
|
|
|
references_many :pages, :validate => false
|
|
|
|
references_many :snippets, :dependent => :destroy, :validate => false
|
|
|
|
references_many :theme_assets, :dependent => :destroy, :validate => false
|
2011-06-17 21:32:54 +00:00
|
|
|
references_many :assets, :dependent => :destroy, :validate => false
|
2011-04-27 08:09:19 +00:00
|
|
|
references_many :content_types, :dependent => :destroy, :validate => false
|
2010-05-10 22:39:52 +00:00
|
|
|
embeds_many :memberships
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-09 09:23:41 +00:00
|
|
|
## validations ##
|
2011-04-05 00:18:17 +00:00
|
|
|
validates_presence_of :name
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-10 15:25:07 +00:00
|
|
|
## callbacks ##
|
2010-05-10 22:39:52 +00:00
|
|
|
after_create :create_default_pages!
|
2010-08-20 15:56:15 +00:00
|
|
|
after_destroy :destroy_pages
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2011-04-03 23:59:41 +00:00
|
|
|
## behaviours ##
|
|
|
|
enable_subdomain_n_domains_if_multi_sites
|
2011-06-25 16:25:31 +00:00
|
|
|
accepts_nested_attributes_for :memberships
|
2011-04-03 23:59:41 +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-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
|
|
|
|
|
|
|
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-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
|