Fix rubocop lint warnings

This commit is contained in:
Vasily Fedoseyev
2022-04-18 20:45:59 +03:00
parent b98762edca
commit 6fb3f7d931
13 changed files with 40 additions and 38 deletions

View File

@@ -238,7 +238,7 @@ module Paperclip
attachment_for(name).file? attachment_for(name).file?
end end
validates_each(name) do |record, attr, value| validates_each(name) do |record, _attr, _value|
attachment = record.attachment_for(name) attachment = record.attachment_for(name)
attachment.send(:flush_errors) unless attachment.valid? attachment.send(:flush_errors) unless attachment.valid?
end end
@@ -307,21 +307,21 @@ module Paperclip
end end
def each_attachment def each_attachment
self.class.attachment_definitions.each do |name, definition| self.class.attachment_definitions.each do |name, _definition|
yield(name, attachment_for(name)) yield(name, attachment_for(name))
end end
end end
def save_attached_files def save_attached_files
logger.info("[paperclip] Saving attachments.") logger.info("[paperclip] Saving attachments.")
each_attachment do |name, attachment| each_attachment do |_name, attachment|
attachment.send(:save) attachment.send(:save)
end end
end end
def destroy_attached_files def destroy_attached_files
logger.info("[paperclip] Deleting attachments.") logger.info("[paperclip] Deleting attachments.")
each_attachment do |name, attachment| each_attachment do |_name, attachment|
attachment.send(:queue_existing_for_delete) attachment.send(:queue_existing_for_delete)
attachment.send(:flush_deletes) attachment.send(:flush_deletes)
end end
@@ -330,7 +330,7 @@ module Paperclip
def flush_attachment_jobs def flush_attachment_jobs
logger.info("[paperclip] flushing jobs.") logger.info("[paperclip] flushing jobs.")
each_attachment do |name, attachment| each_attachment do |_name, attachment|
attachment.try(:flush_jobs) attachment.try(:flush_jobs)
end end
end end

View File

@@ -451,7 +451,7 @@ module Paperclip
end end
def flush_errors #:nodoc: def flush_errors #:nodoc:
errors.each do |error, message| errors.each do |_error, message|
[message].flatten.each {|m| instance.errors.add(name, m) } [message].flatten.each {|m| instance.errors.add(name, m) }
end end
end end

View File

@@ -21,13 +21,13 @@ module Paperclip
end end
module Running 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 # 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. # 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 blk.call
end end
run_callbacks(:"after_#{callback}"){ |result, object| result == false } run_callbacks(:"after_#{callback}"){ |result, _object| result == false }
end end
end end
end end
@@ -39,7 +39,7 @@ module Paperclip
if rails_version >= Gem::Version.new('5.0') if rails_version >= Gem::Version.new('5.0')
{} {}
elsif rails_version >= Gem::Version.new('4.1') elsif rails_version >= Gem::Version.new('4.1')
{terminator: ->(target, result) { result == false }} {terminator: ->(_target, result) { result == false }}
else else
{terminator: 'result == false'} {terminator: 'result == false'}
end end

View File

@@ -36,7 +36,7 @@ module Paperclip
# Parses a "WxH" formatted string, where W is the width and H is the height. # Parses a "WxH" formatted string, where W is the width and H is the height.
def self.parse string 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( Geometry.new(
width: match[1], width: match[1],
height: match[2], height: match[2],
@@ -129,9 +129,9 @@ module Paperclip
def cropping dst, ratio, scale def cropping dst, ratio, scale
if ratio.horizontal? || ratio.square? 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 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 end
end end

View File

@@ -39,29 +39,29 @@ module Paperclip
end end
# Returns the filename, the same way as ":basename.:extension" would. # 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)}" "#{basename(attachment, style_name)}.#{extension(attachment, style_name)}"
end end
# This interpolation is used in the default :path to ease default specifications. # This interpolation is used in the default :path to ease default specifications.
# So it just interpolates :url template without checking if preocessing and # So it just interpolates :url template without checking if preocessing and
# file existence. # file existence.
def url attachment, style_name def url(attachment, style_name)
interpolate(attachment.class.url_template, attachment, style_name) interpolate(attachment.class.url_template, attachment, style_name)
end end
# Returns the timestamp as defined by the <attachment>_updated_at field # Returns the timestamp as defined by the <attachment>_updated_at field
def timestamp attachment, style_name def timestamp(attachment, _style_name)
attachment.instance_read(:updated_at).to_s attachment.instance_read(:updated_at).to_s
end end
# Returns the Rails.root constant. # Returns the Rails.root constant.
def rails_root attachment, style_name def rails_root(_attachment, _style_name)
Rails.root Rails.root
end end
# Returns the Rails.env constant. # Returns the Rails.env constant.
def rails_env attachment, style_name def rails_env(_attachment, _style_name)
Rails.env Rails.env
end end
@@ -69,38 +69,38 @@ module Paperclip
# e.g. "users" for the User class. # e.g. "users" for the User class.
# NOTE: The arguments need to be optional, because some tools fetch # NOTE: The arguments need to be optional, because some tools fetch
# all class names. Calling #class will return the expected class. # 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? return super() if attachment.nil? && style_name.nil?
plural_cache.underscore_and_pluralize_class(attachment.instance.class) plural_cache.underscore_and_pluralize_class(attachment.instance.class)
end end
# Returns the basename of the file. e.g. "file" for "file.jpg" # 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, ".*") File.basename(attachment.original_filename, ".*")
end end
# Returns the extension of the file. e.g. "jpg" for "file.jpg" # 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 # If the style has a format defined, it will return the format instead
# of the actual extension. # of the actual extension.
def extension attachment, style_name def extension(attachment, style_name)
((style_name = attachment.styles[style_name]) && style_name[:format]) || ((style_name = attachment.styles[style_name]) && style_name[:format]) ||
File.extname(attachment.original_filename)[1..-1] || '' File.extname(attachment.original_filename)[1..-1] || ''
end end
# Returns the id of the instance. # Returns the id of the instance.
def id attachment, style_name def id(attachment, _style_name)
attachment.instance.id attachment.instance.id
end end
# Returns the id of the instance in a split path form. e.g. returns # Returns the id of the instance in a split path form. e.g. returns
# 000/001/234 for an id of 1234. # 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("/") ("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
end end
# Returns the pluralized form of the attachment name. e.g. # Returns the pluralized form of the attachment name. e.g.
# "avatars" for an attachment of :avatar # "avatars" for an attachment of :avatar
def attachment attachment, style_name def attachment(attachment, _style_name)
plural_cache.pluralize_symbol(attachment.name) plural_cache.pluralize_symbol(attachment.name)
end end

View File

@@ -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. # 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 def stream_to object, path_or_file, in_blocks_of = 8192
dstio = case path_or_file dstio = case path_or_file
when String then File.new(path_or_file, "wb+") when String then File.new(path_or_file, "wb+")
when IO then path_or_file when IO, Tempfile then path_or_file
when Tempfile then path_or_file
end end
buffer = "" buffer = ""
object.rewind object.rewind

View File

@@ -8,7 +8,7 @@ module Paperclip
class ValidateAttachmentSizeMatcher class ValidateAttachmentSizeMatcher
def initialize attachment_name def initialize attachment_name
@attachment_name = attachment_name @attachment_name = attachment_name
@low, @high = 0, (1.0/0) @low, @high = 0, Float::INFINITY
end end
def less_than size def less_than size
@@ -68,12 +68,12 @@ module Paperclip
end end
def lower_than_high? def lower_than_high?
return true if @high == (1.0/0) return true if @high == Float::INFINITY
passes_validation_with_size(@high - 1) passes_validation_with_size(@high - 1)
end end
def higher_than_high? def higher_than_high?
return true if @high == (1.0/0) return true if @high == Float::INFINITY
not passes_validation_with_size(@high + 1) not passes_validation_with_size(@high + 1)
end end
end end

View File

@@ -15,7 +15,8 @@ module Paperclip
Paperclip::Upfile.content_type_from_file(@file.path) Paperclip::Upfile.content_type_from_file(@file.path)
end end
def optimize(file) def optimize(_file)
# TODO: use the arg?
src = @file.path src = @file.path
dst = "#{src}-#{SecureRandom.hex}" dst = "#{src}-#{SecureRandom.hex}"
src_shell = src.shellescape src_shell = src.shellescape

View File

@@ -2,7 +2,7 @@ module Paperclip
class Railtie < Rails::Railtie class Railtie < Rails::Railtie
initializer 'paperclip.railtie.configure' do initializer 'paperclip.railtie.configure' do
if defined? Rails.root 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 require processor
end end
end end

View File

@@ -130,7 +130,7 @@ module Paperclip
# Enqueues all pending jobs. First, jobs are placed to internal queue in flush_writes # 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). # (in after_save) and this method pushes them for execution (in after_commit).
def flush_jobs def flush_jobs
queued_jobs&.each(&:call).clear queued_jobs&.each(&:call)&.clear
end end
def upload_to(store_id) def upload_to(store_id)

View File

@@ -319,7 +319,8 @@ module Paperclip
break if File.exist?(path) # Ruby 1.9.2 does not raise if the removal failed. break if File.exist?(path) # Ruby 1.9.2 does not raise if the removal failed.
end end
rescue Errno::EEXIST, Errno::EACCES, Errno::ENOTEMPTY, 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 rescue SystemCallError => e
Rollbar.error(e, {path: path, initial_path: initial_path}) Rollbar.error(e, {path: path, initial_path: initial_path})
end end
@@ -333,7 +334,8 @@ module Paperclip
-> { delete_local_files!(filenames) } -> { delete_local_files!(filenames) }
end 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| filesystem_paths.values.each do |filename|
log("Deleting local file #{filename}") log("Deleting local file #{filename}")
delete_recursive(filename) delete_recursive(filename)
@@ -341,7 +343,7 @@ module Paperclip
end end
def flush_jobs def flush_jobs
queued_jobs&.each(&:call).clear queued_jobs&.each(&:call)&.clear
end end
def upload_to(store_id) def upload_to(store_id)

View File

@@ -52,7 +52,7 @@ module Paperclip
begin begin
log("deleting #{path}") log("deleting #{path}")
FileUtils.rm(path) FileUtils.rm(path)
rescue Errno::ENOENT => e rescue Errno::ENOENT
# ignore file-not-found, let everything else pass # ignore file-not-found, let everything else pass
end end
begin begin

View File

@@ -52,7 +52,7 @@ namespace :paperclip do
desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)." desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
task :metadata => :environment do task :metadata => :environment do
for_all_attachments do |instance, name| 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}_file_name=", instance.send("#{name}_file_name").strip)
instance.send("#{name}_content_type=", file.content_type.strip) instance.send("#{name}_content_type=", file.content_type.strip)
instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size") instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")