Skip to content

Commit 7c259e3

Browse files
committed
Code style
1 parent 72b20c1 commit 7c259e3

32 files changed

+222
-273
lines changed

lib/background_course_refresher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def do_refresh
99
course.initial_refresh_ready = true
1010
course.save!
1111
Rails.logger.info "Finished background refresh on course id #{course.id}"
12-
rescue => e
12+
rescue StandardError => e
1313
Rails.logger.warn "Background refresh on course id #{course.id} failed!. Error #{e}"
1414
end
1515
end

lib/course_info.rb

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def course_data(organization, course, opts = {})
1313
exercises = course.exercises.includes(:course, :available_points).to_a.natsort_by(&:name)
1414

1515
@unlocked_exercises = course.unlocks
16-
.where(user_id: @user.id)
17-
.where(['valid_after IS NULL OR valid_after < ?', Time.now])
18-
.pluck(:exercise_name)
16+
.where(user_id: @user.id)
17+
.where(['valid_after IS NULL OR valid_after < ?', Time.now])
18+
.pluck(:exercise_name)
1919

2020
submissions_by_exercise = {}
2121
Submission.where(course_id: course.id, user_id: @user.id).each do |sub|
@@ -27,25 +27,25 @@ def course_data(organization, course, opts = {})
2727
end
2828

2929
@course_list.course_data(organization, course, opts).merge(unlockables: course.unlockable_exercises_for(@user).map(&:name).natsort,
30-
exercises: exercises.map { |ex| exercise_data(ex) }.reject(&:nil?))
30+
exercises: exercises.map { |ex| exercise_data(ex) }.reject(&:nil?))
3131
end
3232

3333
def course_data_core_api(course)
3434
UncomputedUnlock.resolve(course, @user)
3535
@unlocked_exercises = course.unlocks
36-
.where(user_id: @user.id)
37-
.where(['valid_after IS NULL OR valid_after < ?', Time.now])
38-
.pluck(:exercise_name)
36+
.where(user_id: @user.id)
37+
.where(['valid_after IS NULL OR valid_after < ?', Time.now])
38+
.pluck(:exercise_name)
3939

4040
exercises = course.exercises.includes(:course, :available_points)
4141

4242
unless @user.administrator? || @user.teacher?(course.organization) || @user.assistant?(course)
4343
exercises = exercises.where(hidden: false, disabled_status: 0)
4444
exercises = if @unlocked_exercises.empty?
45-
exercises.where(unlock_spec: nil)
46-
else
47-
exercises.where(["unlock_spec IS NULL OR exercises.name IN (#{@unlocked_exercises.map {|_| '?'}.join(', ')})", *@unlocked_exercises])
48-
end.select { |e| e._fast_visible_to?(@user)}
45+
exercises.where(unlock_spec: nil)
46+
else
47+
exercises.where(["unlock_spec IS NULL OR exercises.name IN (#{@unlocked_exercises.map { |_| '?' }.join(', ')})", *@unlocked_exercises])
48+
end.select { |e| e._fast_visible_to?(@user) }
4949
end
5050

5151
exercises = exercises.to_a.natsort_by(&:name)
@@ -94,7 +94,7 @@ def exercise_data(exercise)
9494
runtime_params: exercise.runtime_params_array,
9595
valgrind_strategy: exercise.valgrind_strategy,
9696
code_review_requests_enabled: exercise.code_review_requests_enabled?,
97-
run_tests_locally_action_enabled: exercise.run_tests_locally_action_enabled?,
97+
run_tests_locally_action_enabled: exercise.run_tests_locally_action_enabled?
9898
}
9999

100100
data[:solution_zip_url] = @helpers.exercise_solution_zip_url(exercise) if @user.administrator?
@@ -112,25 +112,25 @@ def exercise_data_core_api(exercise)
112112
locked = exercise.requires_unlock? && !@unlocked_exercises.include?(exercise.name)
113113

114114
data = {
115-
id: exercise.id,
116-
name: exercise.name,
117-
locked: locked,
118-
deadline_description: exercise.deadline_spec_obj.universal_description,
119-
deadline: exercise.deadline_for(@user),
120-
checksum: exercise.checksum,
121-
return_url: @helpers.api_v8_core_exercise_submissions_url(exercise),
122-
zip_url: @helpers.download_api_v8_core_exercise_url(exercise),
123-
returnable: exercise.returnable?,
124-
requires_review: exercise.requires_review?,
125-
attempted: exercise.attempted_by?(@user),
126-
completed: exercise.completed_by?(@user),
127-
reviewed: exercise.reviewed_for?(@user),
128-
all_review_points_given: exercise.all_review_points_given_for?(@user),
129-
memory_limit: exercise.memory_limit,
130-
runtime_params: exercise.runtime_params_array,
131-
valgrind_strategy: exercise.valgrind_strategy,
132-
code_review_requests_enabled: exercise.code_review_requests_enabled?,
133-
run_tests_locally_action_enabled: exercise.run_tests_locally_action_enabled?,
115+
id: exercise.id,
116+
name: exercise.name,
117+
locked: locked,
118+
deadline_description: exercise.deadline_spec_obj.universal_description,
119+
deadline: exercise.deadline_for(@user),
120+
checksum: exercise.checksum,
121+
return_url: @helpers.api_v8_core_exercise_submissions_url(exercise),
122+
zip_url: @helpers.download_api_v8_core_exercise_url(exercise),
123+
returnable: exercise.returnable?,
124+
requires_review: exercise.requires_review?,
125+
attempted: exercise.attempted_by?(@user),
126+
completed: exercise.completed_by?(@user),
127+
reviewed: exercise.reviewed_for?(@user),
128+
all_review_points_given: exercise.all_review_points_given_for?(@user),
129+
memory_limit: exercise.memory_limit,
130+
runtime_params: exercise.runtime_params_array,
131+
valgrind_strategy: exercise.valgrind_strategy,
132+
code_review_requests_enabled: exercise.code_review_requests_enabled?,
133+
run_tests_locally_action_enabled: exercise.run_tests_locally_action_enabled?
134134
}
135135

136136
data[:solution_zip_url] = @helpers.download_api_v8_core_exercise_solution_url(exercise) if @user.administrator?
@@ -147,7 +147,7 @@ def sheets
147147
end
148148

149149
def exercise_return_url(e)
150-
"#{@helpers.exercise_submissions_url(e, format: 'json')}"
150+
@helpers.exercise_submissions_url(e, format: 'json').to_s
151151
end
152152

153153
def get_latest_submission(exercise)

lib/course_list.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ def initialize(user, helpers)
55
@helpers = helpers
66
end
77

8-
def course_list_data(organization, courses, opts={})
8+
def course_list_data(organization, courses, opts = {})
99
courses.map { |c| course_data(organization, c, opts) }
1010
end
1111

1212
def course_list_data_no_organisation(courses, opts = {})
1313
courses.map { |c| course_data(c.organization, c, opts) }
1414
end
1515

16-
def course_data(organization, course, opts={})
16+
def course_data(organization, course, opts = {})
1717
@course = course
1818
data = {
1919
id: course.id,
@@ -23,7 +23,7 @@ def course_data(organization, course, opts={})
2323
unlock_url: @helpers.organization_course_unlock_url(organization, course, format: :json),
2424
reviews_url: @helpers.organization_course_reviews_url(organization, course, format: :json),
2525
comet_url: CometServer.get.client_url,
26-
spyware_urls: SiteSetting.value('spyware_servers'),
26+
spyware_urls: SiteSetting.value('spyware_servers')
2727
}
2828

2929
if opts[:include_points]
@@ -34,7 +34,7 @@ def course_data(organization, course, opts={})
3434
total_available: AvailablePoint.course_sheet_points(course, sheet)
3535
}
3636
end,
37-
total_available: AvailablePoint.course_points_of_exercises(course, exercises),
37+
total_available: AvailablePoint.course_points_of_exercises(course, exercises)
3838
}
3939
end
4040

@@ -43,16 +43,17 @@ def course_data(organization, course, opts={})
4343
exercises.map do |ex|
4444
{
4545
name: ex.name,
46-
conditions: JSON.parse(ex.unlock_spec),
46+
conditions: JSON.parse(ex.unlock_spec)
4747
}
4848
end
4949
]
5050
end
5151

52-
data
52+
data
5353
end
5454

5555
private
56+
5657
def exercises
5758
@exercises ||= @course.exercises.select { |e| e.points_visible_to?(@user) }
5859
end

lib/course_refresher.rb

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ def refresh_course(course, options)
128128
@course.save!
129129
@course.exercises.each &:save!
130130

131-
CourseRefresher.simulate_failure! if ::Rails.env == 'test' && CourseRefresher.respond_to?('simulate_failure!')
131+
CourseRefresher.simulate_failure! if ::Rails.env.test? && CourseRefresher.respond_to?('simulate_failure!')
132132
rescue StandardError, ScriptError # Some YAML parsers throw ScriptError on syntax errors
133133
@report.errors << $!.message + "\n" + $!.backtrace.join("\n")
134134
# Delete the new cache we were working on
135-
FileUtils.rm_rf(@course.cache_path) unless options[:no_directory_changes]
135+
FileUtils.rm_rf(@course.cache_path) unless options[:no_directory_changes]
136136
raise ActiveRecord::Rollback
137137
end
138138
end
@@ -143,18 +143,18 @@ def refresh_course(course, options)
143143
end
144144

145145
course.reload # reload the record given as parameter
146-
fail Failure.new(@report) unless @report.errors.empty?
146+
raise Failure, @report unless @report.errors.empty?
147147
@report
148148
end
149149

150150
def update_or_clone_repository
151-
fail 'Source types other than git not yet implemented' if @course.source_backend != 'git'
151+
raise 'Source types other than git not yet implemented' if @course.source_backend != 'git'
152152

153153
if File.exist?("#{@old_cache_path}/clone/.git")
154154
begin
155155
# Try a fast path: copy old clone and git fetch new stuff
156156
copy_and_update_repository
157-
rescue
157+
rescue StandardError
158158
FileUtils.rm_rf(@course.clone_path)
159159
clone_repository
160160
end
@@ -164,7 +164,7 @@ def update_or_clone_repository
164164
end
165165

166166
def copy_and_update_repository
167-
FileUtils.cp_r("#{@old_cache_path}/clone", "#{@course.clone_path}")
167+
FileUtils.cp_r("#{@old_cache_path}/clone", @course.clone_path.to_s)
168168
Dir.chdir(@course.clone_path) do
169169
sh!('git', 'remote', 'set-url', 'origin', @course.source_url)
170170
sh!('git', 'fetch', 'origin')
@@ -184,7 +184,7 @@ def check_directory_names
184184
Find.find(@course.clone_path) do |path|
185185
relpath = path[@course.clone_path.length..-1]
186186
if File.directory?(path) && exdirs.any? { |exdir| exdir.start_with?(path) } && relpath.include?('-')
187-
fail "The directory #{path} contains a dash (-). Currently that is forbidden. Sorry."
187+
raise "The directory #{path} contains a dash (-). Currently that is forbidden. Sorry."
188188
end
189189
end
190190
end
@@ -248,9 +248,7 @@ def update_exercise_options
248248

249249
e.options = metadata
250250

251-
if (e.new_record? && e.course.refreshed?)
252-
e.disabled!
253-
end
251+
e.disabled! if e.new_record? && e.course.refreshed?
254252

255253
e.save!
256254
rescue SyntaxError
@@ -290,23 +288,22 @@ def update_available_points(no_directory_changes = false)
290288
clone_path = Pathname("#{@course.clone_path}/#{exercise.relative_path}")
291289

292290
points_data = points_for(exercise, no_directory_changes)
293-
if points_data[0].is_a? Hash
294-
point_names += points_data.map { |x| x[:points] }.flatten
295-
else
296-
point_names += points_data.flatten
297-
end
291+
point_names += if points_data[0].is_a? Hash
292+
points_data.map { |x| x[:points] }.flatten
293+
else
294+
points_data.flatten
295+
end
298296

299297
point_names += review_points
300298

301299
added = []
302300
removed = []
303301

304302
point_names.each do |name|
305-
if exercise.available_points.none? { |point| point.name == name }
306-
added << name
307-
point = AvailablePoint.create(name: name, exercise: exercise)
308-
exercise.available_points << point
309-
end
303+
next unless exercise.available_points.none? { |point| point.name == name }
304+
added << name
305+
point = AvailablePoint.create(name: name, exercise: exercise)
306+
exercise.available_points << point
310307
end
311308

312309
exercise.available_points.to_a.clone.each do |point|
@@ -334,19 +331,19 @@ def get_c_exercise_points(exercise)
334331
TestScannerCache.get_or_update(@course, exercise.name, hash) do
335332
all_points = Set.new
336333
Dir.chdir(full_path) do
337-
sh!(%w(make test))
338-
sh!(%w(make get-points > points.txt), {escape: false})
334+
sh!(%w[make test])
335+
sh!(%w[make get-points > points.txt], escape: false)
339336

340337
tmc_available_points = File.join(full_path, 'test', 'tmc_available_points.txt')
341-
if File.exists? tmc_available_points
338+
if File.exist? tmc_available_points
342339
IO.readlines(tmc_available_points).map(&:strip).each do |line|
343340
if line =~ /\[.*\] \[.*\] (.*)/
344-
$1.split(' ').map(&:strip).each { |p| all_points << p}
341+
Regexp.last_match(1).split(' ').map(&:strip).each { |p| all_points << p }
345342
else
346343
raise "Warning: weird line in available points file: #{line}"
347344
end
348345
end
349-
elsif File.exists?(File.join(full_path, 'points.txt'))
346+
elsif File.exist?(File.join(full_path, 'points.txt'))
350347
available_points_content = IO.readlines("#{full_path}/points.txt")
351348
# drop makefile output
352349
# This is how initial check tests used to work.
@@ -360,8 +357,8 @@ def get_c_exercise_points(exercise)
360357
raise "Could not extract points for makefile exercise: #{exercise}"
361358
end
362359

363-
sh!(%w(make clean))
364-
FileUtils.rm("#{full_path}/points.txt") if File.exists?(File.join(full_path, 'points.txt'))
360+
sh!(%w[make clean])
361+
FileUtils.rm("#{full_path}/points.txt") if File.exist?(File.join(full_path, 'points.txt'))
365362
end
366363
all_points
367364
end
@@ -461,12 +458,12 @@ def set_permissions
461458
parent_dirs = Course.cache_root.sub(::Rails.root.to_s, '').split('/').reject(&:blank?)
462459
(0..(parent_dirs.length)).each do |i|
463460
dir = "#{::Rails.root}/#{parent_dirs[0..i].join('/')}"
464-
sh!('chmod', chmod, dir) unless chmod.blank?
465-
sh!('chgrp', chgrp, dir) unless chgrp.blank?
461+
sh!('chmod', chmod, dir) if chmod.present?
462+
sh!('chgrp', chgrp, dir) if chgrp.present?
466463
end
467464

468-
sh!('chmod', '-R', chmod, @course.cache_path) unless chmod.blank?
469-
sh!('chgrp', '-R', chgrp, @course.cache_path) unless chgrp.blank?
465+
sh!('chmod', '-R', chmod, @course.cache_path) if chmod.present?
466+
sh!('chgrp', '-R', chgrp, @course.cache_path) if chgrp.present?
470467
end
471468

472469
def invalidate_unlocks

lib/course_refresher/block_comment_based_filter.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class CourseRefresher
44
class BlockCommentBasedFilter # Abstract
55
def applies_to?(_file_path)
6-
fail 'abstract method'
6+
raise 'abstract method'
77
end
88

99
def filter_for_stub(text)
@@ -52,8 +52,8 @@ def remove_solution_blocks(text)
5252

5353
def uncomment_stubs(text)
5454
text.gsub(stub_regexp) do
55-
before = $1
56-
after = $2
55+
before = Regexp.last_match(1)
56+
after = Regexp.last_match(2)
5757
before + after
5858
end
5959
end
@@ -82,11 +82,11 @@ def solution_file_regexp
8282
end
8383

8484
def comment_begin
85-
fail 'abstract method'
85+
raise 'abstract method'
8686
end
8787

8888
def comment_end
89-
fail 'abstract method'
89+
raise 'abstract method'
9090
end
9191

9292
def resc(s)

lib/course_refresher/java_filter.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,12 @@ def applies_to?(file_path)
99

1010
def filter_for_stub(text)
1111
text = super(text)
12-
if text
13-
remove_html_comments(text)
14-
else
15-
nil
16-
end
12+
remove_html_comments(text) if text
1713
end
1814

1915
def filter_for_solution(text)
2016
text = super(text)
21-
if text
22-
remove_html_comments(text)
23-
else
24-
nil
25-
end
17+
remove_html_comments(text) if text
2618
end
2719

2820
def remove_html_comments(text)

0 commit comments

Comments
 (0)