45 lines
1.1 KiB
Ruby
45 lines
1.1 KiB
Ruby
class Page < CouchFoo::Base
|
|
|
|
property :name, String
|
|
property :parent_page_id, String
|
|
property :content, String
|
|
property :layout_id, String
|
|
property :layout_name, String
|
|
property :load_model, String
|
|
property :external_link, String
|
|
property :position, Integer
|
|
property :is_published, Boolean
|
|
|
|
belongs_to :layout
|
|
has_many :children, :class_name => 'Page', :foreign_key => 'parent_page_id'
|
|
|
|
validates_presence_of :name
|
|
validates_presence_of :position
|
|
|
|
before_save :setup_layout_id
|
|
before_validation :setup_default_value
|
|
|
|
default_sort :position
|
|
|
|
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
|
|
|
|
protected
|
|
|
|
def setup_default_value
|
|
if self.position.blank?
|
|
max_page = Page.last( :use_key => 'position')
|
|
self.position = (max_page)? max_page.position.to_i + 1 : 1
|
|
end
|
|
|
|
if self.parent_page_id.blank?
|
|
self.parent_page_id = "root"
|
|
end
|
|
end
|
|
|
|
end |