Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save icantrap/492597 to your computer and use it in GitHub Desktop.
Save icantrap/492597 to your computer and use it in GitHub Desktop.
Example of factory_girl factory for model that has paperclip attachment
require 'action_controller/test_process'
# Paperclip attachments in factories, made easy based on technicalpickles
Factory.class_eval do
def attach(name, path, content_type = nil)
if content_type
add_attribute name, ActionController::TestUploadedFile.new("#{RAILS_ROOT}/#{path}", content_type)
else
add_attribute name, ActionController::TestUploadedFile.new("#{RAILS_ROOT}/#{path}")
end
end
end
Factory.sequence :badge do |n|
"name#{n}"
end
Factory.define :badge do |badge|
badge.name { Factory.next :badge }
badge.active { true }
badge.description { "You get this when you are awesome" }
badge.tags { "cool awesome" }
badge.site { |badge| badge.association(:site) }
badge.attach ( "image", "test/fixtures/flower.png", "image/png" )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment