engine/app/models/site.rb

69 lines
1.6 KiB
Ruby
Raw Normal View History

class Site
include Locomotive::Mongoid::Document
## Extensions ##
extend Extensions::Site::SubdomainDomains
extend Extensions::Site::FirstInstallation
2010-04-09 09:23:41 +00:00
## fields ##
field :name
field :meta_keywords
field :meta_description
2010-04-25 00:33:38 +00:00
## associations ##
references_many :pages, :validate => false
references_many :snippets, :dependent => :destroy, :validate => false
references_many :theme_assets, :dependent => :destroy, :validate => false
references_many :asset_collections, :dependent => :destroy, :validate => false
references_many :content_types, :dependent => :destroy, :validate => false
embeds_many :memberships
2010-04-09 09:23:41 +00:00
## validations ##
validates_presence_of :name
2010-04-10 15:25:07 +00:00
## callbacks ##
after_create :create_default_pages!
after_destroy :destroy_pages
## behaviours ##
enable_subdomain_n_domains_if_multi_sites
2010-04-09 09:23:41 +00:00
## 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
2010-04-09 09:23: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