2011-10-30 23:02:41 +00:00
|
|
|
module Locomotive
|
|
|
|
class Site
|
|
|
|
|
|
|
|
include Locomotive::Mongoid::Document
|
|
|
|
|
|
|
|
## Extensions ##
|
|
|
|
extend Extensions::Site::SubdomainDomains
|
|
|
|
extend Extensions::Site::FirstInstallation
|
|
|
|
include Extensions::Shared::Seo
|
|
|
|
|
|
|
|
## fields ##
|
|
|
|
field :name
|
|
|
|
field :robots_txt
|
|
|
|
|
|
|
|
## associations ##
|
2011-11-19 14:47:56 +00:00
|
|
|
references_many :pages, :class_name => 'Locomotive::Page', :validate => false
|
|
|
|
references_many :snippets, :class_name => 'Locomotive::Snippet', :dependent => :destroy, :validate => false
|
|
|
|
references_many :theme_assets, :class_name => 'Locomotive::ThemeAsset', :dependent => :destroy, :validate => false
|
|
|
|
references_many :content_assets, :class_name => 'Locomotive::ContentAsset', :dependent => :destroy, :validate => false
|
|
|
|
references_many :content_types, :class_name => 'Locomotive::ContentType', :dependent => :destroy, :validate => false
|
|
|
|
embeds_many :memberships, :class_name => 'Locomotive::Membership'
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## validations ##
|
|
|
|
validates_presence_of :name
|
|
|
|
|
|
|
|
## callbacks ##
|
2011-11-03 13:01:08 +00:00
|
|
|
after_create :create_default_pages!
|
|
|
|
after_destroy :destroy_pages
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## behaviours ##
|
|
|
|
enable_subdomain_n_domains_if_multi_sites
|
|
|
|
accepts_nested_attributes_for :memberships
|
|
|
|
|
|
|
|
## methods ##
|
|
|
|
|
|
|
|
def all_pages_in_once
|
|
|
|
Page.quick_tree(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def accounts
|
|
|
|
Account.criteria.in(:_id => self.memberships.collect(&:account_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def admin_memberships
|
|
|
|
self.memberships.find_all { |m| m.admin? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_liquid
|
|
|
|
Locomotive::Liquid::Drops::Site.new(self)
|
|
|
|
end
|
|
|
|
|
2011-11-25 14:10:56 +00:00
|
|
|
def to_json
|
|
|
|
Locomotive::SitePresenter.new(self).as_json
|
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def create_default_pages!
|
|
|
|
%w{index 404}.each do |slug|
|
|
|
|
self.pages.create({
|
|
|
|
:slug => slug,
|
|
|
|
:title => I18n.t("attributes.defaults.pages.#{slug}.title"),
|
|
|
|
:raw_template => I18n.t("attributes.defaults.pages.#{slug}.body"),
|
|
|
|
:published => true
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_pages
|
|
|
|
# pages is a tree so we just need to delete the root (as well as the page not found page)
|
|
|
|
self.pages.root.first.try(:destroy) && self.pages.not_found.first.try(:destroy)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|