2011-10-30 23:02:41 +00:00
|
|
|
module Locomotive
|
|
|
|
class Site
|
|
|
|
|
|
|
|
include Locomotive::Mongoid::Document
|
|
|
|
|
|
|
|
## Extensions ##
|
2012-01-10 01:24:34 +00:00
|
|
|
extend Extensions::Site::SubdomainDomains
|
|
|
|
extend Extensions::Site::FirstInstallation
|
2011-10-30 23:02:41 +00:00
|
|
|
include Extensions::Shared::Seo
|
2012-01-26 01:33:39 +00:00
|
|
|
include Extensions::Site::Locales
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## 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
|
2011-11-29 01:24:02 +00:00
|
|
|
accepts_nested_attributes_for :memberships, :allow_destroy => true
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## methods ##
|
|
|
|
|
|
|
|
def all_pages_in_once
|
|
|
|
Page.quick_tree(self)
|
|
|
|
end
|
|
|
|
|
2012-03-26 14:31:28 +00:00
|
|
|
def fetch_page(path, logged_in)
|
|
|
|
Locomotive::Page.fetch_page_from_path self, path, logged_in
|
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
def accounts
|
2012-01-04 01:07:53 +00:00
|
|
|
Account.criteria.in(:_id => self.memberships.map(&:account_id))
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def admin_memberships
|
|
|
|
self.memberships.find_all { |m| m.admin? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_liquid
|
|
|
|
Locomotive::Liquid::Drops::Site.new(self)
|
|
|
|
end
|
|
|
|
|
2012-04-26 01:39:23 +00:00
|
|
|
def to_presenter(options = {})
|
|
|
|
Locomotive::SitePresenter.new(self, options)
|
|
|
|
end
|
|
|
|
|
2011-11-27 16:22:54 +00:00
|
|
|
def as_json(options = {})
|
2012-04-26 01:39:23 +00:00
|
|
|
self.to_presenter(options).as_json
|
2011-11-25 14:10:56 +00:00
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
protected
|
|
|
|
|
2012-04-10 11:55:59 +00:00
|
|
|
# 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.
|
2011-10-30 23:02:41 +00:00
|
|
|
def create_default_pages!
|
2012-04-10 11:55:59 +00:00
|
|
|
%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)
|
2012-01-26 15:51:32 +00:00
|
|
|
end
|
2012-04-10 11:55:59 +00:00
|
|
|
|
|
|
|
page.save
|
2011-10-30 23:02:41 +00:00
|
|
|
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
|