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).
-
#trees(scope = nil) ⇒ Object
Returns all roots recursively pre-populated with their children associations.
- #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)
32 33 34 |
# File 'lib/simple_nested_set/class_methods.rb', line 32 def leaves(scope = nil) nested_set_class.scope(scope).with_leaves end |
#nested_set_transaction(sort_order = :id) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/simple_nested_set/class_methods.rb', line 77 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)
22 23 24 |
# File 'lib/simple_nested_set/class_methods.rb', line 22 def root(scope = nil) roots(scope).first end |
#roots(scope = nil) ⇒ Object
Returns root nodes (with the given scope if any)
27 28 29 |
# File 'lib/simple_nested_set/class_methods.rb', line 27 def roots(scope = nil) nested_set_class.scope(scope).without_parent end |
#trees(scope = nil) ⇒ Object
Returns all roots recursively pre-populated with their children associations
14 15 16 17 18 19 |
# File 'lib/simple_nested_set/class_methods.rb', line 14 def trees(scope = nil) nodes = nested_set_class.scope(scope) nodes.select(&:root?).each do |root| root.nested_set.populate_associations(nodes.select { |node| node.descendent_of?(root) }) end end |
#with_ancestors(lft, rgt, opts = {}) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/simple_nested_set/class_methods.rb', line 48 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
56 57 58 59 60 61 62 |
# File 'lib/simple_nested_set/class_methods.rb', line 56 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
72 73 74 75 |
# File 'lib/simple_nested_set/class_methods.rb', line 72 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
64 65 66 |
# File 'lib/simple_nested_set/class_methods.rb', line 64 def with_left_sibling(lft) where(:rgt => lft - 1).order(:lft) end |
#with_parent(parent_id) ⇒ Object
44 45 46 |
# File 'lib/simple_nested_set/class_methods.rb', line 44 def with_parent(parent_id) where(:parent_id => parent_id).order(:lft) end |
#with_right_sibling(rgt) ⇒ Object
68 69 70 |
# File 'lib/simple_nested_set/class_methods.rb', line 68 def with_right_sibling(rgt) where(:lft => rgt + 1).order(:lft) end |
#without_node(id) ⇒ Object
36 37 38 |
# File 'lib/simple_nested_set/class_methods.rb', line 36 def without_node(id) where(arel_table[:id].not_eq(id)).order(:lft) end |
#without_parent ⇒ Object
40 41 42 |
# File 'lib/simple_nested_set/class_methods.rb', line 40 def without_parent with_parent(nil) end |