Changing path for integration tests on a Paperclip Attachment
Asked Answered
R

2

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.

Rainbow answered 3/5, 2016 at 13:28 Comment(0)
F
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'
Fearsome answered 27/1, 2020 at 16:50 Comment(0)
G
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.

Grillo answered 8/6, 2018 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.