Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions app/controllers/races_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,9 @@ def stop_race
# Save race
def save_race
race = Race.find(params[:id])
racer_ids = race.results.pluck(:racer_id)
streak_racer_ids = Racer.where("current_streak > 0 OR longest_streak > 0").pluck(:id)
racer_ids_to_update = (racer_ids + streak_racer_ids).uniq

race_counts = Result.where(racer_id: racer_ids_to_update).group(:racer_id).count
latest_bibs_by_racer = {}
Result.joins(:race)
.where(racer_id: racer_ids_to_update)
.order("results.racer_id ASC, races.date DESC")
.pluck("results.racer_id", "results.bib")
.each do |racer_id, bib|
latest_bibs_by_racer[racer_id] ||= bib
end

Racer.where(id: racer_ids_to_update).find_each do |racer|
racer.update_columns(
race_count: race_counts[racer.id] || 0,
fav_bib: latest_bibs_by_racer[racer.id]
)
update_streak_calendar(racer)
end

race.update(state: 'FINISHED')
redirect_to race
SaveRaceJob.perform_later(race.id)
redirect_to race, notice: "Race save is running in the background."
end

# enable race.
Expand Down
29 changes: 29 additions & 0 deletions app/jobs/save_race_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class SaveRaceJob < ActiveJob::Base
queue_as :default

def perform(race_id)
race = Race.find(race_id)
racer_ids = race.results.pluck(:racer_id)
streak_racer_ids = Racer.where("current_streak > 0 OR longest_streak > 0").pluck(:id)
racer_ids_to_update = (racer_ids + streak_racer_ids).uniq

race_counts = Result.where(racer_id: racer_ids_to_update).group(:racer_id).count
latest_bibs_by_racer = {}
Result.joins(:race)
.where(racer_id: racer_ids_to_update)
.order("results.racer_id ASC, races.date DESC")
.pluck("results.racer_id", "results.bib")
.each do |racer_id, bib|
latest_bibs_by_racer[racer_id] ||= bib
end

Racer.where(id: racer_ids_to_update).find_each do |racer|
racer.update_columns(
race_count: race_counts[racer.id] || 0,
fav_bib: latest_bibs_by_racer[racer.id]
)
update_streak_calendar(racer)
end

end
end
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
module Blog
class Application < Rails::Application
config.load_defaults 6.1
config.active_job.queue_adapter = :async
end
end