2010-06-01 00:06:46 +00:00
|
|
|
class Page
|
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
|
|
|
## Extensions ##
|
2011-04-01 00:34:19 +00:00
|
|
|
include Extensions::Page::Tree
|
|
|
|
include Extensions::Page::EditableElements
|
|
|
|
include Extensions::Page::Parse
|
|
|
|
include Extensions::Page::Render
|
|
|
|
include Extensions::Page::Templatized
|
|
|
|
include Extensions::Page::Redirect
|
|
|
|
include Extensions::Page::Listed
|
2011-04-29 20:10:13 +00:00
|
|
|
include Extensions::Shared::Seo
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## fields ##
|
|
|
|
field :title
|
2010-04-30 14:05:53 +00:00
|
|
|
field :slug
|
2010-05-30 23:57:33 +00:00
|
|
|
field :fullpath
|
2010-08-21 22:48:24 +00:00
|
|
|
field :raw_template
|
2010-04-25 00:33:38 +00:00
|
|
|
field :published, :type => Boolean, :default => false
|
2010-07-09 10:38:50 +00:00
|
|
|
field :cache_strategy, :default => 'none'
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## associations ##
|
2010-07-31 02:15:24 +00:00
|
|
|
referenced_in :site
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-09-15 16:09:07 +00:00
|
|
|
## indexes ##
|
|
|
|
index :site_id
|
|
|
|
index :parent_id
|
|
|
|
index [[:fullpath, Mongo::ASCENDING], [:site_id, Mongo::ASCENDING]]
|
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
## callbacks ##
|
2011-03-04 23:41:44 +00:00
|
|
|
after_initialize :set_default_raw_template
|
2010-07-20 10:15:53 +00:00
|
|
|
before_validation :normalize_slug
|
2010-05-30 23:57:33 +00:00
|
|
|
before_save { |p| p.fullpath = p.fullpath(true) }
|
2010-05-10 22:39:52 +00:00
|
|
|
before_destroy :do_not_remove_index_and_404_pages
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## validations ##
|
2010-04-30 14:05:53 +00:00
|
|
|
validates_presence_of :site, :title, :slug
|
2010-05-10 22:39:52 +00:00
|
|
|
validates_uniqueness_of :slug, :scope => [:site_id, :parent_id]
|
2010-04-30 14:05:53 +00:00
|
|
|
validates_exclusion_of :slug, :in => Locomotive.config.reserved_slugs, :if => Proc.new { |p| p.depth == 0 }
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## named scopes ##
|
2012-01-19 01:30:12 +00:00
|
|
|
scope :latest_updated, :order_by => [[:updated_at, :desc]], :limit => Locomotive.config.latest_items_nb
|
2011-01-27 09:49:42 +00:00
|
|
|
scope :root, :where => { :slug => 'index', :depth => 0 }
|
2010-08-01 06:01:38 +00:00
|
|
|
scope :not_found, :where => { :slug => '404', :depth => 0 }
|
|
|
|
scope :published, :where => { :published => true }
|
2011-01-02 22:58:06 +00:00
|
|
|
scope :fullpath, lambda { |fullpath| { :where => { :fullpath => fullpath } } }
|
2011-07-07 16:03:55 +00:00
|
|
|
scope :minimal_attributes, :only => %w(title slug fullpath position depth published templatized redirect listed parent_id created_at updated_at)
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## methods ##
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
def index?
|
|
|
|
self.slug == 'index' && self.depth.to_i == 0
|
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
def not_found?
|
|
|
|
self.slug == '404' && self.depth.to_i == 0
|
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
def index_or_not_found?
|
|
|
|
self.index? || self.not_found?
|
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def fullpath(force = false)
|
|
|
|
if read_attribute(:fullpath).present? && !force
|
|
|
|
return read_attribute(:fullpath)
|
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
if self.index? || self.not_found?
|
|
|
|
self.slug
|
|
|
|
else
|
2011-08-29 13:17:18 +00:00
|
|
|
slugs = self.self_and_ancestors.sort_by(&:depth).map(&:slug)
|
2010-08-20 00:31:01 +00:00
|
|
|
slugs.shift unless slugs.size == 1
|
2010-05-30 23:57:33 +00:00
|
|
|
File.join slugs
|
|
|
|
end
|
2010-05-02 23:33:17 +00:00
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
def with_cache?
|
|
|
|
self.cache_strategy != 'none'
|
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
|
|
|
def to_liquid
|
2010-06-21 15:46:17 +00:00
|
|
|
Locomotive::Liquid::Drops::Page.new(self)
|
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
protected
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def do_not_remove_index_and_404_pages
|
2011-01-26 13:07:33 +00:00
|
|
|
return if self.site.nil? || self.site.destroyed?
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
if self.index? || self.not_found?
|
2010-07-13 00:46:17 +00:00
|
|
|
self.errors[:base] << I18n.t('errors.messages.protected_page')
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
self.errors.empty?
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
|
|
|
def normalize_slug
|
|
|
|
self.slug = self.title.clone if self.slug.blank? && self.title.present?
|
2011-06-21 20:03:24 +00:00
|
|
|
self.slug.permalink! if self.slug.present?
|
2010-04-30 14:05:53 +00:00
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2011-03-04 23:41:44 +00:00
|
|
|
def set_default_raw_template
|
|
|
|
self.raw_template ||= I18n.t('attributes.defaults.pages.other.body')
|
|
|
|
end
|
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|