Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing path for integration tests on a Paperclip Attachment

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.

like image 915
Sebastian Avatar asked May 03 '16 13:05

Sebastian


2 Answers

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'
like image 91
Luke Avatar answered Sep 20 '22 12:09

Luke


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.

like image 26
Alex A Avatar answered Sep 19 '22 12:09

Alex A