Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffrafter/419014 to your computer and use it in GitHub Desktop.
Save jeffrafter/419014 to your computer and use it in GitHub Desktop.
factory_girl attachments for paperclip
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
@s9gf4ult
Copy link

Dont work for Rails 4.1

.rvm/gems/ruby-2.1.0@banners/gems/factory_girl-4.4.0/lib/factory_girl/definition_proxy.rb:42:in `add_attribute': wrong number of arguments (4 for 1..2) (ArgumentError)

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