Files
nairobi/app/lib/ckeditor/backend/active_storage.rb
Javi Martín cb477149c4 Move lib folder inside the app folder
The purpose of the lib folder is to have code that doesn't necessary
belong in the application but can be shared with other applications.

However, we don't have other applications and, if we did, the way to
share code between them would be using a gem or even a git submodule.

So having both the `app/` and the `lib/` folders is confusing IMHO, and
it causes unnecessary problems with autoloading.

So we're moving the `lib/` folder to `app/lib/`. Originally, some of
these files were in the `app/services/` folder and then they were moved
to the `lib/` folder. We're using `app/lib/` instead of `app/services/`
so the upgrade is less confusing.

There's an exception, though. The `OmniAuth::Strategies::Wordpress`
class needs to be available in the Devise initializer. Since this is an
initializer and trying to autoload a class here will be problematic when
switching to Zeitwerk, we'll keep the `require` clause on top of the
Devise initializer in order to load the file and so it will be loaded
even if it isn't in the autoload paths anymore.
2024-04-11 19:08:01 +02:00

74 lines
1.8 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_validation :apply_data
validate do
if data.nil? || 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 file
@file ||= storage_data
end
def blob
@blob ||= ::ActiveStorage::Blob.find(file.attachment.blob_id)
end
def apply_data
if data.is_a?(Ckeditor::Http::QqFile)
storage_data.attach(io: data, filename: data.original_filename)
else
storage_data.attach(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