module CollectiveIdea::Acts::NestedSet::Model::ClassMethods

Public Instance Methods

add_to_inverse_association(association, record) click to toggle source
   # File lib/awesome_nested_set/model.rb
42 def add_to_inverse_association(association, record)
43   inverse_reflection = association.send(:inverse_reflection_for, record)
44   inverse = record.association(inverse_reflection.name)
45   inverse.target << association.owner
46   inverse.loaded!
47 end
associate_parents(objects) click to toggle source
   # File lib/awesome_nested_set/model.rb
27 def associate_parents(objects)
28   return objects unless objects.all? {|o| o.respond_to?(:association)}
29 
30   id_indexed = objects.index_by(&primary_column_name.to_sym)
31   objects.each do |object|
32     association = object.association(:parent)
33     parent = id_indexed[object.parent_id]
34 
35     if !association.loaded? && parent
36       association.target = parent
37       add_to_inverse_association(association, parent)
38     end
39   end
40 end
children_of(parent_id) click to toggle source
   # File lib/awesome_nested_set/model.rb
49 def children_of(parent_id)
50   where arel_table[parent_column_name].eq(parent_id)
51 end
each_with_level(objects, &block) click to toggle source

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 efficient 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|
   # File lib/awesome_nested_set/model.rb
61 def each_with_level(objects, &block)
62   Iterator.new(objects).each_with_level(&block)
63 end
leaves() click to toggle source
   # File lib/awesome_nested_set/model.rb
65 def leaves
66   nested_set_scope.where "#{quoted_right_column_full_name} - #{quoted_left_column_full_name} = 1"
67 end
left_of(node) click to toggle source
   # File lib/awesome_nested_set/model.rb
69 def left_of(node)
70   where arel_table[left_column_name].lt(node)
71 end
left_of_right_side(node) click to toggle source
   # File lib/awesome_nested_set/model.rb
73 def left_of_right_side(node)
74   where arel_table[right_column_name].lteq(node)
75 end
nested_set_scope(options = {}) click to toggle source
   # File lib/awesome_nested_set/model.rb
81 def nested_set_scope(options = {})
82   order = scope_order_from_options(options)
83   where(options[:conditions]).order(order)
84 end
primary_key_scope(id) click to toggle source
   # File lib/awesome_nested_set/model.rb
86 def primary_key_scope(id)
87   where arel_table[primary_column_name].eq(id)
88 end
right_of(node) click to toggle source
   # File lib/awesome_nested_set/model.rb
77 def right_of(node)
78   where arel_table[left_column_name].gteq(node)
79 end
root() click to toggle source
   # File lib/awesome_nested_set/model.rb
90 def root
91   roots.first
92 end
roots() click to toggle source
   # File lib/awesome_nested_set/model.rb
94 def roots
95   nested_set_scope.children_of nil
96 end

Private Instance Methods

scope_order_from_options(options) click to toggle source
    # File lib/awesome_nested_set/model.rb
100 def scope_order_from_options(options)
101   options.fetch(:order, order_column_name)
102 end