This commit is contained in:
Vasily Fedoseyev
2024-04-10 19:27:24 +03:00
parent 6647c5fa9c
commit 7cef86bb49
13 changed files with 33 additions and 21 deletions

View File

@@ -344,6 +344,6 @@ end
# Set it all up. # Set it all up.
if Object.const_defined?("ActiveRecord") if Object.const_defined?("ActiveRecord")
ActiveRecord::Base.include(Paperclip) ActiveSupport.on_load(:active_record) { include Paperclip }
File.include(Paperclip::Upfile) File.include(Paperclip::Upfile)
end end

View File

@@ -15,11 +15,11 @@ module Paperclip
def define_paperclip_callbacks(*callbacks) def define_paperclip_callbacks(*callbacks)
define_callbacks(*callbacks.flatten, {}) define_callbacks(*callbacks.flatten, {})
callbacks.map(&:to_sym).each do |callback| callbacks.map(&:to_sym).each do |callback|
define_singleton_method "before_#{callback}" do |*args, &blk| define_singleton_method :"before_#{callback}" do |*args, &blk|
set_callback(callback, :before, *args, &blk) set_callback(callback, :before, *args, &blk)
end end
define_singleton_method "after_#{callback}" do |*args, &blk| define_singleton_method :"after_#{callback}" do |*args, &blk|
set_callback(callback, :after, *args, &blk) set_callback(callback, :after, *args, &blk)
end end
end end

View File

@@ -140,7 +140,11 @@ class AttachmentTest < Test::Unit::TestCase
Tempfile.create do |tempfile| Tempfile.create do |tempfile|
content = "file contents" content = "file contents"
tempfile.write(content) tempfile.write(content)
upload = { 'original_name' => 'foo.jpg', 'content_type' => 'application/jpg', 'filepath' => tempfile.tap(&:rewind).path } upload = {
'original_name' => 'foo.jpg',
'content_type' => 'application/jpg',
'filepath' => tempfile.tap(&:rewind).path
}
@attachment.assign(upload) @attachment.assign(upload)
assert_equal "foo.png", @attachment.path assert_equal "foo.png", @attachment.path
assert_equal content, @attachment.queued_for_write[:original].tap(&:rewind).read assert_equal content, @attachment.queued_for_write[:original].tap(&:rewind).read

View File

@@ -21,7 +21,7 @@ class HaveAttachedFileMatcherTest < Test::Unit::TestCase
end end
should "have messages" do should "have messages" do
assert_equal "have an attachment named avatar", @matcher.description assert_equal "have an attachment named avatar", @matcher.description
assert_equal "Should have an attachment named avatar", @matcher.failure_message assert_equal "Should have an attachment named avatar", @matcher.failure_message
assert_equal "Should not have an attachment named avatar", @matcher.negative_failure_message assert_equal "Should not have an attachment named avatar", @matcher.negative_failure_message
end end

View File

@@ -28,9 +28,17 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
end end
should "have messages" do should "have messages" do
assert_equal "validate the content types allowed on attachment avatar", @matcher.description assert_equal "validate the content types allowed on attachment avatar", @matcher.description
assert_equal "Content types image/png, image/jpeg should be accepted and audio/mp3, application/octet-stream rejected by avatar", @matcher.failure_message assert_equal(
assert_equal "Content types image/png, image/jpeg should be rejected and audio/mp3, application/octet-stream accepted by avatar", @matcher.negative_failure_message "Content types image/png, image/jpeg should be accepted and audio/mp3, " \
"application/octet-stream rejected by avatar",
@matcher.failure_message
)
assert_equal(
"Content types image/png, image/jpeg should be rejected and audio/mp3, " \
"application/octet-stream accepted by avatar",
@matcher.negative_failure_message
)
end end
end end
end end

View File

@@ -19,7 +19,7 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase
end end
should "have messages" do should "have messages" do
assert_equal "require presence of attachment avatar", @matcher.description assert_equal "require presence of attachment avatar", @matcher.description
assert_equal "Attachment avatar should be required", @matcher.failure_message assert_equal "Attachment avatar should be required", @matcher.failure_message
assert_equal "Attachment avatar should not be required", @matcher.negative_failure_message assert_equal "Attachment avatar should not be required", @matcher.negative_failure_message
end end

View File

@@ -33,7 +33,7 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase
end end
should "have messages" do should "have messages" do
assert_equal "validate the size of attachment avatar", @matcher.description assert_equal "validate the size of attachment avatar", @matcher.description
assert_equal "Attachment avatar must be between 256 and 1024 bytes", @matcher.failure_message assert_equal "Attachment avatar must be between 256 and 1024 bytes", @matcher.failure_message
assert_equal "Attachment avatar cannot be between 256 and 1024 bytes", @matcher.negative_failure_message assert_equal "Attachment avatar cannot be between 256 and 1024 bytes", @matcher.negative_failure_message
end end

View File

@@ -5,7 +5,7 @@ require 'test_helper'
class OptimizerTest < Test::Unit::TestCase class OptimizerTest < Test::Unit::TestCase
setup do setup do
@pixel_jpg = Base64.decode64( @pixel_jpg = Base64.decode64(
"/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP#{'/'*86}wgALCAABAAEBAREA/8QAFBAB#{'A'*21}P/aAAgBAQABPxA" "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP#{'/' * 86}wgALCAABAAEBAREA/8QAFBAB#{'A' * 21}P/aAAgBAQABPxA"
) )
@pixel_png = Base64.decode64( @pixel_png = Base64.decode64(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg" "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg"

View File

@@ -110,7 +110,7 @@ class PaperclipTest < Test::Unit::TestCase
context "a validation with an if guard clause" do context "a validation with an if guard clause" do
setup do setup do
Dummy.send(:validates_attachment_presence, :avatar, :if => lambda{|i| i.foo }) Dummy.send(:validates_attachment_presence, :avatar, if: ->(i) { i.foo })
@dummy = Dummy.new @dummy = Dummy.new
end end
@@ -129,7 +129,7 @@ class PaperclipTest < Test::Unit::TestCase
context "a validation with an unless guard clause" do context "a validation with an unless guard clause" do
setup do setup do
Dummy.send(:validates_attachment_presence, :avatar, :unless => lambda{|i| i.foo }) Dummy.send(:validates_attachment_presence, :avatar, unless: ->(i) { i.foo })
@dummy = Dummy.new @dummy = Dummy.new
end end

View File

@@ -5,7 +5,7 @@ require 'test_helper'
class RailtieTest < Test::Unit::TestCase class RailtieTest < Test::Unit::TestCase
should "load processors" do should "load processors" do
FileUtils.mkdir_p('tmp/rails/lib/paperclip_processors') FileUtils.mkdir_p('tmp/rails/lib/paperclip_processors')
File.write(Rails.root.join('lib/paperclip_processors/some_custom_processor.rb'), <<~RUBY) Rails.root.join('lib/paperclip_processors/some_custom_processor.rb').write <<~RUBY
class Paperclip::SomeCustomProcessor < Paperclip::Processor class Paperclip::SomeCustomProcessor < Paperclip::Processor
end end
RUBY RUBY

View File

@@ -10,13 +10,13 @@ class RecursiveThumbnailTest < Test::Unit::TestCase
end end
should "use original when style not present" do should "use original when style not present" do
processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :missing, geometry: '1x1'}, @attachment) processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :missing, geometry: '1x1' }, @attachment)
assert_equal @original_file, processor.file assert_equal @original_file, processor.file
end end
should "use original when style failed to download" do should "use original when style failed to download" do
@attachment.expects(:to_file).with(:missing).raises("cannot haz filez") @attachment.expects(:to_file).with(:missing).raises("cannot haz filez")
processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :missing, geometry: '1x1'}, @attachment) processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :missing, geometry: '1x1' }, @attachment)
assert_equal @original_file, processor.file assert_equal @original_file, processor.file
end end
@@ -25,7 +25,9 @@ class RecursiveThumbnailTest < Test::Unit::TestCase
style_file.expects(:close!).once style_file.expects(:close!).once
@original_file.expects(:close!).never @original_file.expects(:close!).never
@attachment.expects(:to_file).with(:existent).returns(style_file) @attachment.expects(:to_file).with(:existent).returns(style_file)
processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :existent, geometry: '1x1'}, @attachment) processor = Paperclip::RecursiveThumbnail.new(
@original_file, { thumbnail: :existent, geometry: '1x1' }, @attachment
)
Paperclip.stubs run: "" Paperclip.stubs run: ""
assert_equal style_file, processor.file assert_equal style_file, processor.file
res = processor.make res = processor.make

View File

@@ -100,8 +100,7 @@ def rebuild_class(options = {})
end end
class FakeModel class FakeModel
def self.set_callback(...) def self.set_callback(...); end
end
include Paperclip include Paperclip

View File

@@ -28,11 +28,10 @@ class UpfileTest < Test::Unit::TestCase
'_.html' => 'text/html', '_.html' => 'text/html',
'__.htm' => 'text/html', '__.htm' => 'text/html',
"_.txt" => "text/plain", "_.txt" => "text/plain",
"_.liquid" => "text/x-liquid", "_.liquid" => "text/x-liquid",
'_.svg' => 'image/svg+xml', '_.svg' => 'image/svg+xml',
'_.xls' => 'application/vnd.ms-excel', '_.xls' => 'application/vnd.ms-excel'
}.each_pair do |example, result| }.each_pair do |example, result|
should "return #{result} for #{example}" do should "return #{result} for #{example}" do
assert_equal result, Paperclip::Upfile.content_type_from_ext(example) assert_equal result, Paperclip::Upfile.content_type_from_ext(example)