Module: Treeoid

Defined in:
lib/treeoid.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/treeoid.rb', line 2

def self.included(base)
  base.class_eval do
    field :ancestry, :type => Array

    references_many :children, :class_name => base.name, :foreign_key => :parent_id
    referenced_in   :parent,   :class_name => base.name

    validate :hierarchical_integrity
    before_save :set_ancestry!
    after_save :set_child_ancestries!

    scope :root, where(:parent_id => nil)
    
    def has_children?
      !children.empty?
    end

    def has_parent?
      !parent.nil?
    end
    
  protected    
    def hierarchical_integrity
      return unless parent_id
      errors.add(:parent_id, "cannot be itself") if parent_id == self.id
      unless self.class.where(:ancestry => id).id(parent_id).empty?
        errors.add(:parent_id, "cannot be a current child") 
      end
    end

    def set_ancestry!
      new_path = [(parent.try(:ancestry) || parent_id), id].compact.flatten
      write_attribute :ancestry, new_path
      
    end
    
    def set_child_ancestries!
      children.each(&:save)
    end
  end
  
  base.instance_eval do
    def hierarchically
      criteria.asc :ancestry
    end
  end
end