engine/app/models/page.rb

67 lines
1.7 KiB
Ruby
Raw Normal View History

2010-04-25 00:33:38 +00:00
class Page
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Acts::Tree
2010-04-25 00:33:38 +00:00
## fields ##
field :title
field :slug
2010-04-25 00:33:38 +00:00
field :published, :type => Boolean, :default => false
field :keywords
field :description
field :position, :type => Integer
2010-04-25 00:33:38 +00:00
## associations ##
belongs_to_related :site
embeds_many :parts, :class_name => 'PagePart'
## validations ##
validates_presence_of :site, :title, :slug
validates_uniqueness_of :slug, :scope => :site_id
validates_exclusion_of :slug, :in => Locomotive.config.reserved_slugs, :if => Proc.new { |p| p.depth == 0 }
2010-04-25 00:33:38 +00:00
## callbacks ##
before_create :add_to_list_bottom
2010-04-25 00:33:38 +00:00
before_create :add_body_part
before_destroy :remove_from_list
before_validate :normalize_slug
2010-04-25 00:33:38 +00:00
## named scopes ##
## behaviours ##
acts_as_tree
2010-04-25 00:33:38 +00:00
## methods ##
def add_body_part
self.parts.build :name => 'body', :value => '---body here---'
end
def parent=(owner) # missing in acts_as_tree
self[self.parent_id_field] = owner._id
self[self.path_field] = owner[owner.path_field] + [owner._id]
self[self.depth_field] = owner[owner.depth_field] + 1
self.instance_variable_set :@_will_move, true
end
def route
File.join self.self_and_ancestors.map(&:slug)
end
protected
2010-04-25 00:33:38 +00:00
def add_to_list_bottom
self.position = (Page.where(:_id.ne => self._id).and(:parent_id => self.parent_id).max(:position) || 0) + 1
end
def remove_from_list
Page.where(:parent_id => self.parent_id).and(:position.gt => self.position).each do |p|
p.position -= 1
p.save
2010-04-25 00:33:38 +00:00
end
end
def normalize_slug
self.slug.slugify!(:without_extension => true) if self.slug.present?
end
2010-04-25 00:33:38 +00:00
end