7

The documentation for Paperclip mentions that you can change the upload path for tests by placing the following code in the test.rb environment file:

Paperclip::Attachment.default_options[:path] = "#{Rails.root}/spec/test_files/:class/:id_partition/:style.:extension"

The issue I'm having is that the Attachment has a path set in the model, that doesn't get overwritten:

has_attached_file :photo, path: ':attachment/:id/:style.:extension'

When I'm running the tests the files get uploaded to the /photo/ folder instead of /spec/test_files/.

I can probably achieve this by writing a custom Paperclip adapter, but there must be an easier way.

Sebastian
  • 2,154
  • 1
  • 26
  • 42

2 Answers2

3

I assume this is well past you needing help :) but on the off chance this helps someone else - you can use paperclip interpolations e.g.

# config/initializers/paperclip.rb
Paperclip.interpolates :path_prefix do |_attachment, _style|
  if Rails.env.test?
    Rails.root.join("spec/test_files/")
  else
    ""
  end
end

Then update your custom path to do use the prefix:

has_attached_file :photo, path: ':path_prefix:attachment/:id/:style.:extension'
Luke
  • 1,639
  • 15
  • 16
1

I had a similar issue, i couldn't create the folder, but instead of placing this:

Paperclip::Attachment.default_options[:path] = "#{Rails.root}/spec/test_files/:class/:id_partition/:style.:extension"

in the environments/test.rb as the instructions were saying, i added it in the rails_helper.rb

Hope it helps.

Alex A
  • 156
  • 1
  • 10