Module: MongoTree::Strategies::AncestorArray

Includes:
Base
Defined in:
lib/mongo_tree/strategies/ancestor_array.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mongo_tree/strategies/ancestor_array.rb', line 6

def self.included(base)
  base.class_eval do
    class << self
      attr_accessor :mongo_tree_options
    end
    
    @mongo_tree_options = { :path_attribute => :id }.merge(@mongo_tree_options)
    
    key :parent_id, ObjectId
    key :ancestor_ids, Array, :index => true
    belongs_to :parent, :class_name => self.name
    before_save :update_ancestors, :if => Proc.new{ |doc| doc.changes.has_key? "parent_id" }
    after_save :update_children, :if => Proc.new{ |doc| doc.changes.has_key? "parent_id" }
  end
  
  base.extend ClassMethods
  base.send :include, InstanceMethods
end