Module: CollectiveIdea::Acts::NestedSet::Model::ClassMethods
- Defined in:
- lib/awesome_nested_set/model.rb
Instance Method Summary collapse
- #add_to_inverse_association(association, record) ⇒ Object
- #associate_parents(objects) ⇒ Object
- #children_of(parent_id) ⇒ Object
-
#each_with_level(objects, &block) ⇒ Object
Iterates over tree elements and determines the current level in the tree.
- #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 ⇒ Object
Instance Method Details
#add_to_inverse_association(association, record) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/awesome_nested_set/model.rb', line 42 def add_to_inverse_association(association, record) inverse_reflection = association.send(:inverse_reflection_for, record) inverse = record.association(inverse_reflection.name) inverse.target << association.owner inverse.loaded! end |
#associate_parents(objects) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File '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(&primary_column_name.to_sym) objects.each do |object| association = object.association(:parent) parent = id_indexed[object.parent_id] if !association.loaded? && parent association.target = parent add_to_inverse_association(association, parent) end end end |
#children_of(parent_id) ⇒ Object
49 50 51 |
# File 'lib/awesome_nested_set/model.rb', line 49 def children_of(parent_id) where arel_table[parent_column_name].eq(parent_id) end |
#each_with_level(objects, &block) ⇒ Object
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|
61 62 63 |
# File 'lib/awesome_nested_set/model.rb', line 61 def each_with_level(objects, &block) Iterator.new(objects).each_with_level(&block) end |
#leaves ⇒ Object
65 66 67 |
# File 'lib/awesome_nested_set/model.rb', line 65 def leaves nested_set_scope.where "#{quoted_right_column_full_name} - #{quoted_left_column_full_name} = 1" end |
#left_of(node) ⇒ Object
69 70 71 |
# File 'lib/awesome_nested_set/model.rb', line 69 def left_of(node) where arel_table[left_column_name].lt(node) end |
#left_of_right_side(node) ⇒ Object
73 74 75 |
# File 'lib/awesome_nested_set/model.rb', line 73 def left_of_right_side(node) where arel_table[right_column_name].lteq(node) end |
#nested_set_scope(options = {}) ⇒ Object
81 82 83 84 |
# File 'lib/awesome_nested_set/model.rb', line 81 def nested_set_scope( = {}) order = () where([:conditions]).order(order) end |
#primary_key_scope(id) ⇒ Object
86 87 88 |
# File 'lib/awesome_nested_set/model.rb', line 86 def primary_key_scope(id) where arel_table[primary_column_name].eq(id) end |
#right_of(node) ⇒ Object
77 78 79 |
# File 'lib/awesome_nested_set/model.rb', line 77 def right_of(node) where arel_table[left_column_name].gteq(node) end |
#root ⇒ Object
90 91 92 |
# File 'lib/awesome_nested_set/model.rb', line 90 def root roots.first end |
#roots ⇒ Object
94 95 96 |
# File 'lib/awesome_nested_set/model.rb', line 94 def roots nested_set_scope.children_of nil end |