Module: SimpleNestedSet::ClassMethods

Defined in:
lib/simple_nested_set/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#after_move(*args, &block) ⇒ Object



9
10
11
# File 'lib/simple_nested_set/class_methods.rb', line 9

def after_move(*args, &block)
  set_callback(:move, :after, *args, &block)
end

#before_move(*args, &block) ⇒ Object



5
6
7
# File 'lib/simple_nested_set/class_methods.rb', line 5

def before_move(*args, &block)
  set_callback(:move, :before, *args, &block)
end

#leaves(scope = nil) ⇒ Object

Returns roots when multiple roots (or virtual root, which is the same)



24
25
26
# File 'lib/simple_nested_set/class_methods.rb', line 24

def leaves(scope = nil)
  nested_set_class.scope(scope).with_leaves
end

#root(scope = nil) ⇒ Object

Returns the first root node (with the given scope if any)



14
15
16
# File 'lib/simple_nested_set/class_methods.rb', line 14

def root(scope = nil)
  roots(scope).first
end

#roots(scope = nil) ⇒ Object

Returns root nodes (with the given scope if any)



19
20
21
# File 'lib/simple_nested_set/class_methods.rb', line 19

def roots(scope = nil)
  nested_set_class.scope(scope).without_parent
end

#with_ancestors(lft, rgt) ⇒ Object



40
41
42
# File 'lib/simple_nested_set/class_methods.rb', line 40

def with_ancestors(lft, rgt)
  where(arel_table[:lft].lt(lft).and(arel_table[:rgt].gt(rgt)))
end

#with_descendants(lft, rgt) ⇒ Object



44
45
46
# File 'lib/simple_nested_set/class_methods.rb', line 44

def with_descendants(lft, rgt)
  where(arel_table[:lft].gt(lft).and(arel_table[:rgt].lt(rgt)))
end

#with_leavesObject



56
57
58
# File 'lib/simple_nested_set/class_methods.rb', line 56

def with_leaves
  where("#{arel_table[:lft].to_sql} = #{arel_table[:rgt].to_sql} - 1")
end

#with_left_sibling(lft) ⇒ Object



48
49
50
# File 'lib/simple_nested_set/class_methods.rb', line 48

def with_left_sibling(lft)
  where(:rgt => lft - 1)
end

#with_parent(parent_id) ⇒ Object



36
37
38
# File 'lib/simple_nested_set/class_methods.rb', line 36

def with_parent(parent_id)
  where(:parent_id => parent_id)
end

#with_right_sibling(rgt) ⇒ Object



52
53
54
# File 'lib/simple_nested_set/class_methods.rb', line 52

def with_right_sibling(rgt)
  where(:lft => rgt + 1)
end

#without_node(id) ⇒ Object



28
29
30
# File 'lib/simple_nested_set/class_methods.rb', line 28

def without_node(id)
  where(arel_table[:id].not_eq(id))
end

#without_parentObject



32
33
34
# File 'lib/simple_nested_set/class_methods.rb', line 32

def without_parent
  with_parent(nil)
end