Permalink
Browse files

Order items in a curated list based on the CSV

any_in didn't guarantee the order of data returned.
  • Loading branch information...
jamiecobbett committed Oct 16, 2012
1 parent 2d26fc6 commit 5564e59e65451a8239a254a08b585c484b374ebf
Showing with 10 additions and 1 deletion.
  1. +10 −1 app/controllers/curated_list_controller.rb
@@ -57,7 +57,7 @@ def process_data_file
artefact_slugs = row.select {|x| !x.nil?}
artefact_slugs.shift
if artefact_slugs.length > 0
- curated_list.artefact_ids = Artefact.any_in(slug: artefact_slugs).collect(&:_id)
+ curated_list.artefact_ids = artefacts_in_order(artefact_slugs).map(&:id)
curated_list.save!
else
raise EmptyArtefactArray
@@ -69,6 +69,15 @@ def process_data_file
end
end
+ def artefacts_in_order(slugs)
+ artefacts_by_slug = slugs.each_with_object({}) { |v,h| h[v] = nil }
+ artefacts = Artefact.any_in(slug: slugs)
+ artefacts.each do |artefact|
+ artefacts_by_slug[artefact.slug] = artefact
+ end
+ artefacts_by_slug.values.compact
+ end
+
def get_file_from_param(param)
if param.respond_to?(:tempfile)
param.tempfile

0 comments on commit 5564e59

Please sign in to comment.