Skip to content
This repository
Browse code

Order items in a curated list based on the CSV

any_in didn't guarantee the order of data returned.
  • Loading branch information...
commit 5564e59e65451a8239a254a08b585c484b374ebf 1 parent 2d26fc6
Jamie Cobbett authored October 16, 2012
11  app/controllers/curated_list_controller.rb
@@ -57,7 +57,7 @@ def process_data_file
57 57
           artefact_slugs = row.select {|x| !x.nil?}
58 58
           artefact_slugs.shift
59 59
           if artefact_slugs.length > 0
60  
-            curated_list.artefact_ids = Artefact.any_in(slug: artefact_slugs).collect(&:_id)
  60
+            curated_list.artefact_ids = artefacts_in_order(artefact_slugs).map(&:id)
61 61
             curated_list.save!
62 62
           else
63 63
             raise EmptyArtefactArray
@@ -69,6 +69,15 @@ def process_data_file
69 69
     end
70 70
   end
71 71
 
  72
+  def artefacts_in_order(slugs)
  73
+    artefacts_by_slug = slugs.each_with_object({}) { |v,h| h[v] = nil }
  74
+    artefacts = Artefact.any_in(slug: slugs)
  75
+    artefacts.each do |artefact|
  76
+      artefacts_by_slug[artefact.slug] = artefact
  77
+    end
  78
+    artefacts_by_slug.values.compact
  79
+  end
  80
+
72 81
   def get_file_from_param(param)
73 82
     if param.respond_to?(:tempfile)
74 83
       param.tempfile

0 notes on commit 5564e59

Please sign in to comment.
Something went wrong with that request. Please try again.