orbit-basic/app/models/page.rb

52 lines
1.1 KiB
Ruby
Raw Normal View History

2010-01-04 07:52:30 +00:00
class Page
include MongoMapper::Document
2010-01-04 07:52:30 +00:00
key :name, String
2010-01-08 10:36:36 +00:00
key :parent_page_name, String
2010-01-08 07:32:44 +00:00
2010-01-08 10:36:36 +00:00
key_i18n :title, String
2010-01-08 07:32:44 +00:00
key_i18n :content, String
2010-01-04 07:52:30 +00:00
key :layout_id, String
key :layout_name, String
key :external_link, String
key :position, Integer
key :is_published, Boolean
2009-05-07 17:54:33 +00:00
2010-01-08 10:36:36 +00:00
key :is_component, Boolean
key :component_name, String
2009-05-07 17:54:33 +00:00
belongs_to :layout
validates_presence_of :name
2009-05-22 06:43:52 +00:00
validates_presence_of :position
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
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
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
end