Last active
August 19, 2019 20:14
-
-
Save spencerldixon/8fcf317aca9c72114de19ae8ad7d6d60 to your computer and use it in GitHub Desktop.
Robust CSV Importer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CsvImporterJob < ApplicationJob | |
queue_as :default | |
# Handle any cleanup after perform | |
after_perform { |job| clean_up_csv_upload(job) } | |
def perform(file, user) | |
# Use begin/rescue - raise StandardError in importer if something goes wrong | |
# Rescue here with email notification to user | |
begin | |
CsvImporter.import(file, user) | |
NotificationMailer.csv_import_successful(user).deliver_later | |
rescue StandardError => error | |
NotificationMailer.csv_import_failed(user, error.message).deliver_later | |
end | |
end | |
def clean_up_csv_upload(job) | |
# Clean up the file from ActiveStorage | |
csv_upload = job.arguments.first | |
csv_upload.destroy | |
end | |
end |
And for mailers...
require 'rails_helper'
RSpec.describe NotificationMailer, type: :mailer do
let(:user) { FactoryBot.create(:user) }
describe '#csv_import_successful' do
let(:mail) { NotificationMailer.csv_import_successful(user) }
it 'renders the headers' do
expect(mail.subject).to eq('Import successful')
expect(mail.to).to eq([user.email])
end
it 'renders the body' do
expect(mail.body.encoded).to match('Your csv has been imported!')
end
end
describe '#csv_import_failed' do
let(:mail) { NotificationMailer.csv_import_failed(user, 'error message') }
it 'renders the headers' do
expect(mail.subject).to eq('Import failed')
expect(mail.to).to eq([user.email])
end
it 'renders the body' do
expect(mail.body.encoded).to match('Something went wrong')
end
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tests for this Job...