Commit ff4a5a9

mo khan <mo@mokhan.ca>
2016-07-01 15:28:57
hound happy.
1 parent 4becb16
app/controllers/workouts_controller.rb
@@ -40,7 +40,8 @@ class WorkoutsController < ApplicationController
   end
 
   def find_routine(routine_id)
-    current_program.routines.find_by(id: routine_id) || current_user.next_routine
+    current_program.routines.find_by(id: routine_id) ||
+      current_user.next_routine
   end
 
   def current_program
app/models/exercise_set.rb
@@ -2,7 +2,7 @@ class ExerciseSet < ActiveRecord::Base
   belongs_to :exercise
   belongs_to :workout
   scope :for, ->(exercise) { where(exercise: exercise).in_order }
-  scope :successful, -> { where('actual_repetitions = target_repetitions') }
+  scope :successful, -> { where("actual_repetitions = target_repetitions") }
   scope :work, -> { where(type: WorkSet.name) }
   scope :in_order, -> { order(:created_at) }
 
@@ -17,7 +17,7 @@ class ExerciseSet < ActiveRecord::Base
   def weight_per_side
     remaining_weight = target_weight.lbs - 45.lbs
     if remaining_weight > 0
-      "#{remaining_weight/2} lb/side"
+      "#{remaining_weight / 2} lb/side"
     end
   end
 end
app/models/quantity.rb
@@ -34,7 +34,7 @@ class Quantity
   end
 
   def >(other)
-    self.amount > amount_from(other)
+    amount > amount_from(other)
   end
 
   def >=(other)
@@ -42,7 +42,7 @@ class Quantity
   end
 
   def <(other)
-    self.amount < amount_from(other)
+    amount < amount_from(other)
   end
 
   def coerce(other)
@@ -50,7 +50,7 @@ class Quantity
   end
 
   def eql?(other, delta = 0.1)
-    (self.amount - amount_from(other)).abs <= delta
+    (amount - amount_from(other)).abs <= delta
   end
 
   def ==(other)
app/models/warm_up.rb
@@ -28,112 +28,112 @@ class WarmUp
 
   def dead_lift_warmup
     return self if target_weight < 155.0
-    return add_set(135.0, 5) if (target_weight < 175.0)
+    return add_set(135.0, 5) if target_weight < 175.0
     add_set(135.0, 5)
-    return add_set(165.0, 5) if (target_weight < 200.0)
-    return add_set(175.0, 5) if (target_weight < 220.0)
-    return add_set(185.0, 5) if (target_weight < 225.0)
+    return add_set(165.0, 5) if target_weight < 200.0
+    return add_set(175.0, 5) if target_weight < 220.0
+    return add_set(185.0, 5) if target_weight < 225.0
     add_set(185.0, 5)
-    return add_set(205.0, 5) if (target_weight < 240.0)
-    return add_set(215.0, 5) if (target_weight < 250.0)
-    return add_set(225.0, 5) if (target_weight < 270.0)
+    return add_set(205.0, 5) if target_weight < 240.0
+    return add_set(215.0, 5) if target_weight < 250.0
+    return add_set(225.0, 5) if target_weight < 270.0
     add_set(225.0, 5)
-    return add_set(245.0, 5) if (target_weight < 275.0)
-    return add_set(255.0, 5) if (target_weight < 290.0)
-    return add_set(265.0, 5) if (target_weight < 301.0)
+    return add_set(245.0, 5) if target_weight < 275.0
+    return add_set(255.0, 5) if target_weight < 290.0
+    return add_set(265.0, 5) if target_weight < 301.0
     add_set(265.0, 3)
-    return add_set(285.0, 1) if (target_weight < 320.0)
-    return add_set(295.0, 1) if (target_weight < 330.0)
-    return add_set(305.0, 1) if (target_weight < 340.0)
-    return add_set(315.0, 1) if (target_weight < 350.0)
+    return add_set(285.0, 1) if target_weight < 320.0
+    return add_set(295.0, 1) if target_weight < 330.0
+    return add_set(305.0, 1) if target_weight < 340.0
+    return add_set(315.0, 1) if target_weight < 350.0
     add_set(315.0, 3)
-    return add_set(335.0, 1) if (target_weight < 360.0)
-    return add_set(340.0, 1) if (target_weight < 370.0)
-    return add_set(345.0, 1) if (target_weight < 380.0)
-    return add_set(350.0, 1) if (target_weight < 390.0)
-    return add_set(355.0, 1) if (target_weight < 395.0)
-    return add_set(360.0, 1) if (target_weight < 400.0)
-    return add_set(365.0, 1)
+    return add_set(335.0, 1) if target_weight < 360.0
+    return add_set(340.0, 1) if target_weight < 370.0
+    return add_set(345.0, 1) if target_weight < 380.0
+    return add_set(350.0, 1) if target_weight < 390.0
+    return add_set(355.0, 1) if target_weight < 395.0
+    return add_set(360.0, 1) if target_weight < 400.0
+    add_set(365.0, 1)
   end
 
   def barbell_row_warmup
     return self if target_weight < 105.0 || target_weight < 65.0
-    return add_set(95.0, 5) if (target_weight < 145.0)
-    return add_set(115.0, 5) if (target_weight < 160.0)
-    return add_set(65.0, 3) if (target_weight < 105.0)
-    return add_set(75.0, 3) if (target_weight < 125.0)
-    return add_set(85.0, 3) if (target_weight < 135.0)
-    return add_set(135.0, 3) if (target_weight < 185.0)
+    return add_set(95.0, 5) if target_weight < 145.0
+    return add_set(115.0, 5) if target_weight < 160.0
+    return add_set(65.0, 3) if target_weight < 105.0
+    return add_set(75.0, 3) if target_weight < 125.0
+    return add_set(85.0, 3) if target_weight < 135.0
+    return add_set(135.0, 3) if target_weight < 185.0
     add_set(135.0, 5)
-    return add_set(165.0, 3) if (target_weight < 200.0)
-    return add_set(175.0, 3) if (target_weight < 220.0)
-    return add_set(185.0, 3) if (target_weight < 225.0)
+    return add_set(165.0, 3) if target_weight < 200.0
+    return add_set(175.0, 3) if target_weight < 220.0
+    return add_set(185.0, 3) if target_weight < 225.0
     add_set(185.0, 5)
-    return add_set(205.0, 2) if (target_weight < 240.0)
-    return add_set(215.0, 2) if (target_weight < 250.0)
-    return add_set(225.0, 2) if (target_weight < 270.0)
+    return add_set(205.0, 2) if target_weight < 240.0
+    return add_set(215.0, 2) if target_weight < 250.0
+    return add_set(225.0, 2) if target_weight < 270.0
     add_set(225.0, 5)
-    return add_set(245.0, 1) if (target_weight < 275.0)
-    return add_set(255.0, 1) if (target_weight < 290.0)
-    return add_set(265.0, 1) if (target_weight < 301.0)
+    return add_set(245.0, 1) if target_weight < 275.0
+    return add_set(255.0, 1) if target_weight < 290.0
+    return add_set(265.0, 1) if target_weight < 301.0
     add_set(265.0, 3)
-    return add_set(285.0, 1) if (target_weight < 320.0)
-    return add_set(295.0, 1) if (target_weight < 330.0)
-    return add_set(305.0, 1) if (target_weight < 340.0)
-    return add_set(315.0, 1) if (target_weight < 350.0)
+    return add_set(285.0, 1) if target_weight < 320.0
+    return add_set(295.0, 1) if target_weight < 330.0
+    return add_set(305.0, 1) if target_weight < 340.0
+    return add_set(315.0, 1) if target_weight < 350.0
     add_set(315.0, 3)
-    return add_set(330.0, 1) if (target_weight < 355.0)
-    return add_set(340.0, 1) if (target_weight < 370.0)
-    return add_set(345.0, 1) if (target_weight < 380.0)
-    return add_set(350.0, 1) if (target_weight < 390.0)
-    return add_set(355.0, 1) if (target_weight < 395.0)
-    return add_set(360.0, 1) if (target_weight < 400.0)
-    return add_set(365.0, 1)
+    return add_set(330.0, 1) if target_weight < 355.0
+    return add_set(340.0, 1) if target_weight < 370.0
+    return add_set(345.0, 1) if target_weight < 380.0
+    return add_set(350.0, 1) if target_weight < 390.0
+    return add_set(355.0, 1) if target_weight < 395.0
+    return add_set(360.0, 1) if target_weight < 400.0
+    add_set(365.0, 1)
   end
 
   def default_warmup
-    return self if (target_weight < 65.0)
+    return self if target_weight < 65.0
     add_set(45.0, 5)
     add_set(45.0, 5)
-    return self if (target_weight < 95.0)
-    return add_set(65.0, 3) if (target_weight < 105.0)
-    return add_set(75.0, 3) if (target_weight < 125.0)
-    return add_set(85.0, 3) if (target_weight < 135.0)
+    return self if target_weight < 95.0
+    return add_set(65.0, 3) if target_weight < 105.0
+    return add_set(75.0, 3) if target_weight < 125.0
+    return add_set(85.0, 3) if target_weight < 135.0
     add_set(95.0, 5)
-    return add_set(115.0, 3) if (target_weight < 150.0)
-    return add_set(125.0, 3) if (target_weight < 160.0)
-    return add_set(135.0, 3) if (target_weight < 185.0)
-    return add_set(135.0, 3) if (target_weight < 185.0)
+    return add_set(115.0, 3) if target_weight < 150.0
+    return add_set(125.0, 3) if target_weight < 160.0
+    return add_set(135.0, 3) if target_weight < 185.0
+    return add_set(135.0, 3) if target_weight < 185.0
     add_set(135.0, 5)
-    return add_set(165.0, 3) if (target_weight < 200.0)
-    return add_set(175.0, 3) if (target_weight < 220.0)
-    return add_set(185.0, 3) if (target_weight < 225.0)
+    return add_set(165.0, 3) if target_weight < 200.0
+    return add_set(175.0, 3) if target_weight < 220.0
+    return add_set(185.0, 3) if target_weight < 225.0
     add_set(185.0, target_weight < 305.0 ? 3 : 5)
-    return add_set(205.0, 2) if (target_weight < 240.0)
-    return add_set(215.0, 2) if (target_weight < 250.0)
-    return add_set(225.0, 2) if (target_weight < 270.0)
+    return add_set(205.0, 2) if target_weight < 240.0
+    return add_set(215.0, 2) if target_weight < 250.0
+    return add_set(225.0, 2) if target_weight < 270.0
     add_set(225.0, target_weight < 305.0 ? 2 : 3)
-    return add_set(245.0, 1) if (target_weight < 275.0)
-    return add_set(255.0, 1) if (target_weight < 290.0)
-    return add_set(265.0, 1) if (target_weight < 301.0)
-    if (target_weight < 305.0)
+    return add_set(245.0, 1) if target_weight < 275.0
+    return add_set(255.0, 1) if target_weight < 290.0
+    return add_set(265.0, 1) if target_weight < 301.0
+    if target_weight < 305.0
       add_set(265.0, 1)
-    elsif (target_weight < 350.0)
+    elsif target_weight < 350.0
       add_set(265.0, 2)
     else
       add_set(265.0, 3)
     end
-    return add_set(285.0, 1) if (target_weight < 320.0)
-    return add_set(295.0, 1) if (target_weight < 330.0)
-    return add_set(305.0, 1) if (target_weight < 340.0)
-    return add_set(315.0, 1) if (target_weight < 350.0)
+    return add_set(285.0, 1) if target_weight < 320.0
+    return add_set(295.0, 1) if target_weight < 330.0
+    return add_set(305.0, 1) if target_weight < 340.0
+    return add_set(315.0, 1) if target_weight < 350.0
     add_set(315.0, 2)
-    return add_set(330.0, 1) if (target_weight < 355.0)
-    return add_set(340.0, 1) if (target_weight < 370.0)
-    return add_set(345.0, 1) if (target_weight < 380.0)
-    return add_set(350.0, 1) if (target_weight < 390.0)
-    return add_set(355.0, 1) if (target_weight < 395.0)
-    return add_set(360.0, 1) if (target_weight < 400.0)
-    return add_set(365.0, 1)
+    return add_set(330.0, 1) if target_weight < 355.0
+    return add_set(340.0, 1) if target_weight < 370.0
+    return add_set(345.0, 1) if target_weight < 380.0
+    return add_set(350.0, 1) if target_weight < 390.0
+    return add_set(355.0, 1) if target_weight < 395.0
+    return add_set(360.0, 1) if target_weight < 400.0
+    add_set(365.0, 1)
   end
 end
app/models/workout.rb
@@ -19,10 +19,11 @@ class Workout < ActiveRecord::Base
       if set.present? && sets.where(exercise: exercise).at(set).present?
         sets.where(exercise: exercise).at(set)
       else
+        recommendation = program.recommendation_for(user, exercise)
         sets.build(
           type: WorkSet.name,
           exercise: exercise,
-          target_repetitions: program.recommendation_for(user, exercise).repetitions
+          target_repetitions: recommendation.repetitions
         )
       end
     set.update!(actual_repetitions: repetitions, target_weight: target_weight)
db/migrate/20160630154702_add_type_to_exercise_sets.rb
@@ -1,7 +1,7 @@
 class AddTypeToExerciseSets < ActiveRecord::Migration
   def change
     change_table :exercise_sets do |t|
-      t.string :type, null: false, default: 'WorkSet'
+      t.string :type, null: false, default: "WorkSet"
     end
     change_column_default :exercise_sets, :type, nil
   end
spec/features/workouts_spec.rb
@@ -9,7 +9,14 @@ feature "Workouts", type: :feature do
   feature "viewing history" do
     include_context "stronglifts_program"
     subject { WorkoutsPage.new }
-    let!(:workout) { create(:workout, user: user, routine: routine_a, occurred_at: DateTime.now, body_weight: 210.0) }
+    let!(:workout) do
+      create(:workout,
+             user: user,
+             routine: routine_a,
+             occurred_at: DateTime.now,
+             body_weight: 210.0
+            )
+    end
 
     it "displays each training session" do
       subject.visit_page
@@ -21,7 +28,7 @@ feature "Workouts", type: :feature do
     include_context "stronglifts_program"
     subject { NewWorkoutPage.new }
 
-    it 'creates a new workout' do
+    it "creates a new workout" do
       subject.visit_page
       subject.change_body_weight(225.0)
       subject.click_start
@@ -51,19 +58,19 @@ feature "Workouts", type: :feature do
       subject.open_section(squat)
     end
 
-    it 'saves the successful set' do
+    it "saves the successful set" do
       first_squat_set = workout.sets.for(squat).at(0)
       subject.complete(set: first_squat_set)
       expect(first_squat_set.reload.actual_repetitions).to eql(5)
     end
 
-    it 'saves the failed set' do
+    it "saves the failed set" do
       second_squat_set = workout.sets.for(squat).at(1)
       subject.complete(set: second_squat_set, repetitions: 4)
       expect(second_squat_set.reload.actual_repetitions).to eql(4)
     end
 
-    it 'does not change an incomplete set' do
+    it "does not change an incomplete set" do
       third_squat_set = workout.sets.for(squat).at(2)
       expect(third_squat_set.reload.actual_repetitions).to be_nil
     end
spec/models/csv/import_spec.rb
@@ -128,7 +128,7 @@ describe Csv::Import do
     end
 
     it "imports the correct number of sets" do
-      row = ["06/05/16", "", "B", "101.93", "225", "Squat", "125", "275", "5", "5", "5", "", "", "Overhead press", "57.5", "125", "5", "5", "5", "5", "5", "Deadlift", "127.5", "285", "5", "", "", "" , "", "", "", "", "", ""]
+      row = ["06/05/16", "", "B", "101.93", "225", "Squat", "125", "275", "5", "5", "5", "", "", "Overhead press", "57.5", "125", "5", "5", "5", "5", "5", "Deadlift", "127.5", "285", "5", "", "", "", "", "", "", "", "", ""]
 
       subject.import(row)
       workout = user.workouts.first
spec/models/exercise_set_spec.rb
@@ -1,15 +1,15 @@
-require 'rails_helper'
+require "rails_helper"
 
 describe ExerciseSet do
   subject { build(:exercise_set) }
 
   describe "#weight_per_side" do
-    it 'returns empty bar' do
+    it "returns empty bar" do
       subject.target_weight = 45.lbs
       expect(subject.weight_per_side).to be_blank
     end
 
-    it 'returns 25 lbs/side' do
+    it "returns 25 lbs/side" do
       subject.target_weight = 95.lbs
       expect(subject.weight_per_side).to eql("25.0 lb/side")
     end
spec/models/program_spec.rb
@@ -21,65 +21,65 @@ describe Program do
     describe "squat" do
       let(:user) { build(:user) }
 
-      it 'returns 5 work sets of 5 repetitions' do
+      it "returns 5 work sets of 5 repetitions" do
         sets = subject.prepare_sets_for(user, squat)
-        worksets = sets.find_all { |x| x.work? }
+        worksets = sets.select(&:work?)
         expect(worksets.length).to eql(5)
         expect(worksets.map(&:target_repetitions)).to match_array([5, 5, 5, 5, 5])
       end
 
-      it 'returns 3 sets of 5 repetitions when the last workout was 3x5' do
+      it "returns 3 sets of 5 repetitions when the last workout was 3x5" do
         workout = create(:workout, user: user, routine: routine_a)
         3.times { workout.train(squat, 45, repetitions: 5) }
 
         sets = subject.prepare_sets_for(user, squat)
-        worksets = sets.find_all { |x| x.work? }
+        worksets = sets.select(&:work?)
         expect(worksets.length).to eql(3)
         expect(worksets.map(&:target_repetitions)).to match_array([5, 5, 5])
       end
 
-      it 'returns the correct exercise for each set' do
+      it "returns the correct exercise for each set" do
         sets = subject.prepare_sets_for(user, squat)
         expect(sets.map(&:exercise).uniq).to match_array([squat])
       end
 
-      it 'returns 45 lbs for the first workout' do
-        sets = subject.prepare_sets_for(user, squat).find_all { |x| x.work? }
+      it "returns 45 lbs for the first workout" do
+        sets = subject.prepare_sets_for(user, squat).select(&:work?)
         expect(sets.map(&:target_weight).uniq).to match_array([45.lbs])
       end
 
-      it 'returns 50 lbs for the second workout' do
+      it "returns 50 lbs for the second workout" do
         workout = create(:workout, user: user, routine: routine_a)
         5.times { workout.train(squat, 45, repetitions: 5) }
 
-        sets = subject.prepare_sets_for(user, squat).find_all { |x| x.work? }
+        sets = subject.prepare_sets_for(user, squat).select(&:work?)
         expect(sets.map(&:target_weight).uniq).to match_array([50.lbs])
       end
 
-      it 'returns the same weight after a failed workout' do
+      it "returns the same weight after a failed workout" do
         workout = create(:workout, user: user, routine: routine_a)
         5.times { |n| workout.train(squat, 45, repetitions: n) }
 
-        sets = subject.prepare_sets_for(user, squat).find_all { |x| x.work? }
+        sets = subject.prepare_sets_for(user, squat).select(&:work?)
         expect(sets.map(&:target_weight).uniq).to match_array([45.lbs])
       end
 
       describe "warmup" do
         describe "when the workset is less than 65 lbs" do
-          it 'returns zero warmup sets' do
+          it "returns zero warmup sets" do
             sets = subject.prepare_sets_for(user, squat)
-            warmup_sets = sets.find_all { |x| x.warm_up? }
+            warmup_sets = sets.select(&:warm_up?)
             expect(warmup_sets.length).to eql(0)
           end
         end
 
         describe "when the work set is between 65 lbs an 95 lbs" do
-          it 'returns two warmup sets' do
+          it "returns two warmup sets" do
             workout = create(:workout, user: user, routine: routine_a)
-            5.times { |n| workout.train(squat, 65, repetitions: 5) }
+            5.times { workout.train(squat, 65, repetitions: 5) }
 
             sets = subject.prepare_sets_for(user, squat)
-            warmup_sets = sets.find_all { |x| x.warm_up? }
+            warmup_sets = sets.select(&:warm_up?)
             expect(warmup_sets.length).to eql(2)
             expect(warmup_sets.at(0).target_weight.lbs).to eql(45.lbs)
             expect(warmup_sets.at(0).target_repetitions).to eql(5)
@@ -89,12 +89,12 @@ describe Program do
         end
 
         describe "when the work set is between 95 lbs and 105 lbs" do
-          it 'returns another warm up set' do
+          it "returns another warm up set" do
             workout = create(:workout, user: user, routine: routine_a)
-            5.times { |n| workout.train(squat, 95, repetitions: 5) }
+            5.times { workout.train(squat, 95, repetitions: 5) }
 
             sets = subject.prepare_sets_for(user, squat)
-            warmup_sets = sets.find_all { |x| x.warm_up? }
+            warmup_sets = sets.select(&:warm_up?)
             expect(warmup_sets.length).to eql(3)
             expect(warmup_sets.at(2).target_weight.lbs).to eql(65.lbs)
             expect(warmup_sets.at(2).target_repetitions).to eql(3)
@@ -102,11 +102,11 @@ describe Program do
         end
 
         describe "when the work set is between 105 lbs and 125 lbs" do
-          it 'returns another warm up set' do
+          it "returns another warm up set" do
             workout = create(:workout, user: user, routine: routine_a)
-            5.times { |n| workout.train(squat, 105, repetitions: 5) }
+            5.times { workout.train(squat, 105, repetitions: 5) }
 
-            warmup_sets = subject.prepare_sets_for(user, squat).find_all { |x| x.warm_up? }
+            warmup_sets = subject.prepare_sets_for(user, squat).select(&:warm_up?)
             expect(warmup_sets.length).to eql(3)
             expect(warmup_sets.last.target_weight.lbs).to eql(75.lbs)
             expect(warmup_sets.last.target_repetitions).to eql(3)
@@ -114,11 +114,11 @@ describe Program do
         end
 
         describe "when the work set is between 125 lbs and 135 lbs" do
-          it 'returns another warm up set' do
+          it "returns another warm up set" do
             workout = create(:workout, user: user, routine: routine_a)
-            5.times { |n| workout.train(squat, 125, repetitions: 5) }
+            5.times { workout.train(squat, 125, repetitions: 5) }
 
-            warmup_sets = subject.prepare_sets_for(user, squat).find_all { |x| x.warm_up? }
+            warmup_sets = subject.prepare_sets_for(user, squat).select(&:warm_up?)
             expect(warmup_sets.length).to eql(3)
             expect(warmup_sets.last.target_weight.lbs).to eql(85.lbs)
             expect(warmup_sets.last.target_repetitions).to eql(3)
@@ -126,11 +126,11 @@ describe Program do
         end
 
         describe "when the work set is between 135 lbs and 150 lbs" do
-          it 'returns another warm up set' do
+          it "returns another warm up set" do
             workout = create(:workout, user: user, routine: routine_a)
-            5.times { |n| workout.train(squat, 135, repetitions: 5) }
+            5.times { workout.train(squat, 135, repetitions: 5) }
 
-            warmup_sets = subject.prepare_sets_for(user, squat).find_all { |x| x.warm_up? }
+            warmup_sets = subject.prepare_sets_for(user, squat).select(&:warm_up?)
             expect(warmup_sets.length).to eql(4)
             expect(warmup_sets.last.target_weight.lbs).to eql(115.lbs)
             expect(warmup_sets.last.target_repetitions).to eql(3)
@@ -142,9 +142,9 @@ describe Program do
     describe "deadlift" do
       let(:user) { build(:user) }
 
-      it 'returns 1 work set with 5 repetitions' do
+      it "returns 1 work set with 5 repetitions" do
         sets = subject.prepare_sets_for(user, deadlift)
-        worksets = sets.find_all { |x| x.work? }
+        worksets = sets.select(&:work?)
         expect(worksets.length).to eql(1)
         expect(worksets.map(&:target_repetitions)).to match_array([5])
       end