52 lines
1.1 KiB
Ruby
52 lines
1.1 KiB
Ruby
class Page
|
|
include MongoMapper::Document
|
|
|
|
key :name, String
|
|
key :parent_page_name, String
|
|
|
|
key_i18n :title, String
|
|
key_i18n :content, String
|
|
|
|
key :layout_id, String
|
|
key :layout_name, String
|
|
key :external_link, String
|
|
key :position, Integer
|
|
key :is_published, Boolean
|
|
|
|
key :is_component, Boolean
|
|
key :component_name, String
|
|
|
|
belongs_to :layout
|
|
|
|
validates_presence_of :name
|
|
validates_presence_of :position
|
|
|
|
before_save :setup_layout_id
|
|
before_validation :setup_default_value
|
|
|
|
def self.find_by_name(page_name)
|
|
Page.find(:first, :conditions => { :name => page_name, :is_published => true })
|
|
end
|
|
|
|
protected
|
|
|
|
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
|
|
|
|
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 |