Skip to content

Commit

Permalink
Merge pull request #17325 from NickLaMuro/flatten_arranged_rels_optim…
Browse files Browse the repository at this point in the history
…ization

Remove N+1 obj creation in flatten_arranged_rels
  • Loading branch information
kbrock authored May 4, 2018
2 parents 2aba99c + fcfd86d commit 32fadeb
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ def resource_pair
#

def self.flatten_arranged_rels(relationships)
relationships.each_with_object([]) do |(rel, children), a|
a << rel
a.concat(flatten_arranged_rels(children))
result = relationships.keys
remaining_children = relationships.values
until remaining_children.empty?
remaining_children.pop.each do |rel, kids|
result << rel
remaining_children << kids
end
end
result
end

def self.arranged_rels_to_resources(relationships, initial = true)
Expand Down

0 comments on commit 32fadeb

Please sign in to comment.