2010-01-04 07:52:30 +00:00
|
|
|
class Page
|
|
|
|
include MongoMapper::Document
|
2009-05-07 17:18:16 +00:00
|
|
|
|
2010-01-04 07:52:30 +00:00
|
|
|
key :name, String
|
|
|
|
key :parent_page_id, String
|
2009-06-19 09:31:10 +00:00
|
|
|
|
2010-01-04 07:52:30 +00:00
|
|
|
key :content_en, String
|
|
|
|
key :content_zh_tw, String
|
|
|
|
|
|
|
|
key :layout_id, String
|
|
|
|
key :layout_name, String
|
|
|
|
key :use_engine, String
|
|
|
|
key :external_link, String
|
|
|
|
key :position, Integer
|
|
|
|
key :is_published, Boolean
|
2009-05-07 17:54:33 +00:00
|
|
|
|
|
|
|
belongs_to :layout
|
2009-05-22 06:43:52 +00:00
|
|
|
has_many :children, :class_name => 'Page', :foreign_key => 'parent_page_id'
|
2009-05-07 17:18:16 +00:00
|
|
|
|
|
|
|
validates_presence_of :name
|
2009-05-22 06:43:52 +00:00
|
|
|
validates_presence_of :position
|
2009-05-07 17:18:16 +00:00
|
|
|
|
2009-05-07 17:54:33 +00:00
|
|
|
before_save :setup_layout_id
|
2009-05-22 06:43:52 +00:00
|
|
|
before_validation :setup_default_value
|
2010-01-04 07:52:30 +00:00
|
|
|
|
2009-06-01 06:20:15 +00:00
|
|
|
def self.find_by_name(page_name)
|
|
|
|
Page.find(:first, :conditions => { :name => page_name, :is_published => true })
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2009-05-07 17:54:33 +00:00
|
|
|
def setup_layout_id
|
|
|
|
if self.layout_name.blank?
|
|
|
|
self.layout_id = nil
|
|
|
|
else
|
|
|
|
self.layout_id = Layout.find_by_name( self.layout_name ).id
|
|
|
|
end
|
|
|
|
end
|
2009-05-22 06:43:52 +00:00
|
|
|
|
|
|
|
def setup_default_value
|
2009-05-22 06:51:32 +00:00
|
|
|
if self.position.blank?
|
2010-01-04 07:52:30 +00:00
|
|
|
max_page = Page.find(:last, :order => 'position')
|
2009-05-22 06:43:52 +00:00
|
|
|
self.position = (max_page)? max_page.position.to_i + 1 : 1
|
|
|
|
end
|
|
|
|
|
2009-05-22 06:51:32 +00:00
|
|
|
if self.parent_page_id.blank?
|
2009-05-22 06:43:52 +00:00
|
|
|
self.parent_page_id = "root"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-07 17:18:16 +00:00
|
|
|
end
|