From 3dcb852b66a5819d1a62f9ede6713c9063ccc74a Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 14:14:10 +0300 Subject: [PATCH 01/10] Tests for optimizer --- test/optimizer_test.rb | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 test/optimizer_test.rb diff --git a/test/optimizer_test.rb b/test/optimizer_test.rb new file mode 100644 index 0000000..abc490f --- /dev/null +++ b/test/optimizer_test.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require 'test_helper' + +class OptimizerTest < Test::Unit::TestCase + setup do + @pixel_jpg = Base64.decode64( + "/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP#{'/'*86}wgALCAABAAEBAREA/8QAFBAB#{'A'*21}P/aAAgBAQABPxA" + ) + @pixel_png = Base64.decode64( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg" + ) + @pixel_gif = Base64.decode64("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7") + end + + def with_tempfile(name, content) + Tempfile.create([File.basename(name), File.extname(name)]) do |tempfile| + tempfile.binmode + tempfile.write(content) + tempfile.flush + tempfile.rewind + yield tempfile + end + end + + context "Paperclip::Optimizer" do + context "#real_content_type" do + should "detect jpeg" do + with_tempfile('pixel.jpg', @pixel_jpg) do |tempfile| + assert_equal("image/jpeg", Paperclip::Optimizer.new(tempfile, {}).real_content_type) + end + end + + should("detect png") do + with_tempfile('image.svg.png', @pixel_png) do |tempfile| + assert_equal("image/png", Paperclip::Optimizer.new(tempfile, {}).real_content_type) + end + end + + should("detect gif") do + with_tempfile('Spiked.gif', @pixel_gif) do |tempfile| + assert_equal("image/gif", Paperclip::Optimizer.new(tempfile, {}).real_content_type) + end + end + end + + context "#make" do + should "process jpeg" do + with_tempfile('pixel.jpg', @pixel_jpg) do |tempfile| + res = Paperclip::Optimizer.new(tempfile, {}).make + assert_equal("image/jpeg", Paperclip::Upfile.content_type_from_file(res.path)) + end + end + + should("process png") do + with_tempfile('image.svg.png', @pixel_png) do |tempfile| + res = Paperclip::Optimizer.new(tempfile, {}).make + assert_equal("image/png", Paperclip::Upfile.content_type_from_file(res.path)) + end + end + + should("process gif") do + with_tempfile('Spiked.gif', @pixel_gif) do |tempfile| + res = Paperclip::Optimizer.new(tempfile, {}).make + assert_equal("image/gif", Paperclip::Upfile.content_type_from_file(res.path)) + end + end + + should("pass others") do + invalid_content = "invalid gif" + with_tempfile('Spiked.gif', invalid_content) do |tempfile| + res = Paperclip::Optimizer.new(tempfile, {}).make + assert_equal(tempfile, res) + assert_equal(invalid_content, res.read) + end + end + + should("handle errors") do + invalid_content = "invalid gif" + with_tempfile('Spiked.gif', invalid_content) do |tempfile| + processor = Paperclip::Optimizer.new(tempfile, {}) + processor.stubs(real_content_type: 'image/gif') + Open3.stubs(:capture3).raises("lala") + Paperclip.expects(:log).with { _1.include?("lala") } + res = processor.make + assert_equal(tempfile, res) + assert_equal(invalid_content, res.read) + end + end + end + end +end From 1c5237ecafeddfeb753d2f9e10bc6aaf5fa975d9 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 16:43:00 +0300 Subject: [PATCH 02/10] drop rails < 5 --- lib/paperclip/callback_compatability.rb | 64 +++++++------------------ paperclip.gemspec | 2 +- test/test_helper.rb | 3 ++ 3 files changed, 20 insertions(+), 49 deletions(-) diff --git a/lib/paperclip/callback_compatability.rb b/lib/paperclip/callback_compatability.rb index c3ec237..20943fd 100644 --- a/lib/paperclip/callback_compatability.rb +++ b/lib/paperclip/callback_compatability.rb @@ -5,63 +5,31 @@ module Paperclip module_function def install_to(base) - mod = base.respond_to?(:set_callback) ? Rails3 : Rails21 - base.extend(mod::Defining) - base.send(:include, mod::Running) + raise "#{base} does not respond to set_callback" unless base.respond_to?(:set_callback) + + base.extend(Defining) + base.send(:include, Running) end - module Rails21 - module Defining - def define_paperclip_callbacks(*args) - args.each do |callback| - define_callbacks("before_#{callback}") - define_callbacks("after_#{callback}") + module Defining + def define_paperclip_callbacks(*callbacks) + define_callbacks(*callbacks.flatten, {}) + callbacks.map(&:to_sym).each do |callback| + define_singleton_method "before_#{callback}" do |*args, &blk| + set_callback(callback, :before, *args, &blk) end - end - end - module Running - def run_paperclip_callbacks(callback, _opts = nil) - # The overall structure of this isn't ideal since after callbacks run even if - # befores return false. But this is how rails 3's callbacks work, unfortunately. - yield if run_callbacks(:"before_#{callback}") { |result, _object| result == false } != false - run_callbacks(:"after_#{callback}") { |result, _object| result == false } + define_singleton_method "after_#{callback}" do |*args, &blk| + set_callback(callback, :after, *args, &blk) + end end end end - module Rails3 - module Defining - rails_version = Gem::Version.new(ActiveSupport::VERSION::STRING) - CALLBACK_OPTIONS = - if rails_version >= Gem::Version.new('5.0') - {} - elsif rails_version >= Gem::Version.new('4.1') - { terminator: ->(_target, result) { result == false } } - else - { terminator: 'result == false' } - end - - def define_paperclip_callbacks(*callbacks) - define_callbacks(*callbacks.flatten, CALLBACK_OPTIONS) - callbacks.map(&:to_sym).each do |callback| - define_singleton_method "before_#{callback}" do |*args, &blk| - set_callback(callback, :before, *args, &blk) - end - - define_singleton_method "after_#{callback}" do |*args, &blk| - set_callback(callback, :after, *args, &blk) - end - end - end - end - - module Running - def run_paperclip_callbacks(callback, &block) - run_callbacks(callback, &block) - end + module Running + def run_paperclip_callbacks(callback, &block) + run_callbacks(callback, &block) end end end end - diff --git a/paperclip.gemspec b/paperclip.gemspec index 6d03eb2..04354a5 100644 --- a/paperclip.gemspec +++ b/paperclip.gemspec @@ -19,7 +19,7 @@ Gem::Specification.new do |s| s.rubyforge_project = %q{paperclip} s.summary = %q{File attachments as attributes for ActiveRecord} - s.add_dependency 'activesupport', '< 7.2' + s.add_dependency 'activesupport', [">= 5.0", '< 7.2'] s.add_dependency 'addressable' s.add_dependency 'fastimage' end diff --git a/test/test_helper.rb b/test/test_helper.rb index 5e8c15b..cfdd5cf 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -99,6 +99,9 @@ def rebuild_class(options = {}) end class FakeModel + def self.set_callback(...) + end + include Paperclip attr_accessor :avatar_file_name, From e7ef0bf8a3a85ee1819cb879392945efcfd0621e Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 16:57:07 +0300 Subject: [PATCH 03/10] More tests --- test/attachment_test.rb | 13 +++++++++++++ test/matchers/have_attached_file_matcher_test.rb | 6 ++++++ ...validate_attachment_content_type_matcher_test.rb | 6 ++++++ .../validate_attachment_presence_matcher_test.rb | 6 ++++++ .../validate_attachment_size_matcher_test.rb | 6 ++++++ 5 files changed, 37 insertions(+) diff --git a/test/attachment_test.rb b/test/attachment_test.rb index b602b70..db9d149 100644 --- a/test/attachment_test.rb +++ b/test/attachment_test.rb @@ -134,6 +134,19 @@ class AttachmentTest < Test::Unit::TestCase @attachment.assign(@file) assert_equal "file.png", @attachment.path end + + context "fast upload via nginx" do + should "return the right extension for the path" do + Tempfile.create do |tempfile| + content = "file contents" + tempfile.write(content) + upload = { 'original_name' => 'foo.jpg', 'content_type' => 'application/jpg', 'filepath' => tempfile.tap(&:rewind).path } + @attachment.assign(upload) + assert_equal "foo.png", @attachment.path + assert_equal content, @attachment.queued_for_write[:original].tap(&:rewind).read + end + end + end end context "An attachment with both 'normal' and hash-style styles" do diff --git a/test/matchers/have_attached_file_matcher_test.rb b/test/matchers/have_attached_file_matcher_test.rb index ad1a279..9d80a0d 100644 --- a/test/matchers/have_attached_file_matcher_test.rb +++ b/test/matchers/have_attached_file_matcher_test.rb @@ -19,5 +19,11 @@ class HaveAttachedFileMatcherTest < Test::Unit::TestCase @dummy_class.has_attached_file :avatar assert_accepts @matcher, @dummy_class end + + should "have messages" do + assert_equal "have an attachment named avatar", @matcher.description + assert_equal "Should have an attachment named avatar", @matcher.failure_message + assert_equal "Should not have an attachment named avatar", @matcher.negative_failure_message + end end end diff --git a/test/matchers/validate_attachment_content_type_matcher_test.rb b/test/matchers/validate_attachment_content_type_matcher_test.rb index b12dd4e..ff9f8f2 100644 --- a/test/matchers/validate_attachment_content_type_matcher_test.rb +++ b/test/matchers/validate_attachment_content_type_matcher_test.rb @@ -26,5 +26,11 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*} assert_accepts @matcher, @dummy_class end + + should "have messages" do + 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 "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 diff --git a/test/matchers/validate_attachment_presence_matcher_test.rb b/test/matchers/validate_attachment_presence_matcher_test.rb index 450253c..c7bb1c6 100644 --- a/test/matchers/validate_attachment_presence_matcher_test.rb +++ b/test/matchers/validate_attachment_presence_matcher_test.rb @@ -17,5 +17,11 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase @dummy_class.validates_attachment_presence :avatar assert_accepts @matcher, @dummy_class end + + should "have messages" do + assert_equal "require presence of attachment avatar", @matcher.description + assert_equal "Attachment avatar should be required", @matcher.failure_message + assert_equal "Attachment avatar should not be required", @matcher.negative_failure_message + end end end diff --git a/test/matchers/validate_attachment_size_matcher_test.rb b/test/matchers/validate_attachment_size_matcher_test.rb index 75bf1bb..5668635 100644 --- a/test/matchers/validate_attachment_size_matcher_test.rb +++ b/test/matchers/validate_attachment_size_matcher_test.rb @@ -31,6 +31,12 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase @dummy_class.validates_attachment_size :avatar, :in => 256..1024 assert_accepts @matcher, @dummy_class end + + should "have messages" do + 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 cannot be between 256 and 1024 bytes", @matcher.negative_failure_message + end end context "validates_attachment_size with infinite range" do From f2b6da2a69ce4cc643400593bd8936639d5540c4 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 17:51:38 +0300 Subject: [PATCH 04/10] Upfile tests + `File#size` is available since ruby 2.0 (AR 5 requires 2.2) --- lib/paperclip/upfile.rb | 5 ----- test/upfile_test.rb | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 test/upfile_test.rb diff --git a/lib/paperclip/upfile.rb b/lib/paperclip/upfile.rb index 3fb09bf..caeefdc 100644 --- a/lib/paperclip/upfile.rb +++ b/lib/paperclip/upfile.rb @@ -39,11 +39,6 @@ module Paperclip def original_filename @original_filename ||= File.basename(path) end - - # Returns the size of the file. - def size - File.size(self) - end end end diff --git a/test/upfile_test.rb b/test/upfile_test.rb new file mode 100644 index 0000000..d6a37d2 --- /dev/null +++ b/test/upfile_test.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require 'test_helper' + +class UpfileTest < Test::Unit::TestCase + context "content_type_from_ext" do + { + 'lala' => 'application/x-octet-stream', + 'lala.foo' => 'application/x-foo', + '__.jpg' => "image/jpeg", + '_.jpeg' => "image/jpeg", + '__.tif' => "image/tiff", + '_.tiff' => "image/tiff", + + '_.png' => "image/png", + '_.gif' => "image/gif", + '_.bmp' => "image/bmp", + "_.webp" => "image/webp", + + # '_.pngfoo' => 'application/x-pngfoo', # ?? + # '_.htmfoo' => 'application/x-htmfoo', # ? + + '_.csv' => "text/csv", + '_.xml' => "text/xml", + '_.css' => "text/css", + '_.js' => "text/js", # ??? + + '_.html' => 'text/html', + '__.htm' => 'text/html', + + + "_.txt" => "text/plain", + "_.liquid" => "text/x-liquid", + '_.svg' => 'image/svg+xml', + '_.xls' => 'application/vnd.ms-excel', + }.each_pair do |example, result| + should "return #{result} for #{example}" do + assert_equal result, Paperclip::Upfile.content_type_from_ext(example) + end + end + end +end From 99cb5b39fea7dadc5312e57c43e8ac3ce16c6de2 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 18:01:56 +0300 Subject: [PATCH 05/10] Drop old jruby support (modern have tempfile similar to mri) --- lib/paperclip.rb | 1 - lib/paperclip/tempfile.rb | 20 -------------------- 2 files changed, 21 deletions(-) delete mode 100644 lib/paperclip/tempfile.rb diff --git a/lib/paperclip.rb b/lib/paperclip.rb index 409cc63..22ee9c4 100644 --- a/lib/paperclip.rb +++ b/lib/paperclip.rb @@ -32,7 +32,6 @@ require 'paperclip/upfile' require 'paperclip/iostream' require 'paperclip/geometry' require 'paperclip/processor' -require 'paperclip/tempfile' require 'paperclip/thumbnail' require 'paperclip/recursive_thumbnail' require 'paperclip/storage' diff --git a/lib/paperclip/tempfile.rb b/lib/paperclip/tempfile.rb deleted file mode 100644 index 48a75a8..0000000 --- a/lib/paperclip/tempfile.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -require 'English' - -module Paperclip - # Due to how ImageMagick handles its image format conversion and how Tempfile - # handles its naming scheme, it is necessary to override how Tempfile makes - # its names so as to allow for file extensions. Idea taken from the comments - # on this blog post: - # http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions - class Tempfile < ::Tempfile - # это похоже актуально только для java (не проверял, в mri 3.2 точно не вызывается) - - # Replaces Tempfile's +make_tmpname+ with one that honors file extensions. - def make_tmpname(basename, n) - extension = File.extname(basename) - sprintf("%s,%d,%d%s", File.basename(basename, extension), $PROCESS_ID, n.to_i, extension) - end - end -end From c4009e1ca12c05af131d508ddadc74a626f894ae Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 18:22:16 +0300 Subject: [PATCH 06/10] Railtie test --- test/railtie_test.rb | 23 +++++++++++++++++++++++ test/test_helper.rb | 1 + 2 files changed, 24 insertions(+) create mode 100644 test/railtie_test.rb diff --git a/test/railtie_test.rb b/test/railtie_test.rb new file mode 100644 index 0000000..9e1d669 --- /dev/null +++ b/test/railtie_test.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'test_helper' + +class RailtieTest < Test::Unit::TestCase + should "load processors" do + FileUtils.mkdir_p('tmp/rails/lib/paperclip_processors') + File.write(Rails.root.join('lib/paperclip_processors/some_custom_processor.rb'), <<~RUBY) + class Paperclip::SomeCustomProcessor < Paperclip::Processor + end + RUBY + Paperclip::Railtie.initializers.each(&:run) + assert defined?(Paperclip::SomeCustomProcessor) + end + + should "load rake tasks" do + require 'rake' + require 'rake/testtask' + + Rails.application.load_tasks + assert_equal Rake::Task, Rake.application["paperclip:refresh:thumbnails"].class + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index cfdd5cf..97252fd 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -30,6 +30,7 @@ if ENV['COVERAGE'] add_group "Libraries", "lib/" track_files "{lib}/**/*.rb" + add_filter "lib/tasks/paperclip_tasks.rake" # TODO: вообще по-хорошема надо и его покрыть end end From 6c99a6f052f99fad00a46e947a1129e88d8a34c0 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 19:00:55 +0300 Subject: [PATCH 07/10] 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 From 4801e731d8abaa6a8f66a357db4c9b997661ce38 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 19:07:29 +0300 Subject: [PATCH 08/10] =?UTF-8?q?Rollbar=20=D0=BC=D0=BE=D0=B6=D0=B5=D1=82?= =?UTF-8?q?=20=D0=B8=20=D0=BD=D0=B5=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=D1=81=D1=8F=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/paperclip/storage/delayed_upload.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/paperclip/storage/delayed_upload.rb b/lib/paperclip/storage/delayed_upload.rb index 0e8d257..304684f 100644 --- a/lib/paperclip/storage/delayed_upload.rb +++ b/lib/paperclip/storage/delayed_upload.rb @@ -34,7 +34,7 @@ module Paperclip raise if model.exists?(id) rescue Errno::ENOENT => e raise if model.exists?(id) - Rollbar.warn(e, file_name: e.message.split(' - ')[-1]) + Rollbar.warn(e, file_name: e.message.split(' - ')[-1]) if defined?(Rollbar) end end end From 6647c5fa9c34cc9859eea97a08995d1a7cb821d2 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 19:15:45 +0300 Subject: [PATCH 09/10] Fix rubocop lint warnings --- Rakefile | 30 +++++++++++++++-------------- lib/paperclip.rb | 4 ++-- lib/paperclip/storage/filesystem.rb | 2 +- test/attachment_test.rb | 2 +- test/paperclip_test.rb | 10 +++++----- test/plural_cache_test.rb | 2 +- test/thumbnail_test.rb | 4 ++-- 7 files changed, 28 insertions(+), 26 deletions(-) diff --git a/Rakefile b/Rakefile index 12a4fe0..9025edd 100644 --- a/Rakefile +++ b/Rakefile @@ -47,24 +47,26 @@ task :clean do Dir.glob("paperclip-*.gem").each{|f| FileUtils.rm f } end -include_file_globs = ["README*", - "LICENSE", - "Rakefile", - "init.rb", - "{generators,lib,tasks,test,shoulda_macros}/**/*"] -exclude_file_globs = ["test/s3.yml", - "test/debug.log", - "test/paperclip.db", - "test/doc", - "test/doc/*", - "test/pkg", - "test/pkg/*", - "test/tmp", - "test/tmp/*"] def spec # TODO: require 'paperclip/version' require 'paperclip' + + include_file_globs = ["README*", + "LICENSE", + "Rakefile", + "init.rb", + "{generators,lib,tasks,test,shoulda_macros}/**/*"] + exclude_file_globs = ["test/s3.yml", + "test/debug.log", + "test/paperclip.db", + "test/doc", + "test/doc/*", + "test/pkg", + "test/pkg/*", + "test/tmp", + "test/tmp/*"] + Gem::Specification.new do |s| s.name = "paperclip" s.version = Paperclip::VERSION diff --git a/lib/paperclip.rb b/lib/paperclip.rb index 22ee9c4..dc6a765 100644 --- a/lib/paperclip.rb +++ b/lib/paperclip.rb @@ -344,6 +344,6 @@ end # Set it all up. if Object.const_defined?("ActiveRecord") - ActiveRecord::Base.send(:include, Paperclip) - File.send(:include, Paperclip::Upfile) + ActiveRecord::Base.include(Paperclip) + File.include(Paperclip::Upfile) end diff --git a/lib/paperclip/storage/filesystem.rb b/lib/paperclip/storage/filesystem.rb index 17bd035..113f2a0 100644 --- a/lib/paperclip/storage/filesystem.rb +++ b/lib/paperclip/storage/filesystem.rb @@ -56,7 +56,7 @@ module Paperclip # ignore file-not-found, let everything else pass end begin - while(true) + loop do path = File.dirname(path) if Dir.entries(path).empty? FileUtils.rmdir(path) diff --git a/test/attachment_test.rb b/test/attachment_test.rb index db9d149..5db8815 100644 --- a/test/attachment_test.rb +++ b/test/attachment_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class Dummy +class Dummy # rubocop:disable Lint/EmptyClass # This is a dummy class end diff --git a/test/paperclip_test.rb b/test/paperclip_test.rb index 358cc6b..8a5241a 100644 --- a/test/paperclip_test.rb +++ b/test/paperclip_test.rb @@ -16,7 +16,7 @@ class PaperclipTest < Test::Unit::TestCase should "execute the right command" do Paperclip.expects(:path_for_command).with("convert").returns("/usr/bin/convert") Paperclip.expects(:bit_bucket).returns("/dev/null") - Paperclip.expects(:"`").with("timeout 30 /usr/bin/convert #{@file_path} #{@file_path2} 2>/dev/null") + Paperclip.expects(:`).with("timeout 30 /usr/bin/convert #{@file_path} #{@file_path2} 2>/dev/null") Paperclip.run("convert", "#{@file_path} #{@file_path2}") end end @@ -33,7 +33,7 @@ class PaperclipTest < Test::Unit::TestCase should "execute the right command" do Paperclip.expects(:path_for_command).with("convert").returns("convert") Paperclip.expects(:bit_bucket).returns("/dev/null") - Paperclip.expects(:"`").with("timeout 30 convert #{@file_path} #{@file_path2} 2>/dev/null") + Paperclip.expects(:`).with("timeout 30 convert #{@file_path} #{@file_path2} 2>/dev/null") Paperclip.run("convert", "#{@file_path} #{@file_path2}") end @@ -41,7 +41,7 @@ class PaperclipTest < Test::Unit::TestCase Paperclip.options[:log_command] = true Paperclip.expects(:bit_bucket).returns("/dev/null") Paperclip.expects(:log).with("convert #{@file_path} #{@file_path2} 2>/dev/null") - Paperclip.expects(:"`").with("timeout 30 convert #{@file_path} #{@file_path2} 2>/dev/null") + Paperclip.expects(:`).with("timeout 30 convert #{@file_path} #{@file_path2} 2>/dev/null") Paperclip.run("convert", "#{@file_path} #{@file_path2}") end end @@ -110,7 +110,7 @@ class PaperclipTest < Test::Unit::TestCase context "a validation with an if guard clause" do setup do - Dummy.send(:"validates_attachment_presence", :avatar, :if => lambda{|i| i.foo }) + Dummy.send(:validates_attachment_presence, :avatar, :if => lambda{|i| i.foo }) @dummy = Dummy.new end @@ -129,7 +129,7 @@ class PaperclipTest < Test::Unit::TestCase context "a validation with an unless guard clause" do setup do - Dummy.send(:"validates_attachment_presence", :avatar, :unless => lambda{|i| i.foo }) + Dummy.send(:validates_attachment_presence, :avatar, :unless => lambda{|i| i.foo }) @dummy = Dummy.new end diff --git a/test/plural_cache_test.rb b/test/plural_cache_test.rb index 6b67b48..b9207cb 100644 --- a/test/plural_cache_test.rb +++ b/test/plural_cache_test.rb @@ -1,7 +1,7 @@ require 'test_helper' class PluralCacheTest < Test::Unit::TestCase - class BigBox; end + class BigBox; end # rubocop:disable Lint/EmptyClass should 'cache pluralizations' do cache = Paperclip::Interpolations::PluralCache.new diff --git a/test/thumbnail_test.rb b/test/thumbnail_test.rb index 2feb7a3..cc558d5 100644 --- a/test/thumbnail_test.rb +++ b/test/thumbnail_test.rb @@ -65,7 +65,7 @@ class ThumbnailTest < Test::Unit::TestCase should "send the right command to convert when sent #make" do Paperclip::Thumbnail.any_instance.stubs(:gamma_correction_if_needed).returns("PNG\nPaletteAlpha") - Paperclip.expects(:"`").with do |arg| + Paperclip.expects(:`).with do |arg| arg.match? %r{convert\s+"#{File.expand_path(@thumb.file.path)}\[0\]"\s+-auto-orient\s+-resize\s+\"x50\"\s+-crop\s+\"100x50\+114\+0\"\s+\+repage\s+} end @thumb.make @@ -90,7 +90,7 @@ class ThumbnailTest < Test::Unit::TestCase should "send the right command to convert when sent #make" do Paperclip::Thumbnail.any_instance.stubs(:gamma_correction_if_needed).returns("PNG\nPaletteAlpha") - Paperclip.expects(:"`").with do |arg| + Paperclip.expects(:`).with do |arg| arg.match? %r{convert\s+"#{File.expand_path(@thumb.file.path)}\[0\]"\s+-auto-orient\s+-resize\s+"x50"\s+-crop\s+"100x50\+114\+0"\s+\+repage\s+-strip\s+-depth\s+8\s+} end @thumb.make From 7cef86bb49b12820352f8e6da6033a457180946a Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Wed, 10 Apr 2024 19:27:24 +0300 Subject: [PATCH 10/10] Rubocop --- lib/paperclip.rb | 2 +- lib/paperclip/callback_compatability.rb | 4 ++-- test/attachment_test.rb | 6 +++++- test/matchers/have_attached_file_matcher_test.rb | 2 +- ...alidate_attachment_content_type_matcher_test.rb | 14 +++++++++++--- .../validate_attachment_presence_matcher_test.rb | 2 +- .../validate_attachment_size_matcher_test.rb | 2 +- test/optimizer_test.rb | 2 +- test/paperclip_test.rb | 4 ++-- test/railtie_test.rb | 2 +- test/recursive_thumbnail_test.rb | 8 +++++--- test/test_helper.rb | 3 +-- test/upfile_test.rb | 3 +-- 13 files changed, 33 insertions(+), 21 deletions(-) diff --git a/lib/paperclip.rb b/lib/paperclip.rb index dc6a765..c811f13 100644 --- a/lib/paperclip.rb +++ b/lib/paperclip.rb @@ -344,6 +344,6 @@ end # Set it all up. if Object.const_defined?("ActiveRecord") - ActiveRecord::Base.include(Paperclip) + ActiveSupport.on_load(:active_record) { include Paperclip } File.include(Paperclip::Upfile) end diff --git a/lib/paperclip/callback_compatability.rb b/lib/paperclip/callback_compatability.rb index 20943fd..b11a61e 100644 --- a/lib/paperclip/callback_compatability.rb +++ b/lib/paperclip/callback_compatability.rb @@ -15,11 +15,11 @@ module Paperclip def define_paperclip_callbacks(*callbacks) define_callbacks(*callbacks.flatten, {}) 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) end - define_singleton_method "after_#{callback}" do |*args, &blk| + define_singleton_method :"after_#{callback}" do |*args, &blk| set_callback(callback, :after, *args, &blk) end end diff --git a/test/attachment_test.rb b/test/attachment_test.rb index 5db8815..4c20987 100644 --- a/test/attachment_test.rb +++ b/test/attachment_test.rb @@ -140,7 +140,11 @@ class AttachmentTest < Test::Unit::TestCase Tempfile.create do |tempfile| content = "file contents" 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) assert_equal "foo.png", @attachment.path assert_equal content, @attachment.queued_for_write[:original].tap(&:rewind).read diff --git a/test/matchers/have_attached_file_matcher_test.rb b/test/matchers/have_attached_file_matcher_test.rb index 9d80a0d..04b26e3 100644 --- a/test/matchers/have_attached_file_matcher_test.rb +++ b/test/matchers/have_attached_file_matcher_test.rb @@ -21,7 +21,7 @@ class HaveAttachedFileMatcherTest < Test::Unit::TestCase end 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 not have an attachment named avatar", @matcher.negative_failure_message end diff --git a/test/matchers/validate_attachment_content_type_matcher_test.rb b/test/matchers/validate_attachment_content_type_matcher_test.rb index ff9f8f2..be048e8 100644 --- a/test/matchers/validate_attachment_content_type_matcher_test.rb +++ b/test/matchers/validate_attachment_content_type_matcher_test.rb @@ -28,9 +28,17 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase end should "have messages" do - 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 "Content types image/png, image/jpeg should be rejected and audio/mp3, application/octet-stream accepted by avatar", @matcher.negative_failure_message + 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( + "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 diff --git a/test/matchers/validate_attachment_presence_matcher_test.rb b/test/matchers/validate_attachment_presence_matcher_test.rb index c7bb1c6..a503e70 100644 --- a/test/matchers/validate_attachment_presence_matcher_test.rb +++ b/test/matchers/validate_attachment_presence_matcher_test.rb @@ -19,7 +19,7 @@ class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase end 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 not be required", @matcher.negative_failure_message end diff --git a/test/matchers/validate_attachment_size_matcher_test.rb b/test/matchers/validate_attachment_size_matcher_test.rb index 5668635..7cc8c88 100644 --- a/test/matchers/validate_attachment_size_matcher_test.rb +++ b/test/matchers/validate_attachment_size_matcher_test.rb @@ -33,7 +33,7 @@ class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase end 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 cannot be between 256 and 1024 bytes", @matcher.negative_failure_message end diff --git a/test/optimizer_test.rb b/test/optimizer_test.rb index abc490f..f76ed4a 100644 --- a/test/optimizer_test.rb +++ b/test/optimizer_test.rb @@ -5,7 +5,7 @@ require 'test_helper' class OptimizerTest < Test::Unit::TestCase setup do @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( "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg" diff --git a/test/paperclip_test.rb b/test/paperclip_test.rb index 8a5241a..377f5c0 100644 --- a/test/paperclip_test.rb +++ b/test/paperclip_test.rb @@ -110,7 +110,7 @@ class PaperclipTest < Test::Unit::TestCase context "a validation with an if guard clause" 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 end @@ -129,7 +129,7 @@ class PaperclipTest < Test::Unit::TestCase context "a validation with an unless guard clause" 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 end diff --git a/test/railtie_test.rb b/test/railtie_test.rb index 9e1d669..78ca8c9 100644 --- a/test/railtie_test.rb +++ b/test/railtie_test.rb @@ -5,7 +5,7 @@ require 'test_helper' class RailtieTest < Test::Unit::TestCase should "load processors" do 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 end RUBY diff --git a/test/recursive_thumbnail_test.rb b/test/recursive_thumbnail_test.rb index 55b157f..bddc184 100644 --- a/test/recursive_thumbnail_test.rb +++ b/test/recursive_thumbnail_test.rb @@ -10,13 +10,13 @@ class RecursiveThumbnailTest < Test::Unit::TestCase end 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 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) + processor = Paperclip::RecursiveThumbnail.new(@original_file, { thumbnail: :missing, geometry: '1x1' }, @attachment) assert_equal @original_file, processor.file end @@ -25,7 +25,9 @@ class RecursiveThumbnailTest < Test::Unit::TestCase 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) + processor = Paperclip::RecursiveThumbnail.new( + @original_file, { thumbnail: :existent, geometry: '1x1' }, @attachment + ) Paperclip.stubs run: "" assert_equal style_file, processor.file res = processor.make diff --git a/test/test_helper.rb b/test/test_helper.rb index 97252fd..36f05c2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -100,8 +100,7 @@ def rebuild_class(options = {}) end class FakeModel - def self.set_callback(...) - end + def self.set_callback(...); end include Paperclip diff --git a/test/upfile_test.rb b/test/upfile_test.rb index d6a37d2..603ff06 100644 --- a/test/upfile_test.rb +++ b/test/upfile_test.rb @@ -28,11 +28,10 @@ class UpfileTest < Test::Unit::TestCase '_.html' => 'text/html', '__.htm' => 'text/html', - "_.txt" => "text/plain", "_.liquid" => "text/x-liquid", '_.svg' => 'image/svg+xml', - '_.xls' => 'application/vnd.ms-excel', + '_.xls' => 'application/vnd.ms-excel' }.each_pair do |example, result| should "return #{result} for #{example}" do assert_equal result, Paperclip::Upfile.content_type_from_ext(example)