Paperclip and Factory Girl Rails both of them are my loved gems.
Unfortunately Factory Girl Rails can't create the model if the model had been Papercliped. Well at least not in Rails 3.0.7. But the good news is that it can be solved by adding a few lines of Ruby code to our factory.
This is my model:
class Club < ActiveRecord::Base
has_many :dancers
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 1.megabytes
end
So in order to use the factory with ease we need to tell our factory to upload some pictures into our model.
Therefore let me show you an example how to write the factory which makes the two gems to co-work:
include ActionDispatch::TestProcess
class Club
attr_accessor :photo_file_name
attr_accessor :photo_file_size
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:url => "/test_can_be_deleted/:attachment/:id/:style/:filename",
:path => ":rails_root/public/test_can_be_deleted/:attachment/:id/:style/:filename"
end
Factory.define :club do |c|
c.title 'Limpex'
c.sefurl 'Limpasm'
c.name 'Limpasm'
c.metadescription 'good etc'
c.content '<p> a good place</p><p>the icecream is prety damn good</p>'
c.state 0
c.city 'Gheorgheni'
c.email 'some@somplexx.com'
c.photo { fixture_file_upload( 'spec/factories/test.png', 'image/png') }
end
As you can observe I have just reopened my model and have redefined the paperclip attachment to point to a different path in order to store the dev and test mode attachments in different places.
That's it. :)