orbit-basic/app/models/page.rb

40 lines
1.0 KiB
Ruby

class Page
include MongoMapper::Document
key :name, String, :required => true, :index => true
key :parent_page_name, String, :required => true, :index => true
key_i18n :title, String, :required => true
key_i18n :content, String
key :layout_name, String, :required => true
key :external_link, String
key :position, Integer, :required => true
key :is_published, Boolean, :required => true, :default => true, :index => true
key :component_name, String
before_validation :setup_default_value
def self.find_by_name(page_name)
Page.find(:first, :conditions => { :name => page_name, :is_published => true })
end
def layout
Layout.find_by_name(self.layout_name)
end
protected
def setup_default_value
if self.position.blank?
max_page = Page.find(:last, :order => 'position')
self.position = (max_page)? max_page.position.to_i + 1 : 1
end
if self.parent_page_name.blank?
self.parent_page_name = "root"
end
end
end