Module: SimpleNestedSet::ClassMethods
- Defined in:
- lib/simple_nested_set/class_methods.rb
Instance Method Summary collapse
- #after_move(*args, &block) ⇒ Object
- #before_move(*args, &block) ⇒ Object
-
#leaves(scope = nil) ⇒ Object
Returns roots when multiple roots (or virtual root, which is the same).
- #nested_set_transaction(sort_order = :id) ⇒ Object
-
#root(scope = nil) ⇒ Object
Returns the first root node (with the given scope if any).
-
#roots(scope = nil) ⇒ Object
Returns root nodes (with the given scope if any).
- #with_ancestors(lft, rgt, opts = {}) ⇒ Object
- #with_descendants(lft, rgt, opts = {}) ⇒ Object
- #with_leaves ⇒ Object
- #with_left_sibling(lft) ⇒ Object
- #with_parent(parent_id) ⇒ Object
- #with_right_sibling(rgt) ⇒ Object
- #without_node(id) ⇒ Object
- #without_parent ⇒ Object
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 |
#nested_set_transaction(sort_order = :id) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/simple_nested_set/class_methods.rb', line 69 def nested_set_transaction(sort_order = :id) begin nested_set_class.move_after_save = false self.transaction do yield self # nested_set_class.new(self).rebuild_by_parents!(sort_order) Rebuild::FromParents.new.run(self, sort_order) end ensure nested_set_class.move_after_save = true end 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, opts = {}) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/simple_nested_set/class_methods.rb', line 40 def with_ancestors(lft, rgt, opts = {}) if opts.fetch(:include_self, false) where(arel_table[:lft].lteq(lft).and(arel_table[:rgt].gteq(rgt))).order(:lft) else where(arel_table[:lft].lt(lft).and(arel_table[:rgt].gt(rgt))).order(:lft) end end |
#with_descendants(lft, rgt, opts = {}) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/simple_nested_set/class_methods.rb', line 48 def with_descendants(lft, rgt, opts = {}) if opts.fetch(:include_self, false) where(arel_table[:lft].gteq(lft).and(arel_table[:rgt].lteq(rgt))).order(:lft) else where(arel_table[:lft].gt(lft).and(arel_table[:rgt].lt(rgt))).order(:lft) end end |
#with_leaves ⇒ Object
64 65 66 67 |
# File 'lib/simple_nested_set/class_methods.rb', line 64 def with_leaves # where("#{arel_table[:lft].to_sql} = #{arel_table[:rgt].to_sql} - 1").order(:lft) where("#{arel_table.name}.lft = #{arel_table.name}.rgt - 1").order(:lft) end |
#with_left_sibling(lft) ⇒ Object
56 57 58 |
# File 'lib/simple_nested_set/class_methods.rb', line 56 def with_left_sibling(lft) where(:rgt => lft - 1).order(:lft) 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).order(:lft) end |
#with_right_sibling(rgt) ⇒ Object
60 61 62 |
# File 'lib/simple_nested_set/class_methods.rb', line 60 def with_right_sibling(rgt) where(:lft => rgt + 1).order(:lft) 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)).order(:lft) end |
#without_parent ⇒ Object
32 33 34 |
# File 'lib/simple_nested_set/class_methods.rb', line 32 def without_parent with_parent(nil) end |