From 6fb3f7d931747688ae92d4a5df78189911222b07 Mon Sep 17 00:00:00 2001 From: Vasily Fedoseyev Date: Mon, 18 Apr 2022 20:45:59 +0300 Subject: [PATCH] Fix rubocop lint warnings --- lib/paperclip.rb | 10 ++++----- lib/paperclip/attachment.rb | 2 +- lib/paperclip/callback_compatability.rb | 8 +++---- lib/paperclip/geometry.rb | 6 ++--- lib/paperclip/interpolations.rb | 22 +++++++++---------- lib/paperclip/iostream.rb | 5 ++--- .../validate_attachment_size_matcher.rb | 6 ++--- lib/paperclip/optimizer.rb | 3 ++- lib/paperclip/railtie.rb | 2 +- lib/paperclip/storage/cached.rb | 2 +- lib/paperclip/storage/delayeds3.rb | 8 ++++--- lib/paperclip/storage/filesystem.rb | 2 +- lib/tasks/paperclip_tasks.rake | 2 +- 13 files changed, 40 insertions(+), 38 deletions(-) diff --git a/lib/paperclip.rb b/lib/paperclip.rb index 5639d5f..621564d 100644 --- a/lib/paperclip.rb +++ b/lib/paperclip.rb @@ -238,7 +238,7 @@ module Paperclip attachment_for(name).file? end - validates_each(name) do |record, attr, value| + validates_each(name) do |record, _attr, _value| attachment = record.attachment_for(name) attachment.send(:flush_errors) unless attachment.valid? end @@ -307,21 +307,21 @@ module Paperclip end def each_attachment - self.class.attachment_definitions.each do |name, definition| + self.class.attachment_definitions.each do |name, _definition| yield(name, attachment_for(name)) end end def save_attached_files logger.info("[paperclip] Saving attachments.") - each_attachment do |name, attachment| + each_attachment do |_name, attachment| attachment.send(:save) end end def destroy_attached_files logger.info("[paperclip] Deleting attachments.") - each_attachment do |name, attachment| + each_attachment do |_name, attachment| attachment.send(:queue_existing_for_delete) attachment.send(:flush_deletes) end @@ -330,7 +330,7 @@ module Paperclip def flush_attachment_jobs logger.info("[paperclip] flushing jobs.") - each_attachment do |name, attachment| + each_attachment do |_name, attachment| attachment.try(:flush_jobs) end end diff --git a/lib/paperclip/attachment.rb b/lib/paperclip/attachment.rb index 8be9968..20e7c3e 100644 --- a/lib/paperclip/attachment.rb +++ b/lib/paperclip/attachment.rb @@ -451,7 +451,7 @@ module Paperclip end def flush_errors #:nodoc: - errors.each do |error, message| + errors.each do |_error, message| [message].flatten.each {|m| instance.errors.add(name, m) } end end diff --git a/lib/paperclip/callback_compatability.rb b/lib/paperclip/callback_compatability.rb index d780170..5a59723 100644 --- a/lib/paperclip/callback_compatability.rb +++ b/lib/paperclip/callback_compatability.rb @@ -21,13 +21,13 @@ module Paperclip end module Running - def run_paperclip_callbacks(callback, opts = nil, &blk) + def run_paperclip_callbacks(callback, _opts = nil, &blk) # 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. - if run_callbacks(:"before_#{callback}"){ |result, object| result == false } != false + if run_callbacks(:"before_#{callback}"){ |result, _object| result == false } != false blk.call end - run_callbacks(:"after_#{callback}"){ |result, object| result == false } + run_callbacks(:"after_#{callback}"){ |result, _object| result == false } end end end @@ -39,7 +39,7 @@ module Paperclip if rails_version >= Gem::Version.new('5.0') {} elsif rails_version >= Gem::Version.new('4.1') - {terminator: ->(target, result) { result == false }} + {terminator: ->(_target, result) { result == false }} else {terminator: 'result == false'} end diff --git a/lib/paperclip/geometry.rb b/lib/paperclip/geometry.rb index 5b67e5e..3a4b0cc 100644 --- a/lib/paperclip/geometry.rb +++ b/lib/paperclip/geometry.rb @@ -36,7 +36,7 @@ module Paperclip # Parses a "WxH" formatted string, where W is the width and H is the height. def self.parse string - if match = (string && string.match(/\b(\d*)x?(\d*)\b(?:,(\d?))?([\>\<\#\@\%^!])?/i)) + if (match = (string && string.match(/\b(\d*)x?(\d*)\b(?:,(\d?))?([\>\<\#\@\%^!])?/i))) Geometry.new( width: match[1], height: match[2], @@ -129,9 +129,9 @@ module Paperclip def cropping dst, ratio, scale if ratio.horizontal? || ratio.square? - "%dx%d+%d+%d" % [ dst.width, dst.height, 0, (self.height * scale - dst.height) / 2 ] + "%dx%d+%d+%d" % [ dst.width, dst.height, 0, ((self.height * scale) - dst.height) / 2 ] else - "%dx%d+%d+%d" % [ dst.width, dst.height, (self.width * scale - dst.width) / 2, 0 ] + "%dx%d+%d+%d" % [ dst.width, dst.height, ((self.width * scale) - dst.width) / 2, 0 ] end end end diff --git a/lib/paperclip/interpolations.rb b/lib/paperclip/interpolations.rb index d6d8f47..8e9e177 100644 --- a/lib/paperclip/interpolations.rb +++ b/lib/paperclip/interpolations.rb @@ -39,29 +39,29 @@ module Paperclip end # Returns the filename, the same way as ":basename.:extension" would. - def filename attachment, style_name + def filename(attachment, style_name) "#{basename(attachment, style_name)}.#{extension(attachment, style_name)}" end # This interpolation is used in the default :path to ease default specifications. # So it just interpolates :url template without checking if preocessing and # file existence. - def url attachment, style_name + def url(attachment, style_name) interpolate(attachment.class.url_template, attachment, style_name) end # Returns the timestamp as defined by the _updated_at field - def timestamp attachment, style_name + def timestamp(attachment, _style_name) attachment.instance_read(:updated_at).to_s end # Returns the Rails.root constant. - def rails_root attachment, style_name + def rails_root(_attachment, _style_name) Rails.root end # Returns the Rails.env constant. - def rails_env attachment, style_name + def rails_env(_attachment, _style_name) Rails.env end @@ -69,38 +69,38 @@ module Paperclip # e.g. "users" for the User class. # NOTE: The arguments need to be optional, because some tools fetch # all class names. Calling #class will return the expected class. - def class attachment = nil, style_name = nil + def class(attachment = nil, style_name = nil) return super() if attachment.nil? && style_name.nil? plural_cache.underscore_and_pluralize_class(attachment.instance.class) end # Returns the basename of the file. e.g. "file" for "file.jpg" - def basename attachment, style_name + def basename(attachment, _style_name) File.basename(attachment.original_filename, ".*") end # Returns the extension of the file. e.g. "jpg" for "file.jpg" # If the style has a format defined, it will return the format instead # of the actual extension. - def extension attachment, style_name + def extension(attachment, style_name) ((style_name = attachment.styles[style_name]) && style_name[:format]) || File.extname(attachment.original_filename)[1..-1] || '' end # Returns the id of the instance. - def id attachment, style_name + def id(attachment, _style_name) attachment.instance.id end # Returns the id of the instance in a split path form. e.g. returns # 000/001/234 for an id of 1234. - def id_partition attachment, style_name + def id_partition(attachment, _style_name) ("%09d" % attachment.instance.id).scan(/\d{3}/).join("/") end # Returns the pluralized form of the attachment name. e.g. # "avatars" for an attachment of :avatar - def attachment attachment, style_name + def attachment(attachment, _style_name) plural_cache.pluralize_symbol(attachment.name) end diff --git a/lib/paperclip/iostream.rb b/lib/paperclip/iostream.rb index 806d122..d14407a 100644 --- a/lib/paperclip/iostream.rb +++ b/lib/paperclip/iostream.rb @@ -15,9 +15,8 @@ module IOStream # in as the destination and returns the IO or Tempfile as passed in if one is sent as the destination. def stream_to object, path_or_file, in_blocks_of = 8192 dstio = case path_or_file - when String then File.new(path_or_file, "wb+") - when IO then path_or_file - when Tempfile then path_or_file + when String then File.new(path_or_file, "wb+") + when IO, Tempfile then path_or_file end buffer = "" object.rewind diff --git a/lib/paperclip/matchers/validate_attachment_size_matcher.rb b/lib/paperclip/matchers/validate_attachment_size_matcher.rb index f84c479..a05e00e 100644 --- a/lib/paperclip/matchers/validate_attachment_size_matcher.rb +++ b/lib/paperclip/matchers/validate_attachment_size_matcher.rb @@ -8,7 +8,7 @@ module Paperclip class ValidateAttachmentSizeMatcher def initialize attachment_name @attachment_name = attachment_name - @low, @high = 0, (1.0/0) + @low, @high = 0, Float::INFINITY end def less_than size @@ -68,12 +68,12 @@ module Paperclip end def lower_than_high? - return true if @high == (1.0/0) + return true if @high == Float::INFINITY passes_validation_with_size(@high - 1) end def higher_than_high? - return true if @high == (1.0/0) + return true if @high == Float::INFINITY not passes_validation_with_size(@high + 1) end end diff --git a/lib/paperclip/optimizer.rb b/lib/paperclip/optimizer.rb index 0af26b9..ccd3316 100644 --- a/lib/paperclip/optimizer.rb +++ b/lib/paperclip/optimizer.rb @@ -15,7 +15,8 @@ module Paperclip Paperclip::Upfile.content_type_from_file(@file.path) end - def optimize(file) + def optimize(_file) + # TODO: use the arg? src = @file.path dst = "#{src}-#{SecureRandom.hex}" src_shell = src.shellescape diff --git a/lib/paperclip/railtie.rb b/lib/paperclip/railtie.rb index f92dc15..3d0977b 100644 --- a/lib/paperclip/railtie.rb +++ b/lib/paperclip/railtie.rb @@ -2,7 +2,7 @@ module Paperclip class Railtie < Rails::Railtie initializer 'paperclip.railtie.configure' do if defined? Rails.root - Dir.glob(File.join(File.expand_path(Rails.root), "lib", "paperclip_processors", "*.rb")).each do |processor| + Dir.glob(File.join(File.expand_path(Rails.root), "lib", "paperclip_processors", "*.rb")).sort.each do |processor| require processor end end diff --git a/lib/paperclip/storage/cached.rb b/lib/paperclip/storage/cached.rb index cdbc1e0..39d150e 100644 --- a/lib/paperclip/storage/cached.rb +++ b/lib/paperclip/storage/cached.rb @@ -130,7 +130,7 @@ module Paperclip # Enqueues all pending jobs. First, jobs are placed to internal queue in flush_writes # (in after_save) and this method pushes them for execution (in after_commit). def flush_jobs - queued_jobs&.each(&:call).clear + queued_jobs&.each(&:call)&.clear end def upload_to(store_id) diff --git a/lib/paperclip/storage/delayeds3.rb b/lib/paperclip/storage/delayeds3.rb index 35ae8f8..8552867 100644 --- a/lib/paperclip/storage/delayeds3.rb +++ b/lib/paperclip/storage/delayeds3.rb @@ -319,7 +319,8 @@ module Paperclip break if File.exist?(path) # Ruby 1.9.2 does not raise if the removal failed. end rescue Errno::EEXIST, Errno::EACCES, Errno::ENOTEMPTY, - Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR, Errno::ESTALE => _e + Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR, Errno::ESTALE + # already deleted rescue SystemCallError => e Rollbar.error(e, {path: path, initial_path: initial_path}) end @@ -333,7 +334,8 @@ module Paperclip -> { delete_local_files!(filenames) } end - def delete_local_files!(filenames = filesystem_paths.values) + def delete_local_files!(_filenames = filesystem_paths.values) + # TODO: _filenames как-то должно использоваться? filesystem_paths.values.each do |filename| log("Deleting local file #{filename}") delete_recursive(filename) @@ -341,7 +343,7 @@ module Paperclip end def flush_jobs - queued_jobs&.each(&:call).clear + queued_jobs&.each(&:call)&.clear end def upload_to(store_id) diff --git a/lib/paperclip/storage/filesystem.rb b/lib/paperclip/storage/filesystem.rb index 0a4829e..17bd035 100644 --- a/lib/paperclip/storage/filesystem.rb +++ b/lib/paperclip/storage/filesystem.rb @@ -52,7 +52,7 @@ module Paperclip begin log("deleting #{path}") FileUtils.rm(path) - rescue Errno::ENOENT => e + rescue Errno::ENOENT # ignore file-not-found, let everything else pass end begin diff --git a/lib/tasks/paperclip_tasks.rake b/lib/tasks/paperclip_tasks.rake index 8efc571..a3e8188 100644 --- a/lib/tasks/paperclip_tasks.rake +++ b/lib/tasks/paperclip_tasks.rake @@ -52,7 +52,7 @@ namespace :paperclip do desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)." task :metadata => :environment do for_all_attachments do |instance, name| - if file = instance.send(name).to_file + if (file = instance.send(name).to_file) instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip) instance.send("#{name}_content_type=", file.content_type.strip) instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")