orbit-basic/app/models/page.rb

55 lines
1.3 KiB
Ruby
Raw Normal View History

class Page < CouchFoo::Base
2009-06-19 09:31:10 +00:00
property :lang, String
property :name, String
2009-05-22 06:43:52 +00:00
property :parent_page_id, String
2009-06-19 09:31:10 +00:00
property :content_zh_tw, String
property :content_en, String
property :content_ja, String
# TODO: more language can dynamic added
2009-05-07 17:54:33 +00:00
property :layout_id, String
property :layout_name, String
2009-06-01 02:54:16 +00:00
property :use_engine, 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 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?
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