Module: Adocca::Acts::ActsAsFastNestedSet::ClassMethods

Defined in:
lib/acts_as_fast_nested_set.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_fast_nested_set(options = {}) ⇒ Object

Makes this model act as a fast_nested_set. It needs to have parent_id, node_id and naturally id among its attributes.

If you want to create different trees (with colliding node_ids, use the :uniqueness_scope param.

Example: acts_as_fast_nested_set :uniqueness_scope => [:forum_id, :forum_class] <- Will have a separate tree of node_ids

for each unique combination of forum_id 
and forum_class in the model.


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/acts_as_fast_nested_set.rb', line 77

def acts_as_fast_nested_set(options = {})
  belongs_to :parent, :class_name => self.name, :foreign_key => 'parent_id'
  has_many :children, :class_name => self.name, :foreign_key => 'parent_id'
  attr_protected :node_id, :parent_id
  if options[:uniqueness_scope]
    validates_uniqueness_of :node_id, :scope => options[:uniqueness_scope]
  else
    validates_uniqueness_of :node_id
  end
  validate :ensure_node_id
  class_eval do
    extend Adocca::Acts::ActsAsFastNestedSet::SingletonMethods
  end
  set_options(options)
  include Adocca::Acts::ActsAsFastNestedSet::InstanceMethods
  descendant_invalidation_proc = Proc.new do |instance|
    ob = instance.parent
    while ob
      expire_cached_value("#{self.name}:#{ob.id}:descendants")
      ob = ob.parent
    end
  end
  after_save descendant_invalidation_proc
  after_save :unlock_nodeid_lock
  after_destroy descendant_invalidation_proc
end