HI.
I have been using shrine and it is very helpful. Thanks to all contributors.
I have a question this time.
I found strange behavior like the topic title. Is this a bug? or is it a specification? code and reproduction procedure is as follows.
environment
shrine 3.1.0
rails 6.0.0
image_uploader.rb
require "image_processing/mini_magick"
class ImageUploader < Shrine
# OK
# plugin :derivatives
# plugin :remove_invalid
# plugin :validation
# NG
plugin :remove_invalid
plugin :derivatives
plugin :validation
Attacher.derivatives do |original|
magick = ImageProcessing::MiniMagick.source(original)
{
small: magick.resize_to_limit!(100, 100)
}
end
Attacher.validate do
# DEBUG: always fail
# errors << "fail"
end
end
shrine.rb
require "shrine"
require "shrine/storage/file_system"
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
store: Shrine::Storage::FileSystem.new("public", prefix: "uploads"), # permanent
}
class Shrine::Attacher
def promote(*)
create_derivatives
super
end
end
Shrine.plugin :activerecord
show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @user.name %>
</p>
<p>
<strong>image_url:</strong>
<%= image_tag @user.image_url %>
</p>
<p>
<strong>image(:small).url:</strong>
<%= image_tag @user.image(:small).url %>
</p>
<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>
Reproduction procedure
-
Register model.
-
Check the derivatives display.(image(:small).url)
-
Check that the original image and derivative are in storage.
-
Uncomment
# errors << "fail"
inimage_uploader.rb
-
Update model.
-
Validation error occurs.
-
Derivatives are deleted.
storage
all code is here