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 ##
|
2010-06-01 00:06:46 +00:00
|
|
|
include Models::Extensions::Page::Tree
|
|
|
|
include Models::Extensions::Page::Parts
|
|
|
|
include Models::Extensions::Page::Render
|
2010-07-16 20:36:07 +00:00
|
|
|
include Models::Extensions::Page::Templatized
|
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-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-08-01 07:50:29 +00:00
|
|
|
# allows newly pages to have a default body
|
|
|
|
attr_accessor :body
|
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## associations ##
|
2010-07-31 02:15:24 +00:00
|
|
|
referenced_in :site
|
|
|
|
referenced_in :layout
|
2010-04-25 00:33:38 +00:00
|
|
|
embeds_many :parts, :class_name => 'PagePart'
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
## callbacks ##
|
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 ##
|
2010-08-01 06:01:38 +00:00
|
|
|
scope :latest_updated, :order_by => [[:updated_at, :desc]], :limit => Locomotive.config.lastest_items_nb
|
|
|
|
scope :index, :where => { :slug => 'index', :depth => 0 }
|
|
|
|
scope :not_found, :where => { :slug => '404', :depth => 0 }
|
|
|
|
scope :published, :where => { :published => true }
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
## behaviours ##
|
2010-06-01 00:06:46 +00:00
|
|
|
liquify_template :joined_parts
|
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
|
|
|
|
slugs = self.self_and_ancestors.map(&:slug)
|
|
|
|
slugs.shift
|
|
|
|
File.join slugs
|
|
|
|
end
|
2010-05-02 23:33:17 +00:00
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
def url
|
2010-05-30 23:57:33 +00:00
|
|
|
"http://#{self.site.domains.first}/#{self.fullpath}.html"
|
|
|
|
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
|
|
|
|
return if (self.site rescue nil).nil?
|
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?
|
2010-04-30 14:05:53 +00:00
|
|
|
self.slug.slugify!(:without_extension => true) if self.slug.present?
|
|
|
|
end
|
2010-07-22 00:18:14 +00:00
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|