From 6c99a6f052f99fad00a46e947a1129e88d8a34c0 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 19:00:55 +0300 Subject: [PATCH] Fix and test recursive_thumbnail --- lib/paperclip/recursive_thumbnail.rb | 24 ++++++++++++++------ test/recursive_thumbnail_test.rb | 34 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 test/recursive_thumbnail_test.rb diff --git a/lib/paperclip/recursive_thumbnail.rb b/lib/paperclip/recursive_thumbnail.rb index cc091c3..414afbb 100644 --- a/lib/paperclip/recursive_thumbnail.rb +++ b/lib/paperclip/recursive_thumbnail.rb @@ -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 diff --git a/test/recursive_thumbnail_test.rb b/test/recursive_thumbnail_test.rb new file mode 100644 index 0000000..55b157f --- /dev/null +++ b/test/recursive_thumbnail_test.rb @@ -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