collectiveidea/awesome_nested_set

Simplification of Iterator#each_with_level

cryptogopher opened this issue · 1 comments

It looks to me that the code of each_with_level

def each_with_level
path = [nil]
objects.each do |o|
if o.parent_id != path.last
# we are on a new level, did we descend or ascend?
if path.include?(o.parent_id)
# remove wrong tailing paths elements
path.pop while path.last != o.parent_id
else
path << o.parent_id
end
end
yield(o, path.length - 1)
end
end

can be simplified to that:

 def each_with_level 
   path = [nil] 
   objects.each do |o| 
     path[path.rindex(o.parent)+1..-1] = o 
     yield(o, path.length - 1) 
   end 
 end 
stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.