Automatic rubocop cleanups

This commit is contained in:
Christoph Haas 2025-02-03 01:08:08 +01:00
parent 890906929f
commit 2ec80b4bde
3 changed files with 96 additions and 97 deletions

View file

@ -1,5 +1,5 @@
# Style/Encoding:
# Enabled: false
Style/Encoding:
Enabled: false
Layout/LineLength:
Max: 99

View file

@ -1,7 +1,6 @@
class PackagesController < ApplicationController
# Allow legacy /uploadfile URL without CSRF protection
protect_from_forgery :except => :legacy_uploadfile
protect_from_forgery except: :legacy_uploadfile
def list
@packages = query_packages.paginate(page: params[:page], per_page: 6)
@ -20,9 +19,7 @@ class PackagesController < ApplicationController
# Highlight a certain screenshot (by ID). This is used by the logs.slim
# view to link to a certain screenshot of a package.
if params[:highlight]
@highlight_id = params[:highlight].to_i
end
@highlight_id = params[:highlight].to_i if params[:highlight]
if @package.nil?
@packagename = params[:name]
@ -48,8 +45,8 @@ class PackagesController < ApplicationController
@invalid_images = []
# If Javascript is disabled a user may submit an empty selection of images.
if params[:file] == nil
flash[:error] = "You have not selected any images."
if params[:file].nil?
flash[:error] = 'You have not selected any images.'
redirect_to upload_path
return
end
@ -59,7 +56,7 @@ class PackagesController < ApplicationController
files = params[:file]
# Turn into array if a single image was uploaded through AJAX
files=[files] if files.class != Array
files = [files] if files.class != Array
files.each do |img|
new_screenshot = @package.screenshots.new(simage: img)
@ -71,7 +68,7 @@ class PackagesController < ApplicationController
new_screenshot.version = @package.version
# ActiveStorage does not yet create a file checksum automatically.
# Let's do that. It helps detect duplicate uploads later.
new_screenshot.image_fingerprint = Digest::MD5.hexdigest(File.read img.path)
new_screenshot.image_fingerprint = Digest::MD5.hexdigest(File.read(img.path))
# Check that this screenshot is not a duplicate for this package
if @package.screenshots.where(image_fingerprint: new_screenshot.image_fingerprint).any?
@ -86,7 +83,7 @@ class PackagesController < ApplicationController
# But this makes it easier to track who screenshots belong to.
unless user_signed_in?
sign_in User.create_pseudo_user
auditlog "New pseudo user for anonymous upload created and logged in.",
auditlog 'New pseudo user for anonymous upload created and logged in.',
package: @package, screenshot: new_screenshot
end
new_screenshot.user = current_user
@ -105,15 +102,13 @@ class PackagesController < ApplicationController
errors = new_screenshot.errors[:simage]
auditlog "Screenshot #{img.original_filename} invalid (#{errors}).",
package: @package
#@invalid_images.push img.original_filename
#raise
# @invalid_images.push img.original_filename
# raise
all_errors << "Your file #{img.original_filename} #{errors.join(' and ')}."
end
end
if all_errors.any?
flash[:error] = all_errors
end
flash[:error] = all_errors if all_errors.any?
# # Redirect back to upload form if all uploads were invalid
# unless @valid_images.any?
@ -130,16 +125,14 @@ class PackagesController < ApplicationController
# ' #{image.image_file_name} (#{image.errors[:image].join(' and ')})
# Inform the admins about the upload
if @valid_images.any?
AdminMailer.with(package: @package).new_uploads_email.deliver_now
end
AdminMailer.with(package: @package).new_uploads_email.deliver_now if @valid_images.any?
# Rails does not allow dots in the URL. So we cannot use the 'respond_to'
# and 'format' ways to handle parameters. Instead the 'returns' parameters
# is set in routes.rb to signal that this method was called by AJAX.
if params[:returns] == :json
# TODO: send all_errors back as JSON and make Javascript display it in #messages
render :json => {errors: all_errors.join(' ')}
render json: { errors: all_errors.join(' ') }
else
redirect_to package_path
end
@ -186,7 +179,7 @@ class PackagesController < ApplicationController
auditlog "Screenshot #{@screenshot.id} hidden",
package: @screenshot.package
@screenshot.hide!
flash['notice'] = "Screenshot hidden."
flash['notice'] = 'Screenshot hidden.'
redirect_back(fallback_location: package_path(name: @screenshot.package.name))
else
head :forbidden
@ -203,7 +196,7 @@ class PackagesController < ApplicationController
auditlog "Screenshot #{@screenshot.id} un-hidden",
package: @screenshot.package
@screenshot.unhide!
flash['notice'] = "Screenshot un-hidden."
flash['notice'] = 'Screenshot un-hidden.'
redirect_back(fallback_location: package_path(name: @screenshot.package.name))
else
head :forbidden
@ -223,12 +216,12 @@ class PackagesController < ApplicationController
# Increase the rejection counter for the user (social scoring)
# if the screenshot is new and pending approval
if !@screenshot.approved
unless @screenshot.approved
@screenshot.user.rejected_screenshots += 1
@screenshot.user.save!
end
flash['notice'] = "Screenshot deleted."
flash['notice'] = 'Screenshot deleted.'
redirect_back(fallback_location: package_path(name: @screenshot.package.name))
else
head :forbidden
@ -240,14 +233,14 @@ class PackagesController < ApplicationController
if can? :approve, @screenshot
@screenshot.approve!
auditlog "Screenshot approved",
auditlog 'Screenshot approved',
package: @screenshot.package, screenshot: @screenshot
# Increase the approval counter for the user (social scoring)
@screenshot.user.approved_screenshots += 1
@screenshot.user.save!
flash['notice'] = "Screenshot approved."
flash['notice'] = 'Screenshot approved.'
redirect_back(fallback_location: package_path(name: @screenshot.package.name))
else
head :forbidden
@ -261,20 +254,22 @@ class PackagesController < ApplicationController
def thumbnail
@package = Package.find_by(name: params[:name])
unless @package
logger.debug "Cannot render thumbnail for packages #{params[:name]} no such package"
thumbnail404
return
end
# Called as /thumbnail-with-version/:name/:version
if params[:version]
@screenshot = @package.best_screenshot_for_version(params[:version])
@screenshot = if params[:version]
@package.best_screenshot_for_version(params[:version])
# Called as /thumbnail/:name
else
@screenshot = @package.screenshots.approved.first
@package.screenshots.approved.first
end
# Return a 404 if the package has no screenshots or the image was not found
unless @screenshot
logger.debug "Cannot render thumbnail for packages #{params[:name]} no screenshot found"
thumbnail404
return
end
@ -294,11 +289,11 @@ class PackagesController < ApplicationController
end
# Called as /screenshot-with-version/:name/:version
if params[:version]
@screenshot = @package.best_screenshot_for_version(params[:version])
@screenshot = if params[:version]
@package.best_screenshot_for_version(params[:version])
# Called as /screenshot/:name
else
@screenshot = @package.screenshots.approved.first
@package.screenshots.approved.first
end
# Return a 404 if the package has no screenshots or the image was not found
@ -318,7 +313,7 @@ class PackagesController < ApplicationController
# @screenshot.description = params[:description]
@screenshot.update params_screenshot_description
@screenshot.save!
flash['notice'] = "Description updated."
flash['notice'] = 'Description updated.'
redirect_back(fallback_location: package_path(name: @screenshot.package.name))
end
@ -334,12 +329,14 @@ class PackagesController < ApplicationController
# Send a dummy thumbnail reading "No screenshot available. Sorry."
def thumbnail404
send_file Rails.root.join('public/images/dummy/thumbnail404.png'), type: 'image/png', disposition: 'inline', status: 404
send_file Rails.root.join('public/images/dummy/thumbnail404.png'), type: 'image/png',
disposition: 'inline', status: 404
end
# Send a dummy screenshot reading "No screenshot available. Sorry."
def screenshot404
send_file Rails.root.join('public/images/dummy/screenshot404.png'), type: 'image/png', disposition: 'inline', status: 404
send_file Rails.root.join('public/images/dummy/screenshot404.png'), type: 'image/png',
disposition: 'inline', status: 404
end
# Return packages matching the criteria given by parameters
@ -362,7 +359,7 @@ class PackagesController < ApplicationController
logger.debug 'Limiting packages to those without screenshots'
end
return packages
packages
end
# Store a random identifier and the client's IP address in the session
@ -383,10 +380,9 @@ class PackagesController < ApplicationController
json = JSON.parse(body)
# Only show english reviews
# TODO: Support further languages
json = json.select {|x| x['language']=='en'}
json = json.select { |x| x['language'] == 'en' }
# Sort by 'usefulness_total' (how many people found this review useful)
json = json.sort { |x,y| y['usefulness_total'].to_i <=> x['usefulness_total'].to_i}
return json
json.sort { |x, y| y['usefulness_total'].to_i <=> x['usefulness_total'].to_i }
end
def params_screenshot_description

View file

@ -1,4 +1,6 @@
# This controller creates the main page and the about page.
class WelcomeController < ApplicationController
# Render the main page.
def home
@newest_upload = Screenshot.where(approved: true).first
@ -13,6 +15,7 @@ class WelcomeController < ApplicationController
@most_wanted_package = query.offset(random_offset).first
end
# Render the about page.
def about
@package_sources = Rails.configuration.package_sources
@package_count = Package.count