From c29256aa81e9077d7712d9e717a2cf758d09a2b1 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Mon, 11 Sep 2023 14:10:16 +0200 Subject: [PATCH] Fix Rubocop offenses --- app/controllers/concerns/file_conversion.rb | 2 +- app/controllers/error_template_attributes_controller.rb | 2 +- app/controllers/error_templates_controller.rb | 2 +- app/controllers/execution_environments_controller.rb | 2 +- app/controllers/exercises_controller.rb | 4 ++-- app/controllers/file_templates_controller.rb | 2 +- app/controllers/file_types_controller.rb | 2 +- app/controllers/internal_users_controller.rb | 2 +- app/controllers/submissions_controller.rb | 2 +- app/controllers/tips_controller.rb | 2 +- app/models/testrun.rb | 2 +- db/migrate/20140918063522_add_hashed_content_to_files.rb | 2 +- db/migrate/20170830083601_add_cause_to_testruns.rb | 2 +- .../20180703125302_create_exercise_collection_items.rb | 2 +- db/migrate/20181129093207_drop_errors.rb | 2 +- ...alized_score_and_submission_to_user_exercise_feedback.rb | 2 +- ...change_type_of_exposed_ports_in_execution_environment.rb | 4 ++-- db/migrate/20230819084917_unify_lti_parameters.rb | 6 +++--- ...30821062901_add_foreign_keys_to_anomaly_notifications.rb | 2 +- ...63101_convert_reason_to_json_in_anomaly_notifications.rb | 2 +- lib/tasks/export_public_exercises.rake | 2 +- spec/features/editor_spec.rb | 2 +- spec/features/external_user_statistics_spec.rb | 2 +- 23 files changed, 27 insertions(+), 27 deletions(-) diff --git a/app/controllers/concerns/file_conversion.rb b/app/controllers/concerns/file_conversion.rb index 4a453700..e42d2547 100644 --- a/app/controllers/concerns/file_conversion.rb +++ b/app/controllers/concerns/file_conversion.rb @@ -21,7 +21,7 @@ module FileConversion # Optimize SQL queries: We are first fetching all required file types from the database. # Then, we store them in a hash, so that we can access them by using their file extension. file_types = {} - FileType.where(file_extension: files.pluck('extension')).each do |file_type| + FileType.where(file_extension: files.pluck('extension')).find_each do |file_type| file_types[file_type.file_extension] = file_type end diff --git a/app/controllers/error_template_attributes_controller.rb b/app/controllers/error_template_attributes_controller.rb index 3ad29480..b00b387a 100644 --- a/app/controllers/error_template_attributes_controller.rb +++ b/app/controllers/error_template_attributes_controller.rb @@ -11,7 +11,7 @@ class ErrorTemplateAttributesController < ApplicationController # GET /error_template_attributes # GET /error_template_attributes.json def index - @error_template_attributes = ErrorTemplateAttribute.all.order('important DESC', :key, + @error_template_attributes = ErrorTemplateAttribute.order('important DESC', :key, :id).paginate(page: params[:page], per_page: per_page_param) authorize! end diff --git a/app/controllers/error_templates_controller.rb b/app/controllers/error_templates_controller.rb index 6825d3dc..6b89e680 100644 --- a/app/controllers/error_templates_controller.rb +++ b/app/controllers/error_templates_controller.rb @@ -11,7 +11,7 @@ class ErrorTemplatesController < ApplicationController # GET /error_templates # GET /error_templates.json def index - @error_templates = ErrorTemplate.all.order(:execution_environment_id, :name).paginate(page: params[:page], per_page: per_page_param) + @error_templates = ErrorTemplate.order(:execution_environment_id, :name).paginate(page: params[:page], per_page: per_page_param) authorize! end diff --git a/app/controllers/execution_environments_controller.rb b/app/controllers/execution_environments_controller.rb index d27421a3..39c09c43 100644 --- a/app/controllers/execution_environments_controller.rb +++ b/app/controllers/execution_environments_controller.rb @@ -15,7 +15,7 @@ class ExecutionEnvironmentsController < ApplicationController private :authorize! def index - @execution_environments = ExecutionEnvironment.all.includes(:user).order(:name).paginate(page: params[:page], per_page: per_page_param) + @execution_environments = ExecutionEnvironment.includes(:user).order(:name).paginate(page: params[:page], per_page: per_page_param) authorize! end diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 31c45d2e..b2052429 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -434,7 +434,7 @@ class ExercisesController < ApplicationController private :not_authorized_for_exercise def set_execution_environments - @execution_environments = ExecutionEnvironment.all.order(:name) + @execution_environments = ExecutionEnvironment.order(:name) end private :set_execution_environments @@ -456,7 +456,7 @@ class ExercisesController < ApplicationController private :set_external_user_and_authorize def set_file_types - @file_types = FileType.all.order(:name) + @file_types = FileType.order(:name) end private :set_file_types diff --git a/app/controllers/file_templates_controller.rb b/app/controllers/file_templates_controller.rb index e8813e1c..224860e0 100644 --- a/app/controllers/file_templates_controller.rb +++ b/app/controllers/file_templates_controller.rb @@ -19,7 +19,7 @@ class FileTemplatesController < ApplicationController # GET /file_templates # GET /file_templates.json def index - @file_templates = FileTemplate.all.order(:file_type_id).paginate(page: params[:page], per_page: per_page_param) + @file_templates = FileTemplate.order(:file_type_id).paginate(page: params[:page], per_page: per_page_param) authorize! end diff --git a/app/controllers/file_types_controller.rb b/app/controllers/file_types_controller.rb index ea7d67b4..f69a795b 100644 --- a/app/controllers/file_types_controller.rb +++ b/app/controllers/file_types_controller.rb @@ -12,7 +12,7 @@ class FileTypesController < ApplicationController private :authorize! def index - @file_types = FileType.all.includes(:user).order(:name).paginate(page: params[:page], per_page: per_page_param) + @file_types = FileType.includes(:user).order(:name).paginate(page: params[:page], per_page: per_page_param) authorize! end diff --git a/app/controllers/internal_users_controller.rb b/app/controllers/internal_users_controller.rb index 9bc96e83..d6a03d13 100644 --- a/app/controllers/internal_users_controller.rb +++ b/app/controllers/internal_users_controller.rb @@ -150,7 +150,7 @@ class InternalUsersController < ApplicationController @user ||= InternalUser.new # Only needed for the `create` action checked_study_group_memberships = @user.study_group_memberships checked_study_groups = checked_study_group_memberships.collect(&:study_group).sort.to_set - unchecked_study_groups = StudyGroup.all.order(name: :asc).to_set.subtract checked_study_groups + unchecked_study_groups = StudyGroup.order(name: :asc).to_set.subtract checked_study_groups @study_group_memberships = checked_study_group_memberships + unchecked_study_groups.collect do |study_group| StudyGroupMembership.new(user: @user, study_group:) end diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index 508433a5..b60eb36a 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -377,7 +377,7 @@ class SubmissionsController < ApplicationController # # Reloading the ErrorTemplate is necessary to allow preloading the ErrorTemplateAttributes. # However, this results in less (and faster) SQL queries than performing manual lookups. - ErrorTemplate.where(id: matching_error_templates).joins(:error_template_attributes).includes(:error_template_attributes).each do |template| + ErrorTemplate.where(id: matching_error_templates).joins(:error_template_attributes).includes(:error_template_attributes).find_each do |template| results << StructuredError.create_from_template(template, @testrun[:output], @submission) end end diff --git a/app/controllers/tips_controller.rb b/app/controllers/tips_controller.rb index a49a6ff2..aae4f2bd 100644 --- a/app/controllers/tips_controller.rb +++ b/app/controllers/tips_controller.rb @@ -56,7 +56,7 @@ class TipsController < ApplicationController end def set_file_types - @file_types = FileType.all.order(:name) + @file_types = FileType.order(:name) end private :set_file_types end diff --git a/app/models/testrun.rb b/app/models/testrun.rb index 757d3e64..ed680461 100644 --- a/app/models/testrun.rb +++ b/app/models/testrun.rb @@ -20,6 +20,6 @@ class Testrun < ApplicationRecord validates :status, presence: true def log - testrun_messages.output.select(:log).map(&:log).join.presence + testrun_messages.output.pluck(:log).join.presence end end diff --git a/db/migrate/20140918063522_add_hashed_content_to_files.rb b/db/migrate/20140918063522_add_hashed_content_to_files.rb index f4d4a4a1..8b5393d4 100644 --- a/db/migrate/20140918063522_add_hashed_content_to_files.rb +++ b/db/migrate/20140918063522_add_hashed_content_to_files.rb @@ -6,7 +6,7 @@ class AddHashedContentToFiles < ActiveRecord::Migration[4.2] reversible do |direction| direction.up do - CodeOcean::File.unscope(:order).all.each(&:save) + CodeOcean::File.unscope(:order).find_each(&:save) end end end diff --git a/db/migrate/20170830083601_add_cause_to_testruns.rb b/db/migrate/20170830083601_add_cause_to_testruns.rb index 6a5617a1..73de7e9c 100644 --- a/db/migrate/20170830083601_add_cause_to_testruns.rb +++ b/db/migrate/20170830083601_add_cause_to_testruns.rb @@ -4,7 +4,7 @@ class AddCauseToTestruns < ActiveRecord::Migration[4.2] def up add_column :testruns, :cause, :string Testrun.reset_column_information - Testrun.all.each do |testrun| + Testrun.find_each do |testrun| if testrun.submission.nil? say_with_time "#{testrun.id} has no submission" else diff --git a/db/migrate/20180703125302_create_exercise_collection_items.rb b/db/migrate/20180703125302_create_exercise_collection_items.rb index ea63a8b6..a41f1d19 100644 --- a/db/migrate/20180703125302_create_exercise_collection_items.rb +++ b/db/migrate/20180703125302_create_exercise_collection_items.rb @@ -4,7 +4,7 @@ class CreateExerciseCollectionItems < ActiveRecord::Migration[4.2] def up rename_table :exercise_collections_exercises, :exercise_collection_items add_column :exercise_collection_items, :position, :integer, default: 0, null: false - add_column :exercise_collection_items, :id, :primary_key + add_column :exercise_collection_items, :id, :primary_key # rubocop:disable Rails/DangerousColumnNames end def down diff --git a/db/migrate/20181129093207_drop_errors.rb b/db/migrate/20181129093207_drop_errors.rb index 90973d62..213e22e1 100644 --- a/db/migrate/20181129093207_drop_errors.rb +++ b/db/migrate/20181129093207_drop_errors.rb @@ -24,7 +24,7 @@ class DropErrors < ActiveRecord::Migration[5.2] submissions_controller = SubmissionsController.new # Iterate only over those Errors containing a message and submission_id - CodeOcean::Error.where.not(message: [nil, '']).where.not(submission_id: [nil, '']).each do |error| + CodeOcean::Error.where.not(message: [nil, '']).where.not(submission_id: [nil, '']).find_each do |error| raw_output = error.message submission = Submission.find_by(id: error.submission_id) diff --git a/db/migrate/20201019090123_add_normalized_score_and_submission_to_user_exercise_feedback.rb b/db/migrate/20201019090123_add_normalized_score_and_submission_to_user_exercise_feedback.rb index ef06fce1..2fc9e8ff 100644 --- a/db/migrate/20201019090123_add_normalized_score_and_submission_to_user_exercise_feedback.rb +++ b/db/migrate/20201019090123_add_normalized_score_and_submission_to_user_exercise_feedback.rb @@ -7,7 +7,7 @@ class AddNormalizedScoreAndSubmissionToUserExerciseFeedback < ActiveRecord::Migr # Disable automatic timestamp modification ActiveRecord::Base.record_timestamps = false - UserExerciseFeedback.all.find_each do |uef| + UserExerciseFeedback.find_each do |uef| latest_submission = Submission .where(user_id: uef.user_id, user_type: uef.user_type, exercise_id: uef.exercise_id) .where('created_at < ?', uef.updated_at) diff --git a/db/migrate/20210602071834_change_type_of_exposed_ports_in_execution_environment.rb b/db/migrate/20210602071834_change_type_of_exposed_ports_in_execution_environment.rb index bac19565..6715c59a 100644 --- a/db/migrate/20210602071834_change_type_of_exposed_ports_in_execution_environment.rb +++ b/db/migrate/20210602071834_change_type_of_exposed_ports_in_execution_environment.rb @@ -6,7 +6,7 @@ class ChangeTypeOfExposedPortsInExecutionEnvironment < ActiveRecord::Migration[6 rename_column :execution_environments, :exposed_ports, :exposed_ports_migration add_column :execution_environments, :exposed_ports, :integer, array: true, default: [], nil: true - ExecutionEnvironment.all.each do |execution_environment| + ExecutionEnvironment.find_each do |execution_environment| next if execution_environment.exposed_ports_migration.nil? cleaned = execution_environment.exposed_ports_migration.scan(/\d+/) @@ -21,7 +21,7 @@ class ChangeTypeOfExposedPortsInExecutionEnvironment < ActiveRecord::Migration[6 rename_column :execution_environments, :exposed_ports, :exposed_ports_migration add_column :execution_environments, :exposed_ports, :string - ExecutionEnvironment.all.each do |execution_environment| + ExecutionEnvironment.find_each do |execution_environment| next if execution_environment.exposed_ports_migration.empty? list = execution_environment.exposed_ports_migration diff --git a/db/migrate/20230819084917_unify_lti_parameters.rb b/db/migrate/20230819084917_unify_lti_parameters.rb index 61f82cc2..ce8ec6b1 100644 --- a/db/migrate/20230819084917_unify_lti_parameters.rb +++ b/db/migrate/20230819084917_unify_lti_parameters.rb @@ -6,13 +6,13 @@ class UnifyLtiParameters < ActiveRecord::Migration[7.0] dir.up do # We cannot add a foreign key to a table that has rows that violate the constraint. LtiParameter.where(external_users_id: nil) - .or(LtiParameter.where.not(external_users_id: ExternalUser.all.select(:id))) + .or(LtiParameter.where.not(external_users_id: ExternalUser.select(:id))) .or(LtiParameter.where(exercises_id: nil)) - .or(LtiParameter.where.not(exercises_id: Exercise.all.select(:id))) + .or(LtiParameter.where.not(exercises_id: Exercise.select(:id))) .delete_all # For each user/exercise pair, keep the most recent LtiParameter. - LtiParameter.all.group(:external_users_id, :exercises_id).having('count(*) > 1').count.each do |ids, count| + LtiParameter.group(:external_users_id, :exercises_id).having('count(*) > 1').count.each do |ids, count| LtiParameter.where(external_users_id: ids.first, exercises_id: ids.second).order(updated_at: :asc).limit(count - 1).delete_all end change_column :lti_parameters, :id, :bigint diff --git a/db/migrate/20230821062901_add_foreign_keys_to_anomaly_notifications.rb b/db/migrate/20230821062901_add_foreign_keys_to_anomaly_notifications.rb index 52f2d46d..b81f8824 100644 --- a/db/migrate/20230821062901_add_foreign_keys_to_anomaly_notifications.rb +++ b/db/migrate/20230821062901_add_foreign_keys_to_anomaly_notifications.rb @@ -4,7 +4,7 @@ class AddForeignKeysToAnomalyNotifications < ActiveRecord::Migration[7.0] def change up_only do # We cannot add a foreign key to a table that has rows that violate the constraint. - AnomalyNotification.where.not(exercise_id: Exercise.all.select(:id)).delete_all + AnomalyNotification.where.not(exercise_id: Exercise.select(:id)).delete_all end change_column_null :anomaly_notifications, :contributor_id, false diff --git a/db/migrate/20230821063101_convert_reason_to_json_in_anomaly_notifications.rb b/db/migrate/20230821063101_convert_reason_to_json_in_anomaly_notifications.rb index c34b9b07..7718ef82 100644 --- a/db/migrate/20230821063101_convert_reason_to_json_in_anomaly_notifications.rb +++ b/db/migrate/20230821063101_convert_reason_to_json_in_anomaly_notifications.rb @@ -2,7 +2,7 @@ class ConvertReasonToJsonInAnomalyNotifications < ActiveRecord::Migration[7.0] def up - AnomalyNotification.where("reason LIKE '%value:%'").each do |anomaly_notification| + AnomalyNotification.where("reason LIKE '%value:%'").find_each do |anomaly_notification| reason = anomaly_notification.reason reason = reason.gsub('value:', '"value":') reason = reason.gsub(/"(\d+\.\d+)"/) {|_| Regexp.last_match(1) } diff --git a/lib/tasks/export_public_exercises.rake b/lib/tasks/export_public_exercises.rake index 023ddec1..8c30a116 100644 --- a/lib/tasks/export_public_exercises.rake +++ b/lib/tasks/export_public_exercises.rake @@ -6,7 +6,7 @@ namespace :export_exercises do codeharbor_link = CodeharborLink.find(args.codeharbor_link_id) successful_exports = [] failed_exports = [] - Exercise.where(public: true).each do |exercise| + Exercise.where(public: true).find_each do |exercise| puts "Exporting exercise ##{exercise.id}" error = ExerciseService::PushExternal.call( zip: ProformaService::ExportTask.call(exercise:), diff --git a/spec/features/editor_spec.rb b/spec/features/editor_spec.rb index a615d01c..41c30149 100644 --- a/spec/features/editor_spec.rb +++ b/spec/features/editor_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -describe 'Editor', js: true do +describe 'Editor', :js do let(:exercise) { create(:audio_video, description: Forgery(:lorem_ipsum).sentence) } let(:scoring_response) do [{ diff --git a/spec/features/external_user_statistics_spec.rb b/spec/features/external_user_statistics_spec.rb index f579a635..0fa9767a 100644 --- a/spec/features/external_user_statistics_spec.rb +++ b/spec/features/external_user_statistics_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -describe 'ExternalUserStatistics', js: true do +describe 'ExternalUserStatistics', :js do let(:learner) { create(:external_user) } let(:exercise) { create(:dummy, user:) } let(:study_group) { create(:study_group) }