Move completions methods to a concern
This commit is contained in:
parent
5525cc814a
commit
7ce684ced9
30
app/controllers/concerns/completions_concern.rb
Normal file
30
app/controllers/concerns/completions_concern.rb
Normal file
@ -0,0 +1,30 @@
|
||||
module CompletionsConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def pad(n)
|
||||
if n > 9
|
||||
return n.to_s
|
||||
end
|
||||
"0" + n.to_s
|
||||
end
|
||||
|
||||
def display_time(seconds)
|
||||
if seconds > 3600
|
||||
hours = seconds / 3600
|
||||
return hours.to_s + ":" + display_time(seconds % 3600)
|
||||
elsif seconds > 60
|
||||
minutes = seconds / 60
|
||||
return pad(minutes) + ":" + display_time(seconds % 60)
|
||||
end
|
||||
pad(seconds)
|
||||
end
|
||||
|
||||
def extend_completions!(completions)
|
||||
current_time_from_start = 0
|
||||
@completions.each do |completion|
|
||||
completion.display_time_from_start = display_time(completion.time_seconds)
|
||||
completion.display_relative_time = display_time(completion.time_seconds - current_time_from_start)
|
||||
current_time_from_start += completion.time_seconds
|
||||
end
|
||||
end
|
||||
end
|
@ -1,4 +1,6 @@
|
||||
class ContestantsController < ApplicationController
|
||||
include CompletionsConcern
|
||||
|
||||
before_action :set_contest
|
||||
before_action :set_contestant, only: %i[ destroy edit update]
|
||||
before_action :set_completions, only: %i[edit update ]
|
||||
@ -57,32 +59,10 @@ class ContestantsController < ApplicationController
|
||||
@contestant = Contestant.find(params[:id])
|
||||
end
|
||||
|
||||
def pad(n)
|
||||
if n > 9
|
||||
return n.to_s
|
||||
end
|
||||
"0" + n.to_s
|
||||
end
|
||||
|
||||
def display_time(seconds)
|
||||
if seconds > 3600
|
||||
hours = seconds / 3600
|
||||
return hours.to_s + ":" + display_time(seconds % 3600)
|
||||
elsif seconds > 60
|
||||
minutes = seconds / 60
|
||||
return pad(minutes) + ":" + display_time(seconds % 60)
|
||||
end
|
||||
pad(seconds)
|
||||
end
|
||||
|
||||
def set_completions
|
||||
@completions = @contestant.completions.order(:time_seconds)
|
||||
current_time_from_start = 0
|
||||
@completions.each do |completion|
|
||||
completion.display_time_from_start = display_time(completion.time_seconds)
|
||||
completion.display_relative_time = display_time(completion.time_seconds - current_time_from_start)
|
||||
current_time_from_start += completion.time_seconds
|
||||
end
|
||||
extend_completions!(@completions)
|
||||
@completions
|
||||
end
|
||||
|
||||
def contestant_params
|
||||
|
@ -1,4 +1,6 @@
|
||||
class ContestsController < ApplicationController
|
||||
include CompletionsConcern
|
||||
|
||||
before_action :set_contest, only: %i[ destroy edit show update ]
|
||||
skip_before_action :require_authentication, only: %i[ scoreboard ]
|
||||
|
||||
@ -68,6 +70,7 @@ class ContestsController < ApplicationController
|
||||
@title = @contest.name
|
||||
@contestants = @contest.contestants.order(:name)
|
||||
@puzzles = @contest.puzzles.order(:id)
|
||||
extend_completions!(@contest.completions)
|
||||
render :scoreboard
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user