Module: Arboreal::ActiveRecordExtensions

Defined in:
lib/arboreal/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#acts_arborealObject

Declares that this ActiveRecord::Base model has a tree-like structure.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/arboreal/active_record_extensions.rb', line 5

def acts_arboreal

  belongs_to :parent, :class_name => self.name
  has_many   :children, :class_name => self.name, :foreign_key => :parent_id

  extend Arboreal::ClassMethods
  include Arboreal::InstanceMethods

  before_validation :populate_ancestry_string
  
  validate do |record|
    record.send(:validate_parent_not_ancestor)
  end

  before_save :detect_ancestry_change
  after_save  :apply_ancestry_change_to_descendants
  
  named_scope :roots, {
    :conditions => ["parent_id IS NULL"]
  }
  
end