2009-05-07 17:18:16 +00:00
|
|
|
class Page < CouchFoo::Base
|
|
|
|
|
|
|
|
property :name, String
|
2009-05-22 06:43:52 +00:00
|
|
|
property :parent_page_id, String
|
2009-06-19 09:31:10 +00:00
|
|
|
|
2009-06-26 13:16:37 +00:00
|
|
|
property_i18n :content, String
|
2009-06-19 09:31:10 +00:00
|
|
|
|
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'
|
2009-05-07 17:18:16 +00:00
|
|
|
|
|
|
|
validates_presence_of :name
|
2009-05-22 06:43:52 +00:00
|
|
|
validates_presence_of :position
|
2009-05-07 17:18:16 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-05-07 17:18:16 +00:00
|
|
|
end
|