Module: CollectiveIdea::Acts::NestedSet::Model::ClassMethods
- Defined in:
- lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb
Instance Method Summary (collapse)
- - (Object) associate_parents(objects)
- - (Object) children_of(parent_id)
-
- (Object) each_with_level(objects, &block)
Iterates over tree elements and determines the current level in the tree.
- - (Object) leaves
- - (Object) left_of(node)
- - (Object) left_of_right_side(node)
- - (Object) nested_set_scope(options = {})
- - (Object) primary_key_scope(id)
- - (Object) right_of(node)
- - (Object) root
- - (Object) roots
Instance Method Details
- (Object) associate_parents(objects)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 27 def associate_parents(objects) return objects unless objects.all? {|o| o.respond_to?(:association)} id_indexed = objects.index_by(&:id) objects.each do |object| association = object.association(:parent) parent = id_indexed[object.parent_id] if !association.loaded? && parent association.target = parent association.set_inverse_instance(parent) end end end |
- (Object) children_of(parent_id)
42 43 44 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 42 def children_of(parent_id) where arel_table[parent_column_name].eq(parent_id) end |
- (Object) each_with_level(objects, &block)
Iterates over tree elements and determines the current level in the tree. Only accepts default ordering, odering by an other column than lft does not work. This method is much more efficent than calling level because it doesn't require any additional database queries.
Example:
Category.each_with_level(Category.root.self_and_descendants) do |o, level|
54 55 56 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 54 def each_with_level(objects, &block) Iterator.new(objects).each_with_level(&block) end |
- (Object) leaves
58 59 60 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 58 def leaves nested_set_scope.where "#{quoted_right_column_full_name} - #{quoted_left_column_full_name} = 1" end |
- (Object) left_of(node)
62 63 64 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 62 def left_of(node) where arel_table[left_column_name].lt(node) end |
- (Object) left_of_right_side(node)
66 67 68 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 66 def left_of_right_side(node) where arel_table[right_column_name].lteq(node) end |
- (Object) nested_set_scope(options = {})
74 75 76 77 78 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 74 def nested_set_scope( = {}) = {:order => quoted_order_column_full_name}.merge() order(.delete(:order)).scoped end |
- (Object) primary_key_scope(id)
80 81 82 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 80 def primary_key_scope(id) where arel_table[primary_key].eq(id) end |
- (Object) right_of(node)
70 71 72 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 70 def right_of(node) where arel_table[left_column_name].gteq(node) end |
- (Object) root
84 85 86 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 84 def root roots.first end |
- (Object) roots
88 89 90 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model.rb', line 88 def roots nested_set_scope.children_of nil end |