Merge pull request #963 from openHPI/dependabot/bundler/rubocop-rails-2.11.1

Bump rubocop-rails from 2.10.1 to 2.11.1
This commit is contained in:
Sebastian Serth
2021-06-25 17:52:56 +02:00
committed by GitHub
9 changed files with 62 additions and 51 deletions

View File

@ -414,7 +414,7 @@ GEM
rubocop-performance (1.11.3)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.10.1)
rubocop-rails (2.11.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.7.0, < 2.0)

View File

@ -8,7 +8,8 @@ class ApplicationController < ActionController::Base
after_action :verify_authorized, except: %i[welcome]
around_action :mnemosyne_trace
before_action :set_sentry_context, :set_locale, :allow_iframe_requests, :load_embed_options
around_action :switch_locale
before_action :set_sentry_context, :allow_iframe_requests, :load_embed_options
protect_from_forgery(with: :exception, prepend: true)
rescue_from Pundit::NotAuthorizedError, with: :render_not_authorized
rescue_from ActionController::InvalidAuthenticityToken, with: :render_csrf_error
@ -71,12 +72,13 @@ class ApplicationController < ActionController::Base
end
private :render_error
def set_locale
def switch_locale(&action)
session[:locale] = params[:custom_locale] || params[:locale] || session[:locale]
I18n.locale = session[:locale] || I18n.default_locale
Sentry.set_extras(locale: I18n.locale)
locale = session[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
Sentry.set_extras(locale: locale)
end
private :set_locale
private :switch_locale
def welcome
# Show root page

View File

@ -566,7 +566,7 @@ working_time_accumulated: working_time_accumulated})
end
def redirect_after_submit
Rails.logger.debug("Redirecting user with score:s #{@submission.normalized_score}")
Rails.logger.debug { "Redirecting user with score:s #{@submission.normalized_score}" }
if @submission.normalized_score.to_d == 1.0.to_d
# if user is external and has an own rfc, redirect to it and message him to clean up and accept the answer. (we need to check that the user is external,
# otherwise an internal user could be shown a false rfc here, since current_user.id is polymorphic, but only makes sense for external users when used with rfcs.)

View File

@ -197,11 +197,11 @@ module_name: File.basename(filename, File.extname(filename)).underscore}
@docker_client.kill_container(result[:container])
else
socket.send data
Rails.logger.debug("Sent the received client data to docker:#{data}")
Rails.logger.debug { "Sent the received client data to docker:#{data}" }
end
rescue JSON::ParserError
socket.send data
Rails.logger.debug("Rescued parsing error, sent the received client data to docker:#{data}")
Rails.logger.debug { "Rescued parsing error, sent the received client data to docker:#{data}" }
Sentry.set_extras(data: data)
end
end

View File

@ -44,10 +44,10 @@ class ProxyExercise < ApplicationRecord
def get_matching_exercise(user)
assigned_user_proxy_exercise = user_proxy_exercise_exercises.find_by(user: user)
if assigned_user_proxy_exercise
Rails.logger.debug("retrieved assigned exercise for user #{user.id}: Exercise #{assigned_user_proxy_exercise.exercise}")
Rails.logger.debug { "retrieved assigned exercise for user #{user.id}: Exercise #{assigned_user_proxy_exercise.exercise}" }
assigned_user_proxy_exercise.exercise
else
Rails.logger.debug("find new matching exercise for user #{user.id}")
Rails.logger.debug { "find new matching exercise for user #{user.id}" }
matching_exercise =
begin
find_matching_exercise(user)
@ -66,7 +66,7 @@ class ProxyExercise < ApplicationRecord
def find_matching_exercise(user)
exercises_user_has_accessed = user.submissions.where("cause IN ('submit','assess')").map(&:exercise).uniq.compact
tags_user_has_seen = exercises_user_has_accessed.map(&:tags).uniq.flatten
Rails.logger.debug("exercises_user_has_accessed #{exercises_user_has_accessed.map(&:id).join(',')}")
Rails.logger.debug { "exercises_user_has_accessed #{exercises_user_has_accessed.map(&:id).join(',')}" }
# find exercises
potential_recommended_exercises = []
@ -76,7 +76,7 @@ class ProxyExercise < ApplicationRecord
potential_recommended_exercises << ex
end
end
Rails.logger.debug("potential_recommended_exercises: #{potential_recommended_exercises.map(&:id)}")
Rails.logger.debug { "potential_recommended_exercises: #{potential_recommended_exercises.map(&:id)}" }
# if all exercises contain tags which the user has never seen, recommend easiest exercise
if potential_recommended_exercises.empty?
Rails.logger.debug('matched easiest exercise in pool')
@ -90,8 +90,8 @@ class ProxyExercise < ApplicationRecord
def select_best_matching_exercise(user, exercises_user_has_accessed, potential_recommended_exercises)
topic_knowledge_user_and_max = get_user_knowledge_and_max_knowledge(user, exercises_user_has_accessed)
Rails.logger.debug("topic_knowledge_user_and_max: #{topic_knowledge_user_and_max}")
Rails.logger.debug("potential_recommended_exercises: #{potential_recommended_exercises.size}: #{potential_recommended_exercises.map(&:id)}")
Rails.logger.debug { "topic_knowledge_user_and_max: #{topic_knowledge_user_and_max}" }
Rails.logger.debug { "potential_recommended_exercises: #{potential_recommended_exercises.size}: #{potential_recommended_exercises.map(&:id)}" }
topic_knowledge_user = topic_knowledge_user_and_max[:user_topic_knowledge]
topic_knowledge_max = topic_knowledge_user_and_max[:max_topic_knowledge]
current_users_knowledge_lack = {}
@ -103,7 +103,7 @@ class ProxyExercise < ApplicationRecord
potential_recommended_exercises.each do |potex|
tags = potex.tags
relative_knowledge_improvement[potex] = 0.0
Rails.logger.debug("review potential exercise #{potex.id}")
Rails.logger.debug { "review potential exercise #{potex.id}" }
tags.each do |tag|
tag_ratio = potex.exercise_tags.find_by(tag: tag).factor.to_f / potex.exercise_tags.inject(0) do |sum, et|
sum + et.factor
@ -111,7 +111,7 @@ class ProxyExercise < ApplicationRecord
max_topic_knowledge_ratio = potex.expected_difficulty * tag_ratio
old_relative_loss_tag = topic_knowledge_user[tag] / topic_knowledge_max[tag]
new_relative_loss_tag = topic_knowledge_user[tag] / (topic_knowledge_max[tag] + max_topic_knowledge_ratio)
Rails.logger.debug("tag #{tag} old_relative_loss_tag #{old_relative_loss_tag}, new_relative_loss_tag #{new_relative_loss_tag}, tag_ratio #{tag_ratio}")
Rails.logger.debug { "tag #{tag} old_relative_loss_tag #{old_relative_loss_tag}, new_relative_loss_tag #{new_relative_loss_tag}, tag_ratio #{tag_ratio}" }
relative_knowledge_improvement[potex] += old_relative_loss_tag - new_relative_loss_tag
end
end
@ -123,29 +123,31 @@ class ProxyExercise < ApplicationRecord
@reason[:current_users_knowledge_lack] = current_users_knowledge_lack
@reason[:relative_knowledge_improvement] = relative_knowledge_improvement
Rails.logger.debug("current users knowledge loss: #{current_users_knowledge_lack.map do |k, v|
"#{k} => #{v}"
end}")
Rails.logger.debug("relative improvements #{relative_knowledge_improvement.map {|k, v| "#{k.id}:#{v}" }}")
Rails.logger.debug do
"current users knowledge loss: #{current_users_knowledge_lack.map do |k, v|
"#{k} => #{v}"
end}"
end
Rails.logger.debug { "relative improvements #{relative_knowledge_improvement.map {|k, v| "#{k.id}:#{v}" }}" }
best_matching_exercise
end
private :select_best_matching_exercise
def find_best_exercise(relative_knowledge_improvement, highest_difficulty_user_has_accessed)
Rails.logger.debug("select most appropiate exercise for user. his highest difficulty was #{highest_difficulty_user_has_accessed}")
Rails.logger.debug { "select most appropiate exercise for user. his highest difficulty was #{highest_difficulty_user_has_accessed}" }
sorted_exercises = relative_knowledge_improvement.sort_by {|_k, v| v }.reverse
sorted_exercises.each do |ex, _diff|
Rails.logger.debug("review exercise #{ex.id} diff: #{ex.expected_difficulty}")
Rails.logger.debug { "review exercise #{ex.id} diff: #{ex.expected_difficulty}" }
if (ex.expected_difficulty - highest_difficulty_user_has_accessed) <= 1
Rails.logger.debug("matched exercise #{ex.id}")
Rails.logger.debug { "matched exercise #{ex.id}" }
return ex
else
Rails.logger.debug("exercise #{ex.id} is too difficult")
Rails.logger.debug { "exercise #{ex.id} is too difficult" }
end
end
easiest_exercise = sorted_exercises.min_by {|_k, v| v }.first
Rails.logger.debug("no match, select easiest exercise as fallback #{easiest_exercise.id}")
Rails.logger.debug { "no match, select easiest exercise as fallback #{easiest_exercise.id}" }
easiest_exercise
end
private :find_best_exercise
@ -169,12 +171,12 @@ class ProxyExercise < ApplicationRecord
def score(user, exercise)
max_score = exercise.maximum_score.to_f
if max_score <= 0
Rails.logger.debug("scoring user #{user.id} for exercise #{exercise.id}: score: 0")
Rails.logger.debug { "scoring user #{user.id} for exercise #{exercise.id}: score: 0" }
return 0.0
end
points_ratio = exercise.maximum_score(user) / max_score
if points_ratio.to_d == 0.0.to_d
Rails.logger.debug("scoring user #{user.id} for exercise #{exercise.id}: points_ratio=#{points_ratio} score: 0")
Rails.logger.debug { "scoring user #{user.id} for exercise #{exercise.id}: points_ratio=#{points_ratio} score: 0" }
return 0.0
elsif points_ratio > 1.0
points_ratio = 1.0 # The score of the exercise was adjusted and is now lower than it was
@ -189,11 +191,11 @@ class ProxyExercise < ApplicationRecord
break
end
end
Rails.logger.debug(
"scoring user #{user.id} exercise #{exercise.id}: worktime #{working_time_user}, points: #{points_ratio}" \
Rails.logger.debug do
"scoring user #{user.id} exercise #{exercise.id}: worktime #{working_time_user}, points: #{points_ratio}" \
"(index #{points_ratio_index}) quantiles #{quantiles_working_time} placed into quantile index #{quantile_index} " \
"score: #{scoring_matrix[points_ratio_index][quantile_index]}"
)
end
scoring_matrix[points_ratio_index][quantile_index]
end
private :score
@ -212,7 +214,7 @@ class ProxyExercise < ApplicationRecord
topic_knowledge_max = all_used_tags_with_count.keys.index_with {|_t| 0 }
exercises_sorted = exercises.sort_by {|ex| ex.time_maximum_score(user) }
exercises_sorted.each do |ex|
Rails.logger.debug("exercise: #{ex.id}: #{ex}")
Rails.logger.debug { "exercise: #{ex.id}: #{ex}" }
user_score_factor = score(user, ex)
ex.tags.each do |t|
tags_counter[t] += 1
@ -220,13 +222,15 @@ class ProxyExercise < ApplicationRecord
tag_ratio = ex.exercise_tags.find_by(tag: t).factor.to_f / ex.exercise_tags.inject(0) do |sum, et|
sum + et.factor
end
Rails.logger.debug("tag: #{t}, factor: #{ex.exercise_tags.find_by(tag: t).factor}, sumall: #{ex.exercise_tags.inject(0) do |sum, et|
sum + et.factor
end }")
Rails.logger.debug("tag #{t}, count #{tags_counter[t]}, max: #{all_used_tags_with_count[t]}, factor: #{tag_diminishing_return_factor}")
Rails.logger.debug("tag_ratio #{tag_ratio}")
Rails.logger.debug do
"tag: #{t}, factor: #{ex.exercise_tags.find_by(tag: t).factor}, sumall: #{ex.exercise_tags.inject(0) do |sum, et|
sum + et.factor
end }"
end
Rails.logger.debug { "tag #{t}, count #{tags_counter[t]}, max: #{all_used_tags_with_count[t]}, factor: #{tag_diminishing_return_factor}" }
Rails.logger.debug { "tag_ratio #{tag_ratio}" }
topic_knowledge_ratio = ex.expected_difficulty * tag_ratio
Rails.logger.debug("topic_knowledge_ratio #{topic_knowledge_ratio}")
Rails.logger.debug { "topic_knowledge_ratio #{topic_knowledge_ratio}" }
topic_knowledge_loss_user[t] += (1 - user_score_factor) * topic_knowledge_ratio * tag_diminishing_return_factor
topic_knowledge_max[t] += topic_knowledge_ratio * tag_diminishing_return_factor
end

View File

@ -91,7 +91,7 @@ class DockerClient
# Internally, Faye::WebSocket uses EventMachine and the ping value is used to wake the EventMachine thread
socket = Faye::WebSocket::Client.new(socket_url, [], headers: headers, ping: 0.1)
Rails.logger.debug "Opening Websocket on URL #{socket_url}"
Rails.logger.debug { "Opening Websocket on URL #{socket_url}" }
socket.on :error do |event|
Rails.logger.info "Websocket error: #{event.message}"
@ -350,7 +350,7 @@ container_execution_time: nil}
end
def exit_container(container)
Rails.logger.debug("exiting container #{container}")
Rails.logger.debug { "exiting container #{container}" }
# exit the timeout thread if it is still alive
exit_thread_if_alive
@socket.close
@ -459,7 +459,7 @@ container_execution_time: nil}
end
def self.return_container(container, execution_environment)
Rails.logger.debug("returning container #{container}")
Rails.logger.debug { "returning container #{container}" }
begin
clean_container_workspace(container)
rescue Docker::Error::NotFoundError => e

View File

@ -53,7 +53,7 @@ module Prometheus
end
def update_notification(object)
Rails.logger.debug("Prometheus metric updated for #{object.class.name}")
Rails.logger.debug { "Prometheus metric updated for #{object.class.name}" }
case object
when RequestForComment
@ -63,7 +63,7 @@ module Prometheus
def create_notification(object)
@instance_count.increment(class: object.class.name)
Rails.logger.debug("Prometheus instance count increased for #{object.class.name}")
Rails.logger.debug { "Prometheus instance count increased for #{object.class.name}" }
case object
when RequestForComment
@ -75,7 +75,7 @@ module Prometheus
def destroy_notification(object)
@instance_count.decrement(class: object.class.name)
Rails.logger.debug("Prometheus instance count decreased for #{object.class.name}")
Rails.logger.debug { "Prometheus instance count decreased for #{object.class.name}" }
case object
when Comment

View File

@ -59,6 +59,8 @@ describe ApplicationController do
context "with a 'locale' value in the session" do
it 'sets this locale' do
session[:locale] = locale
# The around block first sets the default language and then the language requested
expect(I18n).to receive(:locale=).with(I18n.default_locale)
expect(I18n).to receive(:locale=).with(locale)
get :welcome
end
@ -67,7 +69,7 @@ describe ApplicationController do
context "without a 'locale' value in the session" do
it 'sets the default locale' do
expect(session[:locale]).to be_blank
expect(I18n).to receive(:locale=).with(I18n.default_locale)
expect(I18n).to receive(:locale=).with(I18n.default_locale).at_least(:once)
get :welcome
end
end

View File

@ -33,8 +33,6 @@ describe SessionsController do
let(:exercise2) { FactoryBot.create(:dummy) }
let(:nonce) { SecureRandom.hex }
before { I18n.locale = I18n.default_locale }
context 'without OAuth parameters' do
it 'refuses the LTI launch' do
expect(controller).to receive(:refuse_lti_launch).with(message: I18n.t('sessions.oauth.missing_parameters')).and_call_original
@ -87,9 +85,12 @@ describe SessionsController do
end
it 'sets the specified locale' do
expect(controller).to receive(:set_locale).and_call_original
expect(controller).to receive(:switch_locale).and_call_original
i18n = instance_double 'i18n', locale: locale.to_s
allow(I18n).to receive(:locale=).with(I18n.default_locale).and_call_original
allow(I18n).to receive(:locale=).with(locale.to_s).and_return(i18n)
perform_request
expect(I18n.locale).to eq(locale)
expect(i18n.locale.to_sym).to eq(locale)
end
it 'assigns the exercise' do
@ -109,7 +110,8 @@ describe SessionsController do
end
context 'when LTI outcomes are supported' do
let(:message) { I18n.t('sessions.create_through_lti.session_with_outcome', consumer: consumer) }
# The expected message should be localized in the requested localization
let(:message) { I18n.t('sessions.create_through_lti.session_with_outcome', consumer: consumer, locale: locale) }
before do
allow(controller).to receive(:lti_outcome_service?).and_return(true)
@ -120,7 +122,8 @@ describe SessionsController do
end
context 'when LTI outcomes are not supported' do
let(:message) { I18n.t('sessions.create_through_lti.session_without_outcome', consumer: consumer) }
# The expected message should be localized in the requested localization
let(:message) { I18n.t('sessions.create_through_lti.session_without_outcome', consumer: consumer, locale: locale) }
before do
allow(controller).to receive(:lti_outcome_service?).and_return(false)