Add Item#ancestors

This commit is contained in:
Wen-Tien Chang committed 2010-01-18 15:52:52 +08:00
1 parent e1440a79d8
commit 32e8bd108f
17 files changed
+60 -37

No files matched your search

+18 -4
View File
@@ -5,19 +5,30 @@ class Item
key :_type, String
key :name, String, :required => true, :index => true
key :parent_item_name, String, :required => true, :index => true
key :parent_name, String, :index => true
key :parent_id, String, :index => true
key_i18n :title, String, :required => true
key :position, Integer, :required => true
key :is_published, Boolean, :required => true, :default => true, :index => true
belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id
many :children, :class_name => "Item", :foreign_key => :parent_id
before_validation :setup_default_value
def self.find_by_name(item_name)
Item.find(:first, :conditions => { :name => item_name, :is_published => true })
end
def ancestors
node, nodes = self, []
nodes << node = node.parent while !node.parent.blank?
nodes.reverse
end
protected
def setup_default_value
@@ -26,8 +37,11 @@ class Item
self.position = (max_page)? max_page.position.to_i + 1 : 1
end
if self.parent_item_name.blank?
self.parent_item_name = "root"
if self.parent_name.blank?
self.parent_name = nil
self.parent_id = nil
else
self.parent_id = Item.find_by_name( self.parent_name ).id
end
end
+1 -1
View File
@@ -3,7 +3,7 @@ class Snippet
include MongoMapper::Document
key :name, String, :required => true, :index => true
key :parent_item_name, String, :required => true, :index => true
key :parent_name, String, :required => true, :index => true
key_i18n :content, String