diff --git a/.gitignore b/.gitignore index d4484ab46..54e603b88 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ test/dummy/public/uploads/* .DS_Store Gemfile.lock dragonfly.log +public/ diff --git a/lib/ckeditor/backend/shrine.rb b/lib/ckeditor/backend/shrine.rb index 532186e72..b991cecd2 100644 --- a/lib/ckeditor/backend/shrine.rb +++ b/lib/ckeditor/backend/shrine.rb @@ -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 diff --git a/lib/generators/ckeditor/install_generator.rb b/lib/generators/ckeditor/install_generator.rb index 80b1ab403..e56ac9e22 100644 --- a/lib/generators/ckeditor/install_generator.rb +++ b/lib/generators/ckeditor/install_generator.rb @@ -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 @@ -94,7 +94,7 @@ def generator_dir end def uploaders_dir - @uploaders_dir ||= 'base/carrierwave' + @uploaders_dir ||= "base/#{backend}/uploaders" end def orm diff --git a/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/asset.rb b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/asset.rb index 37dd78041..ded33403d 100644 --- a/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/asset.rb +++ b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/asset.rb @@ -5,4 +5,6 @@ class Ckeditor::Asset < ActiveRecord::Base include Ckeditor::Backend::Shrine include CkeditorAttachmentUploader::Attachment(:attachment) + + validates :attachment_data, presence: true 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 index e71046ddc..6920853df 100644 --- a/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/picture.rb +++ b/lib/generators/ckeditor/templates/active_record/shrine/ckeditor/picture.rb @@ -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 diff --git a/lib/generators/ckeditor/templates/base/shrine/attachment_uploader.rb b/lib/generators/ckeditor/templates/base/shrine/attachment_uploader.rb deleted file mode 100644 index 0a36f5e2b..000000000 --- a/lib/generators/ckeditor/templates/base/shrine/attachment_uploader.rb +++ /dev/null @@ -1,3 +0,0 @@ -class CkeditorAttachmentUploader < Shrine - # plugins and uploading logic -end diff --git a/lib/generators/ckeditor/templates/base/shrine/uploaders/ckeditor_attachment_uploader.rb b/lib/generators/ckeditor/templates/base/shrine/uploaders/ckeditor_attachment_uploader.rb new file mode 100644 index 000000000..30d7921f2 --- /dev/null +++ b/lib/generators/ckeditor/templates/base/shrine/uploaders/ckeditor_attachment_uploader.rb @@ -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 diff --git a/test/controllers/attachment_files_controller_test.rb b/test/controllers/attachment_files_controller_test.rb index d6a879fce..8f86878bb 100644 --- a/test/controllers/attachment_files_controller_test.rb +++ b/test/controllers/attachment_files_controller_test.rb @@ -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 diff --git a/test/controllers/pictures_controller_test.rb b/test/controllers/pictures_controller_test.rb index 35685dad8..011fbc843 100644 --- a/test/controllers/pictures_controller_test.rb +++ b/test/controllers/pictures_controller_test.rb @@ -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 diff --git a/test/models/picture_test.rb b/test/models/picture_test.rb index 57809b91e..4de3b3390 100644 --- a/test/models/picture_test.rb +++ b/test/models/picture_test.rb @@ -10,7 +10,8 @@ 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') @@ -18,7 +19,6 @@ def teardown 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 diff --git a/test/support/helpers.rb b/test/support/helpers.rb index 7971d6a3a..61431a25a 100644 --- a/test/support/helpers.rb +++ b/test/support/helpers.rb @@ -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 @@ -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