gpr
4/28/2015 - 9:04 AM

Paperclip warning

Paperclip warning

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
class MyModel < ActiveRecord::Base
  has_attached_file :attachment, processors: [:text]
  validates_attachment_content_type :attachment, content_type: 'text/plain'
end

WARNING

The model must be created before to attach a file.