2010-06-01 00:06:46 +00:00
|
|
|
class Page
|
|
|
|
|
|
|
|
include Locomotive::Mongoid::Document
|
|
|
|
|
|
|
|
## Extensions ##
|
|
|
|
include Models::Extensions::Page::Tree
|
|
|
|
include Models::Extensions::Page::Parts
|
|
|
|
include Models::Extensions::Page::Render
|
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-06-16 14:43:29 +00:00
|
|
|
field :cache_expires_in, :type => Integer, :default => 0
|
2010-04-25 00:33:38 +00:00
|
|
|
|
|
|
|
## associations ##
|
|
|
|
belongs_to_related :site
|
2010-05-09 12:44:53 +00:00
|
|
|
belongs_to_related :layout
|
2010-04-25 00:33:38 +00:00
|
|
|
embeds_many :parts, :class_name => 'PagePart'
|
2010-05-02 23:33:17 +00:00
|
|
|
|
|
|
|
## callbacks ##
|
|
|
|
before_validate :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-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-04-25 00:33:38 +00:00
|
|
|
|
|
|
|
## named scopes ##
|
2010-05-26 00:41:10 +00:00
|
|
|
named_scope :latest_updated, :order_by => [[:updated_at, :desc]], :limit => Locomotive.config.lastest_items_nb
|
2010-06-01 00:06:46 +00:00
|
|
|
named_scope :index, :where => { :slug => 'index', :depth => 0, :published => true }
|
|
|
|
named_scope :not_found, :where => { :slug => '404', :depth => 0, :published => true }
|
2010-04-25 00:33:38 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
## behaviours ##
|
2010-06-21 15:46:17 +00:00
|
|
|
# liquid_methods :title, :fullpath
|
2010-06-01 00:06:46 +00:00
|
|
|
liquify_template :joined_parts
|
2010-04-30 14:05:53 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
## methods ##
|
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
def index?
|
|
|
|
self.slug == 'index' && self.depth.to_i == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_found?
|
|
|
|
self.slug == '404' && self.depth.to_i == 0
|
|
|
|
end
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def fullpath(force = false)
|
|
|
|
if read_attribute(:fullpath).present? && !force
|
|
|
|
return read_attribute(:fullpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def url
|
2010-05-30 23:57:33 +00:00
|
|
|
"http://#{self.site.domains.first}/#{self.fullpath}.html"
|
|
|
|
end
|
2010-06-21 15:46:17 +00:00
|
|
|
|
|
|
|
def to_liquid(options = {})
|
|
|
|
Locomotive::Liquid::Drops::Page.new(self)
|
|
|
|
end
|
2010-06-01 00:06:46 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
protected
|
2010-04-25 00:33:38 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
def do_not_remove_index_and_404_pages
|
|
|
|
# safe_site = self.site rescue nil
|
|
|
|
|
|
|
|
# return if safe_site.nil?
|
|
|
|
return if (self.site rescue nil).nil?
|
|
|
|
|
|
|
|
if self.index? || self.not_found?
|
|
|
|
raise I18n.t('errors.messages.protected_page')
|
|
|
|
end
|
|
|
|
end
|
2010-06-01 00:06:46 +00:00
|
|
|
|
2010-05-02 23:33:17 +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-06-01 00:06:46 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
end
|