Skip to content

Instantly share code, notes, and snippets.

@jeshuaborges
Created November 27, 2012 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeshuaborges/4156261 to your computer and use it in GitHub Desktop.
Save jeshuaborges/4156261 to your computer and use it in GitHub Desktop.
require 'upload_store'
class PhotoClaimer
class FileNotFound < RuntimeError; end
def initialize(user_id, file_name)
@user_id = user_id
@file_name = file_name
end
# Public: Do it!
# Returns a Photo instance.
def claim
photo = create_record
delete_file
photo
end
private
def create_record
photo = Photo.create!(user_id: @user_id)
# parent model must have identity before saving because the id
# is used for the file path.
photo.image.store!(tempfile)
photo.save!
end
def tempfile
tmp = Tempfile.new(@file_name)
tmp.write(uploaded_file.body)
tmp.rewind
tmp
end
def delete_file
uploaded_file.destroy
end
# Internal: Find the associated uploaded file.
#
# Returns: Fog::File instance.
# Raises FileNotFound error when file cannot be located.
def uploaded_file
@uploaded_file ||= UploadStore.get(@file_name)
raise FileNotFound, "#{@file_name} not found" unless @uploaded_file
@uploaded_file
end
end
require_relative '../../../app/domain/photo_claimer'
describe PhotoClaimer do
let(:claimer) { PhotoClaimer.new(1, 'some/path.jpeg') }
before do
stub_const('Photo', double(:photo_class).as_null_object)
stub_const('Tempfile', double(:tempfile_class).as_null_object)
stub_const('UploadStore', double(:uploadstore_class).as_null_object)
end
it 'errors when file cannot be found' do
UploadStore.stub(:get) { nil }
expect{ claimer.claim }.to raise_error(PhotoClaimer::FileNotFound)
end
it 'deletes the uploaded file' do
file = double(:file).as_null_object
UploadStore.stub(:get) { file }
file.should_receive(:destroy)
claimer.claim
end
it 'creates a photo record' do
Photo.should_receive(:create!)
claimer.claim
end
it 'assigns attaches uploaded file' do
tempfile = double(:tempfile).as_null_object
uploader = double(:uploader)
photo = double(:photo, image: uploader)
Photo.stub!(:create!) { photo }
Tempfile.stub(:new) { tempfile }
uploader.should_receive(:store!).with(tempfile)
photo.should_receive(:save!)
claimer.claim
end
end
@jeshuaborges
Copy link
Author

Photo#image is a Carrierwave uploader, ImageUploader. This class depends on Photo object with a unique id such that it can know where to store the files. There are two options here. We can ignore the coupling problem illustrated in our spec, 'assigns attaches uploaded file...', or we can find a way to create a new unique id which is used to create a storage location.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment