<NME> bulk_invoice_job.rb
<BEF> # frozen_string_literal: true

class BulkInvoiceJob < ActiveJob::Base
  def perform(order_ids, filepath)
    pdf = CombinePDF.new

    sorted_orders(order_ids).each do |order|
      invoice = renderer.render_to_string(order)

      pdf << CombinePDF.parse(invoice)
    end

    pdf.save filepath
  end

  private

  # Ensures the records are returned in the same order the ids were originally given in
  def sorted_orders(order_ids)
    Spree::Order.where(id: order_ids).to_a.sort_by{ |order| order_ids.index(order.id) }
  end

  def renderer
    @renderer ||= InvoiceRenderer.new
  end
end

<MSG> clearer intention

- it uses simple index_by and map which is easier to understand than each_with_index.to_h
- it avoids sorting and therefore should be the most efficient solution.

Co-authored-by: Maikel <3db1433c7298ef0d91dd12baeaaf90dc122deafc@email.org.au>
<DFF> @@ -17,7 +17,8 @@ class BulkInvoiceJob < ActiveJob::Base
 
   # Ensures the records are returned in the same order the ids were originally given in
   def sorted_orders(order_ids)
-    Spree::Order.where(id: order_ids).to_a.sort_by{ |order| order_ids.index(order.id) }
+    orders_by_id = Spree::Order.where(id: order_ids).to_a.index_by(&:id)
+    order_ids.map { |id| orders_by_id[id] }
   end
 
   def renderer

{'commit': 'diff --git a/app/jobs/bulk_invoice_job.rb b/app/jobs/bulk_invoice_job.rb\nindex 3a1969c49..29e68c108 100644\n--- a/app/jobs/bulk_invoice_job.rb\n+++ b/app/jobs/bulk_invoice_job.rb\n@@ -17,7 +17,8 @@ class BulkInvoiceJob < ActiveJob::Base\n \n   # Ensures the records are returned in the same order the ids were originally given in\n   def sorted_orders(order_ids)\n-    Spree::Order.where(id: order_ids).to_a.sort_by{ |order| order_ids.index(order.id) }\n+    orders_by_id = Spree::Order.where(id: order_ids).to_a.index_by(&:id)\n+    order_ids.map { |id| orders_by_id[id] }\n   end\n \n   def renderer\n', 'commit_subject': 'clearer intention', 'repo_name': 'openfoodfoundation/openfoodnetwork', 'committer': {'name': 'GitHub', 'email': '1505422b2465e9a84f6fdfaa161078890c593f06@github.com', 'time_sec': '1640163814', 'tz_offset': '60', 'date': {'seconds': '1640163814'}}, 'commit_tree': 'f09e3247739583b5eeaa801c98001631b568400b', 'commit_message': 'clearer intention\r\n\r\n- it uses simple index_by and map which is easier to understand than each_with_index.to_h\r\n- it avoids sorting and therefore should be the most efficient solution.\n\nCo-authored-by: Maikel <3db1433c7298ef0d91dd12baeaaf90dc122deafc@email.org.au>', 'language_name': 'JavaScript', 'license': 'agpl-3.0', 'hunks': '@@ -17,7 +17,8 @@ class BulkInvoiceJob < ActiveJob::Base\n \n   # Ensures the records are returned in the same order the ids were originally given in\n   def sorted_orders(order_ids)\n-    Spree::Order.where(id: order_ids).to_a.sort_by{ |order| order_ids.index(order.id) }\n+    orders_by_id = Spree::Order.where(id: order_ids).to_a.index_by(&:id)\n+    order_ids.map { |id| orders_by_id[id] }\n   end\n \n   def renderer\n', 'addition_count': 2, 'deletion_count': 1, 'src_file': 'a/app/jobs/bulk_invoice_job.rb', 'tgt_file': 'b/app/jobs/bulk_invoice_job.rb', 'file_extension': '.rb', 'before_file': '# frozen_string_literal: true\n\nclass BulkInvoiceJob < ActiveJob::Base\n  def perform(order_ids, filepath)\n    pdf = CombinePDF.new\n\n    sorted_orders(order_ids).each do |order|\n      invoice = renderer.render_to_string(order)\n\n      pdf << CombinePDF.parse(invoice)\n    end\n\n    pdf.save filepath\n  end\n\n  private\n\n  # Ensures the records are returned in the same order the ids were originally given in\n  def sorted_orders(order_ids)\n    Spree::Order.where(id: order_ids).to_a.sort_by{ |order| order_ids.index(order.id) }\n  end\n\n  def renderer\n    @renderer ||= InvoiceRenderer.new\n  end\nend\n', 'message': 'clearer intention\r\n\r\n- it uses simple index_by and map which is easier to understand than each_with_index.to_h\r\n- it avoids sorting and therefore should be the most efficient solution.\n\nCo-authored-by: Maikel <3db1433c7298ef0d91dd12baeaaf90dc122deafc@email.org.au>', 'author_name': 'GitHub', 'lang': 'rb'}