1
0
This repository has been archived on 2023-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
body_tracking/lib/body_tracking/awesome_nested_set_patch.rb
2020-07-25 16:28:55 +02:00

36 lines
1.1 KiB
Ruby

module BodyTracking::AwesomeNestedSetPatch
CollectiveIdea::Acts::NestedSet.class_eval do
module CollectiveIdea::Acts::NestedSet
class Iterator
def each_with_ancestors
return to_enum(__method__) { objects.length } unless block_given?
ancestors = [nil]
objects.each do |o|
ancestors[ancestors.rindex(o.parent)+1..-1] = o
yield ancestors
end
end
end
module Model
module ClassMethods
def each_with_path(objects)
return to_enum(__method__, objects) { objects.length } unless block_given?
Iterator.new(objects).each_with_ancestors do |ancestors|
yield [ancestors.last, ancestors.map { |q| q.try(:name) }.join('::')]
end
end
def each_with_ancestors(objects)
return to_enum(__method__, objects) { objects.length } unless block_given?
Iterator.new(objects).each_with_ancestors { |ancestors| yield ancestors.dup }
end
end
end
end
end
end