Skip to content

Instantly share code, notes, and snippets.

@marten
Created August 28, 2015 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marten/eeeed1341ed381e13044 to your computer and use it in GitHub Desktop.
Save marten/eeeed1341ed381e13044 to your computer and use it in GitHub Desktop.
require 'csv'
#prod
project = Project.find(593)
zoo_user = User.find(1)
subject_set = SubjectSet.find(681)
#staging
# project = Project.find(450)
# zoo_user = User.find(27)
# subject_set = SubjectSet.find(2725)
#cam's dev box
# subject_set = SubjectSet.find(2435)
if project && zoo_user && subject_set
subjects = []
failed_subjects = []
CSV.foreach("#{Rails.root}/hhmi-23k.csv", headers: true) do |row|
header, external_src = row.delete("FileName")
ext = external_src.split(".").last
metadata = row.to_hash
content_type = case ext
when /jpg/i
"image/jpeg"
else
nil
end
if external_src.nil? || content_type.nil? || metadata.empty?
raise "No src / content type from file"
end
location = Medium.where(type: 'subject_location', external_link: true, src: external_src, content_type: content_type).first
if location
subject = location.linked
else
subject = Subject.new(project_id: project.id, upload_user_id: zoo_user.id, metadata: metadata)
location_params = { content_type: content_type, external_link: true, src: external_src, metadata: { index: 0 }}
subject.locations.build(location_params)
end
if subject.save
subjects << subject
else
failed_subjects << subject
end
end
#cleanup if failure
unless failed_subjects.empty?
# subjects.map(&:destroy)
puts "failed to create some fo the subjects..."
#check these here
binding.pry
end
#now link to the subject sets
subject_ids_to_link = SetMemberSubject.where(subject_set_id: subject_set.id).where.not(subject_id: subjects.map(&:id)).pluck(:subject_id)
if Subject.where(id: subject_ids_to_link).count != subject_ids_to_link.size
raise "Error: check the subject set and all the subjects exist."
end
new_sms_values = subject_ids_to_link.map do |subject_id|
[ subject_set.id, subject_id, rand ]
end
sset_import_cols = %w(subject_set_id subject_id random)
SetMemberSubject.import sset_import_cols, new_sms_values, validate: false
SubjectSet.reset_counters(subject_set.id, :set_member_subjects)
puts "Created #{subjects.size} subjects"
else
puts "fail - couldn't find a project, user or subject set to link to"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment