engine/app/models/locomotive/site.rb

91 lines
2.9 KiB
Ruby
Raw Normal View History

module Locomotive
class Site
include Locomotive::Mongoid::Document
## Extensions ##
extend Extensions::Site::SubdomainDomains
extend Extensions::Site::FirstInstallation
include Extensions::Shared::Seo
include Extensions::Site::Locales
## fields ##
field :name
field :robots_txt
## associations ##
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'
## validations ##
validates_presence_of :name
## callbacks ##
after_create :create_default_pages!
after_destroy :destroy_pages
## behaviours ##
enable_subdomain_n_domains_if_multi_sites
accepts_nested_attributes_for :memberships, :allow_destroy => true
## methods ##
def all_pages_in_once
Page.quick_tree(self)
end
def fetch_page(path, logged_in)
Locomotive::Page.fetch_page_from_path self, path, logged_in
end
def accounts
Account.criteria.in(:_id => self.memberships.map(&:account_id))
end
def admin_memberships
self.memberships.find_all { |m| m.admin? }
end
def to_liquid
Locomotive::Liquid::Drops::Site.new(self)
end
def to_presenter(options = {})
Locomotive::SitePresenter.new(self, options)
end
def as_json(options = {})
self.to_presenter(options).as_json
end
protected
# FIXME: Currently there is no t/translate method on the
# Extensions::Site::I18n module which is breaking the testing. The
# namespaced ::I18n should be replaced by simply I18n when the t()
# method will be available.
def create_default_pages!
%w(index 404).each do |slug|
page = self.pages.build(:title => '', :slug => '', :raw_template => '', :published => true)
self.locales.each do |locale|
page.attributes['slug'][locale] = slug
page.attributes['title'][locale] = ::I18n.t("attributes.defaults.pages.#{slug}.title", :locale => locale)
page.attributes['raw_template'][locale] = ::I18n.t("attributes.defaults.pages.#{slug}.body", :locale => locale)
end
page.save
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