Skip to content

Instantly share code, notes, and snippets.

@gpr
Last active August 29, 2015 14:20
Show Gist options
  • Save gpr/8ea2a4720a2fe13891c8 to your computer and use it in GitHub Desktop.
Save gpr/8ea2a4720a2fe13891c8 to your computer and use it in GitHub Desktop.
Paperclip warning

WARNING

The model must be created before to attach a file.

class MyModel < ActiveRecord::Base
has_attached_file :attachment, processors: [:text]
validates_attachment_content_type :attachment, content_type: 'text/plain'
end
require 'test_helper'
require 'faker'
describe MyModel do
it 'fails to store the attachment at the right location' do
file = Tempfile.new(['test_file', '.txt'], '/tmp')
file.write(Faker::Lorem.sentence)
file.close
mm = MyModel.create(attachment: File.open(file))
File.exist?(mm.attachment.path).must_equal true
file.unlink
end
it 'successfully stores the attachment at the right location' do
file = Tempfile.new(['test_file', '.txt'], '/tmp')
file.write(Faker::Lorem.sentence)
file.close
mm = MyModel.create(name: 'test')
mm.attachment = File.open(file)
mm.save
File.exist?(mm.attachment.path).must_equal true
file.unlink
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment