Handle CKEditor attachments with Active Storage

The code is based on what's generated using CKEditor's code generator.

We're doing one minor change to the `Ckeditor::Backend::ActiveStorage`
module; we're assigning the data in a `before_validation` instead of a
`before_save` callback. Validations with `file_validations` didn't work
otherwise; it looks like this backend was written with
`active_storage_validations` in mind [1].

Note we don't need to update the `name` column in the attachments table
because, when using Active Storage, CKEditor uses both `data` (as
attribute accessor) and `storage_data` (as attachment attribute).

[1] https://github.com/galetahub/ckeditor/blob/f9e48420ccb6dc/lib/generators/ckeditor/templates/active_record/active_storage/ckeditor/picture.rb#L4
This commit is contained in:
Javi Martín
2021-07-28 00:25:02 +02:00
parent 600f5c35e9
commit 8c82ff290b
3 changed files with 15 additions and 32 deletions

View File

@@ -14,9 +14,9 @@ module Ckeditor
module ClassMethods
def self.extended(base)
base.class_eval do
before_save :apply_data
before_validation :apply_data
validate do
if data.nil? || storage_file.nil?
if data.nil? || file.nil?
errors.add(:data, :not_data_present, message: "data must be present")
end
end
@@ -46,25 +46,19 @@ module Ckeditor
protected
def storage_file
@storage_file ||= storage_data
def file
@file ||= storage_data
end
def blob
@blob ||= ::ActiveStorage::Blob.find(storage_file.attachment.blob_id)
@blob ||= ::ActiveStorage::Blob.find(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)
if data.is_a?(Ckeditor::Http::QqFile)
storage_data.attach(io: data, filename: data.original_filename)
else
storage_data.attach(non_paperclip_data)
storage_data.attach(data)
end
self.data_file_name = storage_data.blob.filename