2011-10-30 23:02:41 +00:00
|
|
|
module Locomotive
|
|
|
|
class Page
|
|
|
|
|
|
|
|
include Locomotive::Mongoid::Document
|
|
|
|
|
|
|
|
## Extensions ##
|
|
|
|
include Extensions::Page::Tree
|
|
|
|
include Extensions::Page::EditableElements
|
|
|
|
include Extensions::Page::Parse
|
|
|
|
include Extensions::Page::Render
|
2012-04-05 15:12:26 +00:00
|
|
|
include Extensions::Page::Fullpath
|
2011-10-30 23:02:41 +00:00
|
|
|
include Extensions::Page::Redirect
|
|
|
|
include Extensions::Page::Listed
|
|
|
|
include Extensions::Shared::Seo
|
|
|
|
|
|
|
|
## fields ##
|
2012-01-25 21:07:10 +00:00
|
|
|
field :title, :localize => true
|
|
|
|
field :slug, :localize => true
|
2012-02-10 23:39:06 +00:00
|
|
|
field :handle
|
2012-01-25 21:07:10 +00:00
|
|
|
field :raw_template, :localize => true
|
|
|
|
field :locales, :type => Array
|
|
|
|
field :published, :type => Boolean, :default => false
|
|
|
|
field :cache_strategy, :default => 'none'
|
2012-03-22 18:20:31 +00:00
|
|
|
field :response_type, :default => 'text/html'
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## associations ##
|
2012-02-01 01:01:42 +00:00
|
|
|
belongs_to :site, :class_name => 'Locomotive::Site'
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## indexes ##
|
|
|
|
index :site_id
|
|
|
|
index :parent_id
|
|
|
|
index [[:fullpath, Mongo::ASCENDING], [:site_id, Mongo::ASCENDING]]
|
|
|
|
|
|
|
|
## callbacks ##
|
2011-11-03 13:01:08 +00:00
|
|
|
after_initialize :set_default_raw_template
|
|
|
|
before_validation :normalize_slug
|
2012-01-25 21:07:10 +00:00
|
|
|
before_save :record_current_locale
|
2011-11-03 13:01:08 +00:00
|
|
|
before_destroy :do_not_remove_index_and_404_pages
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## validations ##
|
|
|
|
validates_presence_of :site, :title, :slug
|
|
|
|
validates_uniqueness_of :slug, :scope => [:site_id, :parent_id]
|
2012-02-10 23:39:06 +00:00
|
|
|
validates_uniqueness_of :handle, :allow_blank => true
|
2012-02-16 21:07:00 +00:00
|
|
|
validates_exclusion_of :slug, :in => Locomotive.config.reserved_slugs, :if => Proc.new { |p| p.depth <= 1 }
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## named scopes ##
|
2012-03-01 23:31:45 +00:00
|
|
|
scope :latest_updated, :order_by => [[:updated_at, :desc]], :limit => Locomotive.config.ui[:latest_entries_nb]
|
2011-11-21 01:27:05 +00:00
|
|
|
scope :root, :where => { :slug => 'index', :depth => 0 }
|
|
|
|
scope :not_found, :where => { :slug => '404', :depth => 0 }
|
|
|
|
scope :published, :where => { :published => true }
|
2012-02-10 23:39:06 +00:00
|
|
|
scope :handle, lambda { |handle| { :where => { :handle => handle } } }
|
2012-04-10 15:44:13 +00:00
|
|
|
scope :minimal_attributes, lambda { |attrs = []| { :only => (attrs || []) + %w(title slug fullpath position depth published with_wildcards redirect listed wildcard response_type parent_id parent_ids site_id created_at updated_at) } }
|
2011-10-30 23:02:41 +00:00
|
|
|
|
|
|
|
## methods ##
|
|
|
|
|
|
|
|
def index?
|
|
|
|
self.slug == 'index' && self.depth.to_i == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_found?
|
|
|
|
self.slug == '404' && self.depth.to_i == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def index_or_not_found?
|
|
|
|
self.index? || self.not_found?
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_cache?
|
|
|
|
self.cache_strategy != 'none'
|
|
|
|
end
|
|
|
|
|
2012-03-22 18:20:31 +00:00
|
|
|
def default_response_type?
|
|
|
|
self.response_type == 'text/html'
|
|
|
|
end
|
|
|
|
|
2012-03-14 00:16:01 +00:00
|
|
|
def translated?
|
|
|
|
self.title_translations.key?(::Mongoid::Fields::I18n.locale.to_s) rescue false
|
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
def to_liquid
|
|
|
|
Locomotive::Liquid::Drops::Page.new(self)
|
|
|
|
end
|
|
|
|
|
2011-12-08 15:47:17 +00:00
|
|
|
def to_presenter
|
|
|
|
Locomotive::PagePresenter.new(self)
|
|
|
|
end
|
|
|
|
|
2011-11-21 01:27:05 +00:00
|
|
|
def as_json(options = {})
|
2011-12-08 15:47:17 +00:00
|
|
|
self.to_presenter.as_json
|
2011-11-21 01:27:05 +00:00
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def do_not_remove_index_and_404_pages
|
|
|
|
return if self.site.nil? || self.site.destroyed?
|
|
|
|
|
|
|
|
if self.index? || self.not_found?
|
2012-01-14 00:38:09 +00:00
|
|
|
self.errors[:base] << ::I18n.t('errors.messages.protected_page')
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self.errors.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def normalize_slug
|
|
|
|
self.slug = self.title.clone if self.slug.blank? && self.title.present?
|
|
|
|
self.slug.permalink! if self.slug.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_default_raw_template
|
2012-01-14 00:38:09 +00:00
|
|
|
self.raw_template ||= ::I18n.t('attributes.defaults.pages.other.body')
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
|
|
|
|
2012-01-25 21:07:10 +00:00
|
|
|
def record_current_locale
|
|
|
|
self.locales ||= []
|
|
|
|
self.locales << ::Mongoid::Fields::I18n.locale
|
|
|
|
self.locales.uniq!
|
|
|
|
end
|
|
|
|
|
2011-10-30 23:02:41 +00:00
|
|
|
end
|
2012-01-25 11:16:43 +00:00
|
|
|
end
|