Skip to content

Instantly share code, notes, and snippets.

@chuckg
Created January 4, 2012 00:19
Show Gist options
  • Save chuckg/1557734 to your computer and use it in GitHub Desktop.
Save chuckg/1557734 to your computer and use it in GitHub Desktop.
Rails 3: Add #attachment FactoryGirl method for defining paperclip attachments on models.
require 'action_dispatch/testing/test_process'
Factory::DefinitionProxy.class_eval do
def attachment(name, path, content_type = nil)
path_with_rails_root = "#{Rails.root}/#{path}"
uploaded_file = if content_type
Rack::Test::UploadedFile.new(path_with_rails_root, content_type)
else
Rack::Test::UploadedFile.new(path_with_rails_root)
end
add_attribute name, uploaded_file
end
end
Factory.define :application do |factory|
factory.attachment(:sample, "public/samples/sample.doc", "application/msword")
end
@chuckg
Copy link
Author

chuckg commented Jan 4, 2012

Take from the following gist and updated for Rails 3 comptability:
https://gist.github.com/162881

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