Module: AbAdmin::Concerns::NestedSet::ClassMethods

Defined in:
lib/ab_admin/concerns/nested_set.rb

Instance Method Summary collapse

Instance Method Details

#build_tree(records) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ab_admin/concerns/nested_set.rb', line 18

def build_tree(records)
  tree = {}
  roots = []
  records.each do |record|
    if record.root?
      roots << record
      next
    end
    tree[record.parent_id] ||= []
    tree[record.parent_id] << record
  end
  roots.each do |root|
    root.tree_children(tree)
  end
  roots
end

#nested_opts(records, item = nil) ⇒ Object


35
36
37
38
39
40
41
42
43
# File 'lib/ab_admin/concerns/nested_set.rb', line 35

def nested_opts(records, item=nil)
  item = nil if item && !item.is_a?(self)
  res = []
  records.each do |r|
    next if item && item.id == r.id
    res << ["#{'' * r.depth} #{AbAdmin.display_name(r)}", r.id]
  end
  res
end

#nested_opts_with_parent(records, item = nil) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ab_admin/concerns/nested_set.rb', line 45

def nested_opts_with_parent(records, item=nil)
  item = nil if item && !item.is_a?(self)
  res = []
  parents = []
  records.each do |r|
    r.root? ? parents = [] : parents.reject! { |p| p.depth >= r.depth }

    unless item && item.id == r.id
      res << ["#{parents.map { |c| "#{AbAdmin.display_name(c)} - " }.join} <b>#{AbAdmin.display_name(r)}</b>", r.id]
    end

    parents << r
  end
  res
end