diff --git a/app/controllers/reasons_controller.rb b/app/controllers/reasons_controller.rb index c21c681..145cd4c 100644 --- a/app/controllers/reasons_controller.rb +++ b/app/controllers/reasons_controller.rb @@ -28,15 +28,17 @@ def create @reason.user_id = current_user.id @reason.pair_id = params[:pair_id] + respond_to do |format| if @reason.save - format.html { redirect_to @reason, notice: 'Reason was successfully created.' } + format.html { redirect_to replacements_path, notice: 'Reason was successfully created.' } format.json { render :show, status: :created, location: @reason } else format.html { render :new } format.json { render json: @reason.errors, status: :unprocessable_entity } end end + end # PATCH/PUT /reasons/1 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 340704d..eb6e68c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,5 @@ class UsersController < ApplicationController + before_action :set_user, only: [:show, :edit, :update, :destroy] # GET /users @@ -61,6 +62,16 @@ def destroy end end + def accept_pair + @current_user = User.last.username + @next_to_pair = User.first.username + redirect_to matrix_index_path + end + + def replacements + @users = User.all + end + private # Use callbacks to share common setup or constraints between actions. def set_user diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 93816e1..e085f75 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,6 +1,15 @@ +require 'pairings_helper' + class WelcomeController < ApplicationController + include PairingsHelper + def index - @user = User.first + + if current_user + @list = pair_me(current_user) + @next_pair = @list[0] + end + end diff --git a/app/helpers/pairings_helper.rb b/app/helpers/pairings_helper.rb new file mode 100644 index 0000000..2fe1dd7 --- /dev/null +++ b/app/helpers/pairings_helper.rb @@ -0,0 +1,49 @@ +module PairingsHelper + + # Return an array of potential partners in descending order of attractiveness + def pair_me(primary_user) + # Filter the available partners from the current cohort + # Next version should filter users already paired for today in pair history + available_users = unpaired_users_today(cohort_of_user(primary_user)) + + available_users = remove_user(available_users, primary_user) + + @list_of_users = available_users.shuffle + @list_of_users + end + + def save_entire_pair(users=[]) + users.each do |user| + pair_id = Pairing.maximum(:pair_id).to_i+1 + Pairing.create(pair_id: pair_id, user_id: user.id, cohort_id: user.cohort_id) + end + end + + def users_in_same_cohort_as(user) + return User.where(cohort_id: user.cohort_id).find_each.to_a + end + + def cohort_of_user(user) + return Cohort.where(id: user.cohort_id).first + end + + def remove_user(user_list, reject_user=nil) + return user_list.reject { |user| user == reject_user } + end + + def users_in_cohort(cohort) + return User.where(cohort_id: cohort.id).find_each.to_a + end + + def unpaired_users_today(cohort) + return users_in_cohort(cohort).reject do |user| + Pairing.where("created_at >= ? AND user_id = ?", Time.zone.now.beginning_of_day, user.id).first != nil + end + end + + # def assign_pairs(cohort_name) + # # this is a method used elsewhere + # # odd number should return a final pair of one person + # end + +end diff --git a/app/models/user.rb b/app/models/user.rb index a742acb..d381ea0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,7 @@ class User < ActiveRecord::Base def self.create_with_omniauth(auth) create! do |user| user.provider = auth.provider + user.cohort = Cohort.first user.uid = auth.uid user.name = auth.info.name user.profile_picture = auth.info.image diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 8931c7e..3018584 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -5,15 +5,17 @@
| Username | -Full name | -Profile picture | -Slack username | -- | Username | +Full name | +Profile picture | +Slack username | ++ + + <% @users.each do |user| %> |
|---|---|---|---|---|---|---|
| <%= user.username %> | @@ -29,3 +31,5 @@||||||
Username: - <%= @user.username %>
- - -Full name: - <%= @user.full_name %>
-Slack username: - <%= @user.slack_username %>
<%= link_to 'Edit', edit_user_path(@user) %>| - <%= link_to 'Back', users_path %> -Github Pairname
- -Github slack name
- - -
+