Module: LoyalCore::ActsAsTree::ExtendsMethods

Defined in:
lib/loyal_core/acts/tree.rb

Instance Method Summary collapse

Instance Method Details

#nested_set_options(class_or_item = nil, mover = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/loyal_core/acts/tree.rb', line 69

def nested_set_options(class_or_item=nil, mover = nil)
  class_or_item ||= self

  if class_or_item.is_a? Array
    items = class_or_item.reject { |e| !e.root? }
  else
    class_or_item = class_or_item.roots if class_or_item.respond_to?(:scoped)
    items = Array(class_or_item)
  end

  result = []

  items.each do |root|
    result += root.class.associate_parents(root.self_and_descendants).map do |i|
      if mover.nil? || mover.new_record? || mover.move_possible?(i)
        [yield(i), i.id]
      end
    end.compact
  end

  result
end

#nested_set_select_options(class_or_item = nil, mover = nil, options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/loyal_core/acts/tree.rb', line 92

def nested_set_select_options(class_or_item=nil, mover=nil, options={})
  result = []

  result << ['- - - -', nil] if options[:include_blank]

  result += nested_set_options(class_or_item, mover) do |item|
    "#{'- ' * item.level}#{item.name}"
  end
end