Module: CollectiveIdea::Acts::NestedSet::Model::ClassMethods

Defined in:
lib/awesome_nested_set/model.rb

Instance Method Summary collapse

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 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|


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

#leavesObject



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
85
# File 'lib/awesome_nested_set/model.rb', line 81

def nested_set_scope(options = {})
  options = {:order => quoted_order_column_full_name}.merge(options)

  where(options[:conditions]).order(options.delete(:order))
end

#primary_key_scope(id) ⇒ Object



87
88
89
# File 'lib/awesome_nested_set/model.rb', line 87

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

#rootObject



91
92
93
# File 'lib/awesome_nested_set/model.rb', line 91

def root
  roots.first
end

#rootsObject



95
96
97
# File 'lib/awesome_nested_set/model.rb', line 95

def roots
  nested_set_scope.children_of nil
end