Move file duplication code to concern to share

I chose `has_one_migrating` as method name for two reasons:

 1. It reflects Active Storage's method `has_one_attached`.
 2. And it has the same length as Paperclip's `has_attached_file`.
    Therefore the commits don't need any whitespace changes.

When we change it to `has_one_attached`, we will also remove the
Paperclip options which then don't need whitespace changes either.
This commit is contained in:
Maikel Linke
2022-03-28 17:16:02 +11:00
parent ec64e5c8f7
commit c36ad96acc
2 changed files with 60 additions and 24 deletions

View File

@@ -0,0 +1,57 @@
# frozen_string_literal: true
module HasMigratingFile
extend ActiveSupport::Concern
class_methods do
def has_one_migrating(name, paperclip_options = {})
# Active Storage declaration
has_one_attached name
# Backup Active Storage methods before they get overridden by Paperclip.
alias_method "active_storage_#{name}", name
alias_method "active_storage_#{name}=", "#{name}="
# Paperclip declaration
#
# This will define the `name` and `name=` methods as well.
has_attached_file name, paperclip_options
# Paperclip callback to duplicate file with Active Storage
#
# We store files with Paperclip *and* Active Storage while we migrate
# old Paperclip files to Active Storage. This enables availability
# during the migration.
after_post_process do
if public_send(name).errors.blank?
file = File.open(processed_local_file_path)
attach_file(name, file)
end
end
end
end
def attach_file(name, io)
attachable = {
io: io,
filename: public_send("#{name}_file_name"),
content_type: public_send("#{name}_content_type"),
identify: false,
}
public_send("active_storage_#{name}=", attachable)
end
private
def processed_local_file_path(name)
attachment = public_send(name)
temporary = attachment.queued_for_write[:original]
if temporary&.path.present?
temporary.path
else
attachment.path
end
end
end

View File

@@ -4,27 +4,18 @@ require 'spree/core/s3_support'
module Spree
class Image < Asset
include HasMigratingFile
validates_attachment_presence :attachment
validate :no_attachment_errors
# Active Storage declaration
has_one_attached :attachment
# Backup Active Storage methods before they get overridden by Paperclip.
alias_method :active_storage_attachment, :attachment
alias_method :active_storage_attachment=, :attachment=
# Paperclip declaration
#
# This will define the `name` and `name=` methods as well.
#
# This is where the styles are used in the app:
# - mini: used in the BackOffice: Bulk Product Edit page and Order Cycle edit page
# - small: used in the FrontOffice: Product List page
# - product: used in the BackOffice: Product Image upload modal in the Bulk Product Edit page
# and Product image edit page
# - large: used in the FrontOffice: product modal
has_attached_file :attachment,
has_one_migrating :attachment,
styles: { mini: "48x48#", small: "227x227#",
product: "240x240>", large: "600x600>" },
default_style: :product,
@@ -32,18 +23,6 @@ module Spree
path: ':rails_root/public/spree/products/:id/:style/:basename.:extension',
convert_options: { all: '-strip -auto-orient -colorspace sRGB' }
after_post_process do
if attachment.errors.blank?
attachable = {
io: File.open(local_filename_of_original),
filename: attachment_file_name,
content_type: attachment_content_type,
identify: false,
}
self.active_storage_attachment = attachable
end
end
# save the w,h of the original image (from which others can be calculated)
# we need to look at the write-queue for images which have not been saved yet
after_post_process :find_dimensions