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.
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With