mirror of
https://github.com/kemko/paperclip.git
synced 2026-01-01 16:05:40 +03:00
Fix and test recursive_thumbnail
This commit is contained in:
@@ -6,15 +6,25 @@ module Paperclip
|
||||
source_style = options[:thumbnail] || :original
|
||||
# если по каким-то причинам не сформировался файл прыдущего размера - генерим из оригинального
|
||||
source_file = begin
|
||||
attachment.to_file(source_style)
|
||||
rescue
|
||||
Paperclip.log "Using original for #{options}"
|
||||
file
|
||||
end
|
||||
attachment.to_file(source_style)
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
|
||||
unless source_file
|
||||
Paperclip.log "Using original for #{options}"
|
||||
source_file = file
|
||||
end
|
||||
|
||||
@original_file = file
|
||||
super(source_file, options, attachment)
|
||||
end
|
||||
|
||||
def make
|
||||
super
|
||||
ensure
|
||||
if source_file != file && source_file.respond_to?(:close!) && !attachment&.queued_for_write&.value?(source_file)
|
||||
source_file.close!
|
||||
if @file != @original_file && @file.respond_to?(:close!) && !attachment&.queued_for_write&.value?(@file)
|
||||
@file.close!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
34
test/recursive_thumbnail_test.rb
Normal file
34
test/recursive_thumbnail_test.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
class RecursiveThumbnailTest < Test::Unit::TestCase
|
||||
setup do
|
||||
Paperclip::Geometry.stubs from_file: Paperclip::Geometry.parse('1x1')
|
||||
@original_file = stub("originalfile", path: 'originalfile.txt')
|
||||
@attachment = attachment({})
|
||||
end
|
||||
|
||||
should "use original when style not present" do
|
||||
processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :missing, geometry: '1x1'}, @attachment)
|
||||
assert_equal @original_file, processor.file
|
||||
end
|
||||
|
||||
should "use original when style failed to download" do
|
||||
@attachment.expects(:to_file).with(:missing).raises("cannot haz filez")
|
||||
processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :missing, geometry: '1x1'}, @attachment)
|
||||
assert_equal @original_file, processor.file
|
||||
end
|
||||
|
||||
should "use style when present" do
|
||||
style_file = stub("stylefile", path: 'style.txt')
|
||||
style_file.expects(:close!).once
|
||||
@original_file.expects(:close!).never
|
||||
@attachment.expects(:to_file).with(:existent).returns(style_file)
|
||||
processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :existent, geometry: '1x1'}, @attachment)
|
||||
Paperclip.stubs run: ""
|
||||
assert_equal style_file, processor.file
|
||||
res = processor.make
|
||||
assert_equal Tempfile, res.class
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user