Module: SimpleNestedSet::ActMacro

Defined in:
lib/simple_nested_set/act_macro.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_nested_set(options = {}) ⇒ Object



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

def acts_as_nested_set(options = {})
  return if acts_as_nested_set?

  extend  SimpleNestedSet::ClassMethods
  extend  SimpleNestedSet::Inspect

  include SimpleNestedSet::InstanceMethods
  include SimpleNestedSet::Inspect

  define_callbacks :move, :terminator => "result == false"

  before_create  lambda { |r| r.nested_set.init_as_node }
  before_destroy lambda { |r| r.nested_set.prune_branch }
  after_save     lambda { |r| r.nested_set.save! }

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

  default_scope :order => :lft

  class_inheritable_accessor :nested_set_class
  self.nested_set_class = NestedSet.build_class(self, options[:scope])
end

#acts_as_nested_set?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/simple_nested_set/act_macro.rb', line 27

def acts_as_nested_set?
  included_modules.include?(SimpleNestedSet::InstanceMethods)
end