-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create a table/model to store daily pairs of buddies * Fix RuboCop issues * WIP: job * Finish job * Add Buddy model * Wording * Update schema from main * Adjust cron timezones (with fugit) * Wording * Fix job * Adjust crons * Add @daily_buddy in page controller with sugar in Buddy model * Add the front for buddies: on top of calendar, link to Slack, fallback to profile * Never happy with crons * Fix RuboCop issues * Add explicit .present?
- Loading branch information
Showing
7 changed files
with
130 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module Buddies | ||
class GenerateDailyPairsJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(day) | ||
@day = day | ||
|
||
if Buddy.exists?(day:) | ||
Rails.logger.info "Daily pairs already exist for day #{day}" | ||
return | ||
end | ||
|
||
retrieve_confirmed_users | ||
handle_odd_number_of_users | ||
generate_possible_pairs_of_buddies | ||
|
||
@possible_pairs.shuffle! | ||
|
||
pick_valid_pairs_of_buddies | ||
handle_unpaired_users | ||
|
||
insert_generated_buddies | ||
end | ||
|
||
private | ||
|
||
def retrieve_confirmed_users | ||
@user_ids = User.confirmed.order(:id).pluck(:id) | ||
end | ||
|
||
def handle_odd_number_of_users | ||
@user_ids.pop if @user_ids.size.odd? | ||
end | ||
|
||
def generate_possible_pairs_of_buddies | ||
all_pairs = @user_ids.combination(2).to_set | ||
past_buddies = Buddy.pluck(:id_1, :id_2).to_set | ||
|
||
@possible_pairs = (all_pairs - past_buddies).to_a | ||
end | ||
|
||
def pick_valid_pairs_of_buddies | ||
@users_to_match = Set.new(@user_ids) | ||
@buddies = [] | ||
|
||
# Iterate once over possible pairs to find matches | ||
@possible_pairs.each do |pair| | ||
# If a pair contains two available IDs, it's a match! | ||
if pair.all? { |id| @users_to_match.include?(id) } | ||
@buddies << pair | ||
pair.each { |id| @users_to_match.delete(id) } | ||
end | ||
end | ||
end | ||
|
||
def handle_unpaired_users | ||
@buddies += @users_to_match.to_a.shuffle.each_slice(2).to_a | ||
end | ||
|
||
def insert_generated_buddies | ||
Buddy.insert_all!(@buddies.map { |a, b| { day: @day, id_1: a, id_2: b } }) | ||
Rails.logger.info "Buddies successfully generated for day #{@day}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class Buddy < ApplicationRecord | ||
scope :of_today, -> { where(day: Aoc.latest_day) } | ||
scope :of_user, ->(user_id) { where("id_1 = ? OR id_2 = ?", user_id, user_id) } | ||
|
||
def self.of_the_day(user) | ||
buddy_pair = of_today.of_user(user.id).first | ||
daily_buddy_id = user.id == buddy_pair.id_1 ? buddy_pair.id_2 : buddy_pair.id_1 if buddy_pair | ||
|
||
User.find_by(id: daily_buddy_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateBuddies < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :buddies do |t| | ||
t.integer :id_1 | ||
t.integer :id_2 | ||
t.integer :day | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters