Skip to content

Commit

Permalink
Add uploader for shrine attachment toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
galetahub committed Jul 2, 2024
1 parent 52c04d7 commit b7381e2
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ test/dummy/public/uploads/*
.DS_Store
Gemfile.lock
dragonfly.log
public/
4 changes: 2 additions & 2 deletions lib/ckeditor/backend/shrine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def self.included(base)

module InstanceMethods
def data=(value)
self.attachment_data = value
self.attachment = value
end

def url
attachment.url
attachment&.url
end

def data_file_name
Expand Down
8 changes: 4 additions & 4 deletions lib/generators/ckeditor/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ def create_models

def create_uploaders
if backend_carrierwave?
template "#{uploaders_dir}/uploaders/ckeditor_attachment_file_uploader.rb",
template "#{uploaders_dir}/ckeditor_attachment_file_uploader.rb",
File.join('app/uploaders', 'ckeditor_attachment_file_uploader.rb')

template "#{uploaders_dir}/uploaders/ckeditor_picture_uploader.rb",
template "#{uploaders_dir}/ckeditor_picture_uploader.rb",
File.join('app/uploaders', 'ckeditor_picture_uploader.rb')
end

if backend_shrine?
template "base/shrine/attachment_uploader.rb",
template "#{uploaders_dir}/ckeditor_attachment_uploader.rb",
File.join('app/uploaders', 'ckeditor_attachment_uploader.rb')
end
end
Expand Down Expand Up @@ -94,7 +94,7 @@ def generator_dir
end

def uploaders_dir
@uploaders_dir ||= 'base/carrierwave'
@uploaders_dir ||= "base/#{backend}/uploaders"
end

def orm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class Ckeditor::Asset < ActiveRecord::Base
include Ckeditor::Backend::Shrine

include CkeditorAttachmentUploader::Attachment(:attachment)

validates :attachment_data, presence: true
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

class Ckeditor::Picture < Ckeditor::Asset
def url_content
attachment.url
attachment&.url
end

def url_thumb
attachment.url
attachment&.url
end
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CkeditorAttachmentUploader < Shrine
plugin :validation_helpers

Attacher.validate do
validate_max_size 5*1024*1024 # file size must not be greater than 5 MB
validate_min_size 1024 # file size must not be less than 1 KB
end
end
3 changes: 2 additions & 1 deletion test/controllers/attachment_files_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class AttachmentFilesControllerTest < ActionController::TestCase
tests Ckeditor::AttachmentFilesController

def setup
@attachment = fixture_file_upload('files/rails.tar.gz', 'application/x-gzip')
@attachment = Rack::Test::UploadedFile.new('test/dummy/test/fixtures/files/rails.tar.gz', 'application/x-gzip')

@routes = Ckeditor::Engine.routes
end

Expand Down
2 changes: 1 addition & 1 deletion test/controllers/pictures_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PicturesControllerTest < ActionController::TestCase
tests Ckeditor::PicturesController

def setup
@image = fixture_file_upload('files/rails.png', 'image/png')
@image = Rack::Test::UploadedFile.new('test/dummy/test/fixtures/files/rails.png', 'image/png')
@routes = Ckeditor::Engine.routes
end

Expand Down
4 changes: 2 additions & 2 deletions test/models/picture_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def teardown
test 'Set file content_type and size' do
@picture = create_picture

assert_equal 'rails.png', @picture.data_file_name unless CKEDITOR_BACKEND == :shrine
assert_equal 'rails.png', @picture.data_file_name

case CKEDITOR_BACKEND
when :dragonfly
assert @picture.url_thumb.include?('thumb_rails')
when :active_storage
assert @picture.url_thumb =~ /\/representations\/.*\/rails.png/
when :shrine
assert @picture.url_thumb =~ /\S{32}\.png/
assert @picture.data_file_name =~ /image_processing(\d{8})-(\d{5})-(\S{,7})\.png/
else
assert @picture.url_thumb.include?('thumb_rails.png')
end
Expand Down
4 changes: 2 additions & 2 deletions test/support/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ActiveSupport::TestCase
include ActionDispatch::TestProcess

def new_attachment(data = nil)
data ||= fixture_file_upload('files/rails.tar.gz', 'application/x-gzip')
data ||= Rack::Test::UploadedFile.new('test/dummy/test/fixtures/files/rails.tar.gz', 'application/x-gzip')

Ckeditor.attachment_file_model.new(data: data)
end
Expand All @@ -19,7 +19,7 @@ def create_attachment(data = nil)
end

def new_picture(data = nil)
data ||= fixture_file_upload('files/rails.png', 'image/png')
data ||= Rack::Test::UploadedFile.new('test/dummy/test/fixtures/files/rails.png', 'image/png')

Ckeditor.picture_model.new(data: data)
end
Expand Down

0 comments on commit b7381e2

Please sign in to comment.