Commit 8c30eb5

mo khan <mo@mokhan.ca>
2016-06-07 00:52:09
remove backbone model and maybe disable sets before you can start them.
1 parent 06feacd
app/assets/javascripts/models/training_session.js.coffee
@@ -1,3 +0,0 @@
-class Stronglifters.TrainingSession extends Backbone.Model
-  initialize: ->
-    console.log('hello')
app/assets/javascripts/templates/training_session_view.ractive
@@ -1,5 +1,5 @@
 <div class="row">
-  {{#each json.exercises}}
+  {{#each exercises}}
   <div class="panel small-12 columns">
     <div class="row">
       <div class="small-6 columns">
@@ -15,7 +15,7 @@
     <div class="row">
       <div class="small-12 columns">
       {{#each reps}}
-        <button on-click="completeSet" class="button small round secondary {{status}}">{{completed}}</button>
+        <button on-click="updateProgress" class="button small round {{status}}">{{completed}}</button>
       {{/each}}
       </div>
     </div>
app/assets/javascripts/views/home_gym.js.coffee
@@ -1,6 +1,6 @@
 Stronglifters.HomeGym = Ractive.extend
   template: RactiveTemplates["templates/home_gym"]
-  data:
+  data: ->
     city: ''
     gyms: []
     search:
app/assets/javascripts/views/training_session_view.js.coffee
@@ -2,19 +2,17 @@ class Stronglifters.TrainingSessionView extends Ractive
   template: RactiveTemplates["templates/training_session_view"]
 
   oninit: ->
-    @on 'completeSet', (event) -> @completeSet(event)
-    @observe '*.exercises.*.reps.*', (newValue, oldValue, keypath) ->
-      @updateStatus(newValue, oldValue, keypath)
+    @on 'updateProgress', (event) -> @updateProgress(event)
+    @observe 'exercises.*.reps.*', (newValue, oldValue, keypath) ->
+      @refreshStatus(newValue, oldValue, keypath)
 
-  completeSet: (event) ->
-    console.log(event)
+  updateProgress: (event) ->
     if @get("#{event.keypath}.completed") == 0
       @set("#{event.keypath}.completed", @get("#{event.keypath}.target"))
     else
       @subtract("#{event.keypath}.completed")
 
-  updateStatus: (newValue, oldValue, keyPath) ->
-    console.log([newValue, oldValue, keyPath])
+  refreshStatus: (newValue, oldValue, keyPath) ->
     if @get("#{keyPath}.completed") == 0
       @set("#{keyPath}.status", "secondary")
       return
app/views/training_sessions/_edit.json.jbuilder
@@ -1,8 +1,6 @@
 json.id training_session.id
 json.body_weight training_session.body_weight
-json.workout do
-  json.name training_session.workout.name
-end
+json.workout_name training_session.workout.name
 json.exercises training_session.workout.exercise_workouts do |exercise|
   json.name exercise.name
   json.sets exercise.sets
@@ -13,5 +11,3 @@ json.exercises training_session.workout.exercise_workouts do |exercise|
   end
   json.target_weight current_user.next_weight_for(exercise.exercise)
 end
-json.count training_session.exercise_sessions.count
-
app/views/training_sessions/edit.html.erb
@@ -1,6 +1,5 @@
 <div class="row">
   <div class="large-12 columns">
-    <%= render partial: 'edit.json.jbuilder', locals: { training_session: @training_session } %>
     <div id="training-session-view">
     </div>
   </div>
@@ -9,12 +8,8 @@
 
 <script type="text/javascript" charset="utf-8">
 var json = <%= raw render partial: 'edit.json.jbuilder', locals: { training_session: @training_session } %>;
-var model = new Stronglifters.TrainingSession(json);
 window.currentView = new Stronglifters.TrainingSessionView({
   el: 'training-session-view',
-  data: {
-    json: json,
-    model: model
-  }
+  data: json
 })
 </script>
spec/javascripts/views/training_session_view_spec.js.coffee
@@ -0,0 +1,64 @@
+#= require views/training_session_view
+describe "TrainingSessionView", ->
+  beforeEach ->
+    @el = $('<div>')
+    @subject = new Stronglifters.TrainingSessionView(
+      el: @el,
+      data: ->
+        {
+          id: "1",
+          body_weight: 225,
+          workout_name: "A",
+          exercises: [{
+              name: 'Squat',
+              sets: 3,
+              repetitions: 5,
+              reps: [{target: 5, completed: 0}, {target: 5, completed: 1},{target: 5, completed: 2}]
+              target_weight: 315,
+          }]
+        }
+    )
+
+  it "has one exercise", ->
+    @subject.get('exercises')
+    expect(@subject.get('exercises').length).toEqual(1)
+
+  it "indicates no progress recorded", ->
+    result = @subject.get('exercises.0.reps.0.status')
+    expect(result).toEqual('secondary')
+
+  it "disables the other sets", ->
+    secondSetButton = @el.find('button:eq(1)')
+    thirdSetButton = @el.find('button:eq(2)')
+    expect(secondSetButton.attr('disabled')).toEqual('disabled')
+    expect(thirdSetButton.attr('disabled')).toEqual('disabled')
+
+  describe "updating progress", ->
+    describe "when no reps are completed", ->
+      it "sets the reps to the target", ->
+        @el.find('button').first().trigger('click')
+        result = @subject.get('exercises.0.reps.0.completed')
+        expect(result).toEqual(5)
+
+      it "indicates a successful set", ->
+        @el.find('button').first().trigger('click')
+        result = @subject.get('exercises.0.reps.0.status')
+        expect(result).toEqual('success')
+
+      it "enables the next set", ->
+        @el.find('button').first().trigger('click')
+        expect(@el.find('button:eq(1)').attr('disabled')).toEqual('')
+
+    describe "when at least one rep is completed", ->
+      beforeEach ->
+        @subject.set('exercises.0.reps.0.completed', 5)
+
+      it 'decrements the count', ->
+        @el.find('button').first().trigger('click')
+        result = @subject.get('exercises.0.reps.0.completed')
+        expect(result).toEqual(4)
+
+      it "indicates a failed set", ->
+        @el.find('button').first().trigger('click')
+        result = @subject.get('exercises.0.reps.0.status')
+        expect(result).toEqual('alert')