Orbit/app/models/page.rb

44 lines
1.0 KiB
Ruby
Raw Normal View History

class Page < CouchFoo::Base
property :name, String
2009-05-22 06:43:52 +00:00
property :parent_page_id, String
property :content, String
2009-05-07 17:54:33 +00:00
property :layout_id, String
property :layout_name, String
2009-05-22 06:51:32 +00:00
property :external_link, String
2009-05-22 06:43:52 +00:00
property :position, Integer
property :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'
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
default_sort :position
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
protected
def setup_default_value
2009-05-22 06:51:32 +00:00
if self.position.blank?
2009-05-22 06:43:52 +00:00
max_page = Page.last( :use_key => 'position')
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
end