Files
grecia/lib/ckeditor/backend/active_storage.rb
Javi Martín 1290e2ecd3 Store files with both Paperclip and ActiveStorage
In order to migrate existing files from Paperclip to ActiveStorage, we
need Paperclip to find out the files associated to existing database
records. So we can't simply replace Paperclip with ActiveStorage.

That's why it's usually recommended [1] to first run the migration and
then replace Paperclip with ActiveStorage using two consecutive
deployments.

However, in our case we can't rely on two consecutive deployments
because we have to make an easy process so existing CONSUL installations
don't run into any issues. We can't just release version 1.4.0 and 1.5.0
and day and ask everyone to upgrade twice on the same day.

Instead, we're following a different plan:

* We're going to provide a Rake task (which will require Paperclip) to
  migrate existing files
* We still use Paperclip to generate link and image tags
* New files are handled using both Paperclip and ActiveStorage; that
  way, when we make the switch, we won't have to migrate them, and in
  the meantime they'll be accessible thanks to Paperclip
* After we make the switch, we'll update the `name` column in the active
  storage attachments tables in order to remove the `storage_` prefix

Regarding our handling of new files, the exception are cached
attachments. Since those attachments are temporary files used while
submitting a form and we have to delete them afterwards, we're only
handling them with Paperclip. We'll handle these ones in version 1.5.0.

Note the task creating the dev seeds was failing after these changes
with an `ActiveStorage::IntegrityError` exception because we were
opening some files without closing them. If the same file was attached
twice, it failed the second time.

We're solving it by closing the files with `File.open` and a block. Even
though we didn't get any errors, we're doing the same thing in the
`Attachable` concern because it's a good practice to close files after
we're done with them.

Also note we have to change the CKEditor Active Storage code so it's
compatible with Paperclip. In this case, I haven't been able to write a
test to confirm the attachment exists; I was getting the same
`ActiveStorage::IntegrityError` mentioned above.

Finally, we're updating the site customization image controller to use
`update` so the image and the attachment are updated within the same
transaction. This is also what we do in most controllers.

[1] https://www.youtube.com/watch?v=tZ_WNUytO9o
2021-09-24 13:39:15 +02:00

80 lines
2.2 KiB
Ruby

# frozen_string_literal: true
# Code copied from the ckeditor gem:
# https://github.com/galetahub/ckeditor/pull/853
module Ckeditor
module Backend
module ActiveStorage
def self.included(base)
base.send(:include, Rails.application.routes.url_helpers)
base.send(:include, InstanceMethods)
base.send(:extend, ClassMethods)
end
module ClassMethods
def self.extended(base)
base.class_eval do
before_save :apply_data
validate do
if data.nil? || storage_file.nil?
errors.add(:data, :not_data_present, message: "data must be present")
end
end
end
end
end
module InstanceMethods
def url
rails_blob_path(self.storage_data, only_path: true)
end
def path
rails_blob_path(self.storage_data, only_path: true)
end
def styles
end
def content_type
self.storage_data.content_type
end
def content_type=(_content_type)
self.storage_data.content_type = _content_type
end
protected
def storage_file
@storage_file ||= storage_data
end
def blob
@blob ||= ::ActiveStorage::Blob.find(storage_file.attachment.blob_id)
end
def apply_data
non_paperclip_data = if data.is_a?(::Paperclip::Attachment)
file.instance_variable_get("@target")
else
data
end
if non_paperclip_data.is_a?(Ckeditor::Http::QqFile)
storage_data.attach(io: non_paperclip_data, filename: non_paperclip_data.original_filename)
else
storage_data.attach(non_paperclip_data)
end
self.data_file_name = storage_data.blob.filename
self.data_content_type = storage_data.blob.content_type
self.data_file_size = storage_data.blob.byte_size
end
end
end
autoload :ActiveStorage, "ckeditor/backend/active_storage"
end
end