Skip to content

Commit

Permalink
Switch from array to hash to fix increment
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperka committed Oct 12, 2023
1 parent d2183b1 commit f814a6a
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/tasks/form_rank_helpers.rake
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ namespace :rank do
end

puts "Re-ranking #{form.descendants.count} descendants..."
rank_tree = []
ranks = {}
FormItem.where(form: form).ordered_by_ancestry_and(:rank).each do |form_item|
depth = form_item.depth
next if depth.zero? # This is the root node.
rank_tree += [0] if depth > rank_tree.count
rank_tree.pop if depth < rank_tree.count
rank_tree[-1] += 1
puts "#{form_item.full_dotted_rank}: #{form_item.rank} => #{rank_tree.last}"
form_item.update!(rank: rank_tree.last)
next if form_item.depth.zero? # This is the root node.
ranks[form_item.ancestry] ||= 0
ranks[form_item.ancestry] += 1
puts "#{form_item.full_dotted_rank}: #{form_item.rank} => #{ranks[form_item.ancestry]}"
form_item.update!(rank: ranks[form_item.ancestry])
end
end
end
Expand Down

0 comments on commit f814a6a

Please sign in to comment.