Commit eb0ac1c

mo khan <mo@mokhan.ca>
2016-07-10 01:05:26
add duration to recommendations.
1 parent 7235bb0
app/models/routine.rb
@@ -15,11 +15,13 @@ class Routine < ApplicationRecord
     name
   end
 
-  def add_exercise(exercise, sets: 5, repetitions: 5)
+  def add_exercise(exercise, sets: 5, repetitions: 5, duration: nil)
+    repetitions = 1 if duration.present?
     recommendations.create!(
+      duration: duration,
       exercise: exercise,
+      repetitions: repetitions,
       sets: sets,
-      repetitions: repetitions
     ) unless exercises.include?(exercise)
   end
 
db/migrate/20160710010020_add_duration_to_recommendations.rb
@@ -0,0 +1,5 @@
+class AddDurationToRecommendations < ActiveRecord::Migration[5.0]
+  def change
+    add_column :recommendations, :duration, :integer
+  end
+end
db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20160704162352) do
+ActiveRecord::Schema.define(version: 20160710010020) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -96,6 +96,7 @@ ActiveRecord::Schema.define(version: 20160704162352) do
     t.integer  "repetitions", null: false
     t.datetime "created_at",  null: false
     t.datetime "updated_at",  null: false
+    t.integer  "duration"
   end
 
   create_table "routines", id: :uuid, default: -> { "uuid_generate_v4()" }, force: :cascade do |t|
spec/models/routine_spec.rb
@@ -33,5 +33,17 @@ describe Routine do
       expect(subject.exercises.count).to eql(1)
       expect(subject.recommendations.count).to eql(1)
     end
+
+    it 'adds a timed exercise' do
+      subject.add_exercise(exercise, sets: 3, duration: 60.seconds)
+      expect(subject.exercises).to match_array([exercise])
+      expect(subject.recommendations.count).to eql(1)
+      recommendation = subject.recommendations.first.reload
+
+      expect(recommendation.duration).to eql(60.seconds.to_i)
+      expect(recommendation.exercise).to eql(exercise)
+      expect(recommendation.repetitions).to eql(1)
+      expect(recommendation.sets).to eql(3)
+    end
   end
 end