diff --git a/lib/ckeditor/backend/shrine.rb b/lib/ckeditor/backend/shrine.rb index a37a95e15..532186e72 100644 --- a/lib/ckeditor/backend/shrine.rb +++ b/lib/ckeditor/backend/shrine.rb @@ -8,8 +8,12 @@ def self.included(base) end module InstanceMethods + def data=(value) + self.attachment_data = value + end + def url - data_url + attachment.url end def data_file_name @@ -21,7 +25,7 @@ def data_file_size end def datasource - @datasource ||= data&.metadata || {} + @datasource ||= attachment&.metadata || {} end end end diff --git a/lib/generators/ckeditor/install_generator.rb b/lib/generators/ckeditor/install_generator.rb index 4f5effdd0..80b1ab403 100644 --- a/lib/generators/ckeditor/install_generator.rb +++ b/lib/generators/ckeditor/install_generator.rb @@ -47,7 +47,9 @@ def create_models template "#{generator_dir}/ckeditor/#{filename}.rb", File.join('app/models', ckeditor_dir, "#{filename}.rb") end + end + def create_uploaders if backend_carrierwave? template "#{uploaders_dir}/uploaders/ckeditor_attachment_file_uploader.rb", File.join('app/uploaders', 'ckeditor_attachment_file_uploader.rb') @@ -55,6 +57,11 @@ def create_models template "#{uploaders_dir}/uploaders/ckeditor_picture_uploader.rb", File.join('app/uploaders', 'ckeditor_picture_uploader.rb') end + + if backend_shrine? + template "base/shrine/attachment_uploader.rb", + File.join('app/uploaders', 'ckeditor_attachment_uploader.rb') + end end def create_ckeditor_migration diff --git a/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/asset.rb b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/asset.rb new file mode 100644 index 000000000..37dd78041 --- /dev/null +++ b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/asset.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class Ckeditor::Asset < ActiveRecord::Base + include Ckeditor::Orm::ActiveRecord::AssetBase + include Ckeditor::Backend::Shrine + + include CkeditorAttachmentUploader::Attachment(:attachment) +end diff --git a/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/attachment_file.rb b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/attachment_file.rb new file mode 100644 index 000000000..dc315b5d1 --- /dev/null +++ b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/attachment_file.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Ckeditor::AttachmentFile < Ckeditor::Asset + def url_thumb + Ckeditor::Utils.filethumb(filename) + end +end diff --git a/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/picture.rb b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/picture.rb new file mode 100644 index 000000000..e71046ddc --- /dev/null +++ b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/picture.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class Ckeditor::Picture < Ckeditor::Asset + def url_content + attachment.url + end + + def url_thumb + attachment.url + end +end diff --git a/lib/generators/ckeditor/templates/active_record/shrine/migration.rb b/lib/generators/ckeditor/templates/active_record/shrine/migration.rb new file mode 100644 index 000000000..cf8a9ca4f --- /dev/null +++ b/lib/generators/ckeditor/templates/active_record/shrine/migration.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class CreateCkeditorAssets < ActiveRecord::Migration[6.1] + def up + create_table :ckeditor_assets do |t| + t.integer :data_size + t.string :type, limit: 30 + + # Shrine column + t.text :attachment_data + + # Uncomment these to save image dimensions, if your need them. + # t.integer :data_width + # t.integer :data_height + + t.timestamps null: false + end + + add_index :ckeditor_assets, :type + end + + def down + drop_table :ckeditor_assets + end +end diff --git a/lib/generators/ckeditor/templates/base/shrine/attachment_uploader.rb b/lib/generators/ckeditor/templates/base/shrine/attachment_uploader.rb new file mode 100644 index 000000000..0a36f5e2b --- /dev/null +++ b/lib/generators/ckeditor/templates/base/shrine/attachment_uploader.rb @@ -0,0 +1,3 @@ +class CkeditorAttachmentUploader < Shrine + # plugins and uploading logic +end diff --git a/lib/generators/ckeditor/templates/base/shrine/initializer.rb b/lib/generators/ckeditor/templates/base/shrine/initializer.rb index 981e36f56..56dac3d3f 100644 --- a/lib/generators/ckeditor/templates/base/shrine/initializer.rb +++ b/lib/generators/ckeditor/templates/base/shrine/initializer.rb @@ -8,16 +8,15 @@ require 'image_processing/mini_magick' SHRINE_PICTURE_PROCESSOR = ImageProcessing::MiniMagick -# require 'image_processing/vips' -# SHRINE_PICTURE_PROCESSOR = ImageProcessing::Vips - Shrine.storages = { cache: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/cache'), store: Shrine::Storage::FileSystem.new('public', prefix: 'system') } -Shrine.plugin :determine_mime_type Shrine.plugin :mongoid +Shrine.plugin :activerecord + +Shrine.plugin :determine_mime_type Shrine.plugin :instrumentation Shrine.plugin :validation_helpers diff --git a/test/support/helpers.rb b/test/support/helpers.rb index 3a2987d28..7971d6a3a 100644 --- a/test/support/helpers.rb +++ b/test/support/helpers.rb @@ -9,7 +9,7 @@ class ActiveSupport::TestCase def new_attachment(data = nil) data ||= fixture_file_upload('files/rails.tar.gz', 'application/x-gzip') - Ckeditor.attachment_file_model.new(:data => data) + Ckeditor.attachment_file_model.new(data: data) end def create_attachment(data = nil) @@ -21,7 +21,7 @@ def create_attachment(data = nil) def new_picture(data = nil) data ||= fixture_file_upload('files/rails.png', 'image/png') - Ckeditor.picture_model.new(:data => data) + Ckeditor.picture_model.new(data: data) end def create_picture(data = nil)