Commit 72f5f27

mo khan <mo@mokhan.ca>
2015-05-31 16:12:46
add a method to session to make it easier to record a completed exercise.
1 parent 673f0da
app/models/training_session.rb
@@ -3,6 +3,20 @@ class TrainingSession < ActiveRecord::Base
   belongs_to :workout
   has_many :exercise_sessions
 
+  def train(exercise, target_weight, completed_sets)
+    recommendation = workout.
+      exercise_workouts.
+      find_by(exercise: exercise)
+    exercise_sessions.create!(
+      target_weight: target_weight,
+      exercise_workout: recommendation,
+      sets: completed_sets
+    )
+  end
+
+  # refactor this to use the new api to add a workout
+  # training_session = user.begin(workout_a)
+  # training_session.train(squat, 200, [5,5,5,5,5])
   def self.create_workout_from(workout_row, program)
     transaction do
       workout = program.workouts.find_by(name: workout_row.workout)
app/models/user.rb
@@ -16,6 +16,7 @@ class User < ActiveRecord::Base
     username
   end
 
+  # extract a personal record object
   def personal_record(exercise)
     exercise_sessions.
       joins(:exercise).
spec/models/training_session_spec.rb
@@ -85,4 +85,24 @@ describe TrainingSession, type: :model do
       end
     end
   end
+
+  describe "#train" do
+    subject { create(:training_session) }
+    let(:workout) { subject.workout }
+    let(:sets) { [5, 5, 5, 5, 5] }
+    let(:target_weight) { 200 }
+    let(:squat) { create(:exercise) }
+
+    before :each do
+      workout.add_exercise(squat)
+    end
+
+    it "returns a completed exercise" do
+      result = subject.train(squat, target_weight, sets)
+      expect(result).to be_persisted
+      expect(result.target_weight).to eql(target_weight.to_f)
+      expect(result.exercise).to eql(squat)
+      expect(result.sets).to eql(sets.map { |x| x.to_s })
+    end
+  end
 end
spec/factories.rb
@@ -7,7 +7,9 @@ FactoryGirl.define do
   end
   factory :training_session do
     association :user
+    association :workout
     occurred_at { DateTime.now }
+    body_weight { rand(250) }
   end
   factory :user do
     username { FFaker::Internet.user_name }