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 @@ - - - - - - + + + + + + + + <% @users.each do |user| %> @@ -29,3 +31,5 @@
UsernameFull nameProfile pictureSlack username
UsernameFull nameProfile pictureSlack username
<%= user.username %>

+ +<%= link_to 'New User', new_user_path %> diff --git a/app/views/users/matrix.html.erb b/app/views/users/matrix.html.erb new file mode 100644 index 0000000..320bb8f --- /dev/null +++ b/app/views/users/matrix.html.erb @@ -0,0 +1,4 @@ +
+<%= "#{@current_user} and #{@next_to_pair}" %> +
+<%= "Congratulations on pairing." %> \ No newline at end of file diff --git a/app/views/users/replacements.html.erb b/app/views/users/replacements.html.erb new file mode 100644 index 0000000..4dec159 --- /dev/null +++ b/app/views/users/replacements.html.erb @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/app/views/welcome/index.html.erb b/app/views/welcome/index.html.erb index 68fbc69..d988123 100644 --- a/app/views/welcome/index.html.erb +++ b/app/views/welcome/index.html.erb @@ -18,80 +18,52 @@ -
-

Pairmiscuous

- -

"Not Pairing makes Pandas sad"


-
- -
- <% unless current_user %> - -
-
-
-
-
- - <%=link_to "Sign in with Github", "/auth/github"%> <% else %> -
- -
-
- - -
    -
  • -

    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

- -

-

- Yes No (submit - reason)
- - -
-
- -
-

- <% end %> -
-
+ + +
+
+

+ Pairmiscuous +

+

"Not Pairing makes Pandas sad"

+ +
+ +
+ <% unless current_user %> +
+
+ +
+
+
+ + +
+ + + <%=link_to "Sign in with Github", "/auth/github"%> + + + + <% else %> + <%= "You are paired with #{@next_pair.username}" %> + + <%= link_to 'Accept', accept_pair_path %> + <%= link_to 'Reject (say reason)', new_reason_path %> + + <% end %> +
+ + + + + + + + + + + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index dfa8556..a2af938 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,8 @@ get "/login" => 'sessions#new' get "/auth/:provider/callback" => "sessions#create" get "/signout" => "sessions#destroy", :as => :signout + get "/accept_pair" => 'users#accept_pair' + get "/replacements" => 'users#replacements' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 3cb5e70..e2a56c2 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "covered_percent": 100.0 + "covered_percent": 72.89 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 976bd3d..ea444a6 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -1,7 +1,7 @@ { "RSpec": { "coverage": { - "/Users/rocco/makers/week9/pairmiscuous/spec/dummy_test_spec.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/spec/dummy_test_spec.rb": [ 1, null, 1, @@ -12,7 +12,7 @@ null, null ], - "/Users/rocco/makers/week9/pairmiscuous/spec/rails_helper.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/spec/rails_helper.rb": [ null, 1, 1, @@ -66,14 +66,14 @@ 1, null ], - "/Users/rocco/makers/week9/pairmiscuous/config/environment.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/environment.rb": [ null, 1, null, null, 1 ], - "/Users/rocco/makers/week9/pairmiscuous/config/application.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/application.rb": [ 1, null, 1, @@ -110,12 +110,12 @@ null, null ], - "/Users/rocco/makers/week9/pairmiscuous/config/boot.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/boot.rb": [ 1, null, 1 ], - "/Users/rocco/makers/week9/pairmiscuous/config/environments/test.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/environments/test.rb": [ 1, null, null, @@ -159,7 +159,7 @@ null, null ], - "/Users/rocco/makers/week9/pairmiscuous/config/initializers/assets.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/initializers/assets.rb": [ null, null, null, @@ -172,7 +172,7 @@ null, null ], - "/Users/rocco/makers/week9/pairmiscuous/config/initializers/backtrace_silencers.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/initializers/backtrace_silencers.rb": [ null, null, null, @@ -181,18 +181,18 @@ null, null ], - "/Users/rocco/makers/week9/pairmiscuous/config/initializers/cookies_serializer.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/initializers/cookies_serializer.rb": [ null, null, 1 ], - "/Users/rocco/makers/week9/pairmiscuous/config/initializers/filter_parameter_logging.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/initializers/filter_parameter_logging.rb": [ null, null, null, 1 ], - "/Users/rocco/makers/week9/pairmiscuous/config/initializers/inflections.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/initializers/inflections.rb": [ null, null, null, @@ -210,26 +210,101 @@ null, null ], - "/Users/rocco/makers/week9/pairmiscuous/config/initializers/mime_types.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/initializers/mime_types.rb": [ null, null, null, null ], - "/Users/rocco/makers/week9/pairmiscuous/config/initializers/session_store.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/initializers/omniauth.rb": [ + 1, + 1, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/initializers/session_store.rb": [ null, null, 1 ], - "/Users/rocco/makers/week9/pairmiscuous/config/initializers/wrap_parameters.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/initializers/wrap_parameters.rb": [ + null, null, null, null, null, null, + 1, + 1, + null, + null, + null, + null, + null, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/config/routes.rb": [ + 1, + null, + 1, + 1, + 1, null, 1, 1, + 1, + 1, + 1, + 1, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, + null, null, null, null, @@ -237,55 +312,126 @@ null, null ], - "/Users/rocco/makers/week9/pairmiscuous/config/routes.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/helpers/application_helper.rb": [ 1, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/helpers/matrix_helper.rb": [ 1, null, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, null, null, null, + 0, null, null, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, null, null, null, null, + 0, null, null, null, null, + 1, + 0, + 0, + 0, + 0, + 0, + 0, null, + 0, null, null, + 1, + 0, + 0, null, + 0, + 0, + 0, null, null, + 0, null, null, + 1, + 0, + 0, null, + 0, + 0, + 0, + 0, + 0, null, null, + 0, null, null, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/helpers/pairings_helper.rb": [ + 1, null, null, + 1, null, null, null, + 3, + 3, null, + 3, null, null, null, + 1, + 18, + 30, + 30, null, null, null, + 1, + 0, null, null, + 1, + 3, null, null, + 1, + 35, null, null, + 1, + 22, + null, null, + 1, + 22, + 422, null, null, null, @@ -296,15 +442,285 @@ null, null ], - "/Users/rocco/makers/week9/pairmiscuous/app/helpers/application_helper.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/helpers/reasons_helper.rb": [ 1, null ], - "/Users/rocco/makers/week9/pairmiscuous/app/helpers/users_helper.rb": [ + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/helpers/sessions_helper.rb": [ + 1, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/helpers/users_helper.rb": [ + 1, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/helpers/welcome_helper.rb": [ + 1, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/spec/feature/user_feature_spec.rb": [ + 1, + null, + null, + 1, + null, + 1, + 3, + 3, + 3, + 3, + null, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + 1, + 0, + 0, + null, + null, + 1, + 1, + 1, + 0, + null, + null, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + null, + null, + null, + 1, + 2, + 2, + 2, + 2, + 2, + null, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/spec/pair_selection_spec.rb": [ + 1, + 1, + 1, + 1, + null, + 1, + null, + 1, + 4, + 4, + 66, + null, + null, + 1, + 3, + 18, + null, + null, + null, + 1, + 1, + 1, + 1, + null, + null, + 1, + 1, + 1, + 1, + 20, + 1, + null, + null, + 1, + 1, + 1, + 1, + null, + null, + 1, + 1, + 1, + 1, + 1, + 1, + null, + null, + null, + null, + 1, + null, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/spec/reason_spec.rb": [ + 1, + null, + 1, + 1, + 1, + 1, + 1, + 1, + null, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/models/reason.rb": [ + 1, + 1, + 1, + 1, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/models/user.rb": [ + 1, + 1, + 1, + 1, + 1, + null, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + null, + null, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/controllers/welcome_controller.rb": [ + 1, + 1, + null, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/controllers/application_controller.rb": [ + 1, + null, + null, + 1, + 1, + null, + 1, + null, + 1, + 10, + null, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/controllers/users_controller.rb": [ + 1, + 1, + null, + null, + null, + 1, + 0, + 0, + null, + null, + null, + null, + 1, + null, + null, + null, + 1, + 2, + null, + null, + null, + 1, + null, + null, + null, + null, + 1, + 2, + null, + 2, + 2, + 4, + 2, + null, + 0, + 0, + null, + null, + null, + null, + null, + null, + 1, + 0, + 0, + 0, + 0, + null, + 0, + 0, + null, + null, + null, + null, + null, + null, + 1, + 0, + 0, + 0, + 0, + null, + null, + null, + 1, + 0, + 0, + 0, + null, + null, + 1, + 0, + null, + null, + 1, + null, + 1, + 2, + null, + null, + null, + 1, + 2, + null, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/models/cohort.rb": [ + 1, + null, + 1, + 1, + null + ], + "/Users/makerslaptop66/Desktop/Projects/pairmiscuous/app/models/pairing.rb": [ + 1, + null, 1, null ] }, - "timestamp": 1435306949 + "timestamp": 1435328125 } } diff --git a/db/seeds.rb b/db/seeds.rb index 2df74c0..4da1a2f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -42,4 +42,4 @@ def assign_pairs(cohort_name) end end -7.times { assign_pairs('#april2015') } +# 7.times { assign_pairs('#april2015') } diff --git a/spec/feature/user_feature_spec.rb b/spec/feature/user_feature_spec.rb new file mode 100644 index 0000000..0158e8b --- /dev/null +++ b/spec/feature/user_feature_spec.rb @@ -0,0 +1,49 @@ +require 'rails_helper' + + +feature 'As a Pairmiscuous user' do + + before(:each) do + User.create(username: "Molly") + User.create(username: "Daryl") + User.create(username: "Tim") + User.create(username: 'Chidu') + end + + scenario 'pair is shown' do + visit '/' + expect(page).to have_content("You are paired with Molly"); + end + + scenario 'accepting a pair' do + sign_up('Rocco', 'rocco') + click_link('Accept') + expect(page).to have_content('Rocco and Molly') + expect(page).to have_content('Congratulations on pairing.') + end + + scenario 'rejecting a pair' do + sign_up('Rocco', 'rocco') + click_link('Reject') + expect(page).to have_content('Daryl Tim Chidu') + end + + xscenario 'no pairs available' do + Pairing.create(pair_id: 1, user_id: 1) + Pairing.create(pair_id: 1, user_id: 2) + Pairing.create(pair_id: 2, user_id: 3) + Pairing.create(pair_id: 2, user_id: 4) + sign_up('Rocco', 'rocco') + click_link('Back') + expect(page).to have_content('Please speak to the coaches to find a pair') + end + + + def sign_up(username, name) + visit new_user_path + fill_in('Full name', with: name) + fill_in('Username', with: username) + click_button('Create User') + visit '/' + end +end diff --git a/spec/pair_selection_spec.rb b/spec/pair_selection_spec.rb new file mode 100644 index 0000000..bb2b1e6 --- /dev/null +++ b/spec/pair_selection_spec.rb @@ -0,0 +1,52 @@ +require 'rails_helper' +require './app/helpers/pairings_helper' +include PairingsHelper +require 'byebug' + +describe 'Create a pair' do + + def create_test_cohort(cohort_size) + @cohort = Cohort.create() + @user = [] + cohort_size.times {@user << @cohort.users.create()} + end + + def create_pair_history(cohort,number_pairs,date=Date.today) + number_pairs.times do + save_entire_pair(unpaired_users_today(cohort)[0..1]) + end + end + + it 'should return a cohort pair partner' do + create_test_cohort(2) + result = pair_me(@user[0]) + expect(result).to eq([@user[1]]) + end + + it 'should return all cohort partners excluding the user being matched' do + create_test_cohort(20) + result = pair_me(@user[0]).sort + expect(result.length).to eq(19) + @user[1..19].each { |use| expect(result).to include(use) } + expect(result).not_to include(@user[0]) + end + + it 'should return all cohort partners excluding those already paired today' do + create_test_cohort(20) + create_pair_history(@cohort,5) + expect(pair_me(unpaired_users_today(@cohort)[0]).length).to eq 9 + end + + it 'should order potential partners by number of times paired' do + create_test_cohort(20) + create_pair_history(@cohort,9,Date.today.days_ago(1)) + create_pair_history(@cohort,4,Date.today.days_ago(2)) + byebug + expect(pair_me(@user[0]).length).to eq 19 + end + +end + +describe 'Pair up a cohort' do + # to be written +end