Commit 3b111ed

mo khan <mo@mokhan.ca>
2016-07-20 02:28:45
convert parameters to hash of options..
1 parent d422a74
Changed files (6)
app/assets/javascripts/models/timer.js.coffee
@@ -1,12 +1,13 @@
 class Stronglifters.Timer
-  constructor: (databag, key = 'clock', maxMilliseconds = 600000) ->
-    @databag = databag
-    @format = 'mm:ss'
+  constructor: (options) ->
+    @databag = options['databag']
+    @format = options['format'] || 'mm:ss'
     @interval = 1000
-    @key = key
-    @maxMilliseconds = maxMilliseconds
+    @key = options['key'] || 'clock'
+    @maxMilliseconds = options['maxMilliseconds'] || 600000
+    @success = options['success'] || -> { }
 
-  start: ->
+  start: (options) ->
     @stop()
 
     @databag.set('timer', 0)
@@ -17,6 +18,7 @@ class Stronglifters.Timer
     @databag.set(@key, moment.utc(@databag.get('timer')).format(@format))
     if @databag.get('timer') >= @maxMilliseconds
       @stop()
+      @success()
 
   stop: =>
     if @running()
app/assets/javascripts/views/workout_view.js.coffee
@@ -4,7 +4,10 @@ class Stronglifters.WorkoutView extends Ractive
   template: RactiveTemplates["templates/workout_view"]
 
   oninit: ->
-    @clock = new Stronglifters.Timer(@)
+    @clock = new Stronglifters.Timer
+      databag: @
+      key: 'clock'
+
     @timers = { }
     @on 'updateProgress', (event) ->
       @withModel event.keypath, (model) =>
@@ -70,7 +73,16 @@ class Stronglifters.WorkoutView extends Ractive
     timer = @timers[keypath]
     if timer?
       timer.stop()
+      model.save()
     else
-      timer = new Stronglifters.Timer(@, "#{keypath}.actual_duration", 60000)
+      targetMilliseconds = model.get('target_duration') * 1000
+      timer = new Stronglifters.Timer
+        databag: @
+        format: 'ss'
+        key: "#{keypath}.actual_duration"
+        maxMilliseconds: targetMilliseconds
+        success: ->
+          console.log("SAVING")
+          model.save()
       @timers[keypath] = timer
       timer.start()
app/controllers/sets_controller.rb
@@ -7,6 +7,6 @@ class SetsController < ApplicationController
   private
 
   def secure_params
-    params.require(:set).permit(:actual_repetitions)
+    params.require(:set).permit(:actual_repetitions, :actual_duration)
   end
 end
app/models/exercise_set.rb
@@ -1,4 +1,5 @@
 class ExerciseSet < ApplicationRecord
+  attr_accessor :actual_duration
   attribute :target_weight, :quantity
   belongs_to :exercise
   belongs_to :workout
app/views/layouts/application.html.erb
@@ -4,8 +4,8 @@
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
     <title><%= t('.title') %> <%= content_for?(:title) ? "| #{yield(:title)}" : "" %></title>
-    <%= stylesheet_link_tag    "application" %>
-    <%= javascript_include_tag "application" %>
+    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
+    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
     <%= javascript_include_tag "https://www.google.com/jsapi", "chartkick" %>
     <%= javascript_include_tag "https://apis.google.com/js/api.js" %>
     <%= javascript_include_tag "https://apis.google.com/js/client.js" %>
app/views/sets/_set.jbuilder
@@ -4,4 +4,5 @@ json.target_weight set.target_weight.pretty_print
 json.weight_per_side set.weight_per_side
 json.target_repetitions set.target_repetitions
 json.actual_repetitions set.actual_repetitions
+json.actual_duration set.actual_duration
 json.target_duration set.target_duration