Skip to content

Commit

Permalink
Setup shrine uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
galetahub committed Jul 2, 2024
1 parent e52aaf7 commit 52c04d7
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 8 deletions.
8 changes: 6 additions & 2 deletions lib/ckeditor/backend/shrine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +25,7 @@ def data_file_size
end

def datasource
@datasource ||= data&.metadata || {}
@datasource ||= attachment&.metadata || {}
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/generators/ckeditor/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ 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')

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Ckeditor::AttachmentFile < Ckeditor::Asset
def url_thumb
Ckeditor::Utils.filethumb(filename)
end
end
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CkeditorAttachmentUploader < Shrine
# plugins and uploading logic
end
7 changes: 3 additions & 4 deletions lib/generators/ckeditor/templates/base/shrine/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 52c04d7

Please sign in to comment.