2010-01-04 07:52:30 +00:00
|
|
|
class Page
|
|
|
|
include MongoMapper::Document
|
2009-05-07 17:18:16 +00:00
|
|
|
|
2010-01-11 09:09:50 +00:00
|
|
|
key :name, String, :required => true, :index => true
|
|
|
|
key :parent_page_name, String, :required => true, :index => true
|
2010-01-08 07:32:44 +00:00
|
|
|
|
2010-01-11 09:09:50 +00:00
|
|
|
key_i18n :title, String, :required => true
|
2010-01-08 07:32:44 +00:00
|
|
|
key_i18n :content, String
|
2010-01-04 07:52:30 +00:00
|
|
|
|
2010-01-11 09:09:50 +00:00
|
|
|
key :layout_name, String, :required => true
|
2010-01-04 07:52:30 +00:00
|
|
|
key :external_link, String
|
2010-01-11 09:09:50 +00:00
|
|
|
key :position, Integer, :required => true
|
|
|
|
key :is_published, Boolean, :required => true, :default => true, :index => true
|
2009-05-07 17:54:33 +00:00
|
|
|
|
2010-01-08 10:36:36 +00:00
|
|
|
key :component_name, String
|
|
|
|
|
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
|
|
|
|
|
2010-01-11 09:09:50 +00:00
|
|
|
def layout
|
|
|
|
Layout.find_by_name(self.layout_name)
|
2009-05-07 17:54:33 +00:00
|
|
|
end
|
2010-01-11 09:09:50 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
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
|
|
|
|
|
2010-01-08 10:36:36 +00:00
|
|
|
if self.parent_page_name.blank?
|
|
|
|
self.parent_page_name = "root"
|
2009-05-22 06:43:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-07 17:18:16 +00:00
|
|
|
end
|