carrierwaveuploader / carrierwave

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks
https://github.com/carrierwaveuploader/carrierwave
8.78k stars 1.65k forks source link

#{column}_download_errors is not cleared on reassigning remote url #2725

Open brunosedler opened 4 months ago

brunosedler commented 4 months ago

When reassign a remote URLs, previous errors are not cleared.

Example Code (Rails)

class BlogArticle < ActiveRecord::Base
  mount_uploader :image, ImageUploader
end

class BlogArticle::ImageUploader < CarrierWave::Uploader::Base
  def extension_allowlist
    %w[jpg jpeg gif png]
  end
end

Steps to reproduce

article = BlogArticle.new

# Ensure tested URL is valid
article.remote_image_url = "https://fastly.picsum.photos/id/521/200/300.jpg?hmac=_MGlU-tHw5IBlsNL7YvJ9lTMo4Ge605GWQwuKGxWIWU"
article.valid?
# => true

Assign invalid URL
article.remote_image_url =  "https://fastly.picsum.photos/id/521/200/invalid.jpg"
article.valid?
# => false

article.image_download_errors
# => [#<CarrierWave::DownloadError: could not download file: 404 "Not Found">]

article.errors.messages[:image]
["could not download file: 404 \"Not Found\""]

# Re-assign valid URL
article.remote_image_url = "https://fastly.picsum.photos/id/521/200/300.jpg?hmac=_MGlU-tHw5IBlsNL7YvJ9lTMo4Ge605GWQwuKGxWIWU"
article.valid?
# => false 
# should be true

article.image_download_errors
# => [#<CarrierWave::DownloadError: could not download file: 404 "Not Found">]
# should be empty

article.errors.messages[:image]
# => ["could not download file: 404 \"Not Found\"]
# should be empty

Further Infos: I'm using Rails 6.1.7.2, Ruby 2.7.6 and CarrierWave 3.0.5