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.



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
# File 'lib/arboreal/active_record_extensions.rb', line 7

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

  case ActiveRecord::VERSION::MAJOR

  when 3
    scope :roots, where(:parent_id => nil)

  when 2
    named_scope :roots, {
      :conditions => ["parent_id IS NULL"]
    }

  end

end