Module: MongoNestedSet::SingletonMethods

Defined in:
lib/mongo_nested_set.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_nested_set(options = {}) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongo_nested_set.rb', line 7

def acts_as_nested_set(options = {})
  options = {
    :parent_column => 'parent_id',
    :left_column => 'lft',
    :right_column => 'rgt',
    :dependent => :delete_all, # or :destroy
  }.merge(options)
  
  if options[:scope].is_a?(Symbol) && options[:scope].to_s !~ /_id$/
    options[:scope] = "#{options[:scope]}_id".intern
  end

  write_inheritable_attribute :acts_as_nested_set_options, options
  class_inheritable_reader :acts_as_nested_set_options
  
  unless self.is_a?(ClassMethods)
    include Comparable
    include Columns
    include InstanceMethods
    extend Columns
    extend ClassMethods
    
    belongs_to :parent, :class_name => self.base_class.to_s,
      :foreign_key => parent_column_name
    many :children, :class_name => self.base_class.to_s,
      :foreign_key => parent_column_name, :order => quoted_left_column_name

    attr_accessor :skip_before_destroy
  
    key left_column_name.intern, Integer
    key right_column_name.intern, Integer
    key parent_column_name.intern, ObjectId
  
    # no bulk assignment
    # if accessible_attributes.blank?
    #   attr_protected  left_column_name.intern, right_column_name.intern 
    # end
                  
    before_create  :set_default_left_and_right
    before_save    :store_new_parent
    after_save     :move_to_new_parent
    before_destroy :destroy_descendants
                  
    # no assignment to structure fields
    # [left_column_name, right_column_name].each do |column|
    #   module_eval <<-"end_eval", __FILE__, __LINE__
    #     def #{column}=(x)
    #       raise "Unauthorized assignment to #{column}: it's an internal field handled by acts_as_nested_set code, use move_to_* methods instead."
    #     end
    #   end_eval
    # end
  
    # named_scope :roots, :conditions => {parent_column_name => nil}, :order => quoted_left_column_name
    # named_scope :leaves, :conditions => "#{quoted_right_column_name} - #{quoted_left_column_name} = 1", :order => quoted_left_column_name

    define_callbacks("before_move", "after_move")
  end
end