Skip to content

Commit

Permalink
CSV Import
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmarcia authored and kalimar committed Oct 4, 2018
1 parent da5342a commit 6087019
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion rails/app/models/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def picture_url

def self.import_csv(filename)
CSV.parse(filename, headers: true) do |row|
Speaker.where(name: row[0], community: row[2]).first_or_create
speaker = Speaker.where(name: row[0], community: row[2]).first_or_create
if row[3] && File.exist?(Rails.root.join('media', row[3]))
file = File.open(Rails.root.join('media',row[3]))
speaker.media.attach(io: file, filename: row[3])
speaker.save
end
end
end

Expand Down
7 changes: 6 additions & 1 deletion rails/app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ def self.import_csv(filename)
pointid = Point.where(title: row[3])&.first&.id
speakerid = Speaker.where(name: row[2])&.first&.id
perm = row[9].blank? ? "anonymous" : "user_only"
Story.create(title:row[0], point_id: pointid, speaker_id: speakerid, permission_level: perm)
story = Story.create(title:row[0], point_id: pointid, speaker_id: speakerid, permission_level: perm)
if row[8] && File.exist?(Rails.root.join('media', row[8]))
file = File.open(Rails.root.join('media',row[8]))
story.media.attach(io: file, filename: row[8])
story.save
end
end
end

Expand Down

0 comments on commit 6087019

Please sign in to comment.