Commit 71ad00f

mo khan <mo@mokhan.ca>
2016-07-01 14:40:07
display weight per side.
1 parent b2a7d78
app/assets/javascripts/templates/workout_view.ractive
@@ -9,7 +9,7 @@
   {{#each workout.exercises}}
   <li class="accordion-navigation">
     <a href="#panel-{{id}}">{{name}}</a>
-    <div id="panel-{{id}}" class="content">
+    <div id="panel-{{id}}" class="content active">
       {{#each sets}}
       <div class="row">
         <div class="columns small-3">
@@ -24,7 +24,7 @@
         </div>
         <div class="columns small-4">
           <p class="text-center">
-            110 lb/side
+            {{weight_per_side}}
           </p>
         </div>
       </div>
app/models/exercise_set.rb
@@ -11,4 +11,11 @@ class ExerciseSet < ActiveRecord::Base
   def warm_up?
     type == WarmUpSet.name
   end
+
+  def weight_per_side
+    remaining_weight = target_weight.lbs - 45.lbs
+    if remaining_weight > 0
+      "#{remaining_weight/2} lb/side"
+    end
+  end
 end
app/models/quantity.rb
@@ -18,6 +18,18 @@ class Quantity
     Quantity.new(amount + amount_from(other), unit)
   end
 
+  def -(other)
+    Quantity.new(amount - amount_from(other), unit)
+  end
+
+  def /(other)
+    Quantity.new(amount / amount_from(other), unit)
+  end
+
+  def *(other)
+    Quantity.new(amount * amount_from(other), unit)
+  end
+
   def >(other)
     self.amount > amount_from(other)
   end
@@ -30,6 +42,10 @@ class Quantity
     self.amount < amount_from(other)
   end
 
+  def coerce(other)
+    [self, other]
+  end
+
   def eql?(other, delta = 0.1)
     (self.amount - amount_from(other)).abs <= delta
   end
app/views/sets/_set.jbuilder
@@ -1,4 +1,5 @@
 json.id set.id
 json.target_weight set.target_weight
+json.weight_per_side set.weight_per_side
 json.target_repetitions set.target_repetitions
 json.actual_repetitions set.actual_repetitions
spec/models/exercise_set_spec.rb
@@ -0,0 +1,17 @@
+require 'rails_helper'
+
+describe ExerciseSet do
+  subject { build(:exercise_set) }
+
+  describe "#weight_per_side" 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
+      subject.target_weight = 95.lbs
+      expect(subject.weight_per_side).to eql("25.0 lb/side")
+    end
+  end
+end
spec/models/quantity_spec.rb
@@ -80,6 +80,72 @@ describe Quantity do
     end
   end
 
+  describe "#-" do
+    it 'subtracts lbs from lbs' do
+      quantity = Quantity.new(135.0, :lbs)
+      other = Quantity.new(135.0, :lbs)
+      expect(quantity - other).to eql(Quantity.new(0.0, :lbs))
+    end
+
+    it 'subtracts kgs from lbs' do
+      quantity = Quantity.new(135.0, :lbs)
+      other = Quantity.new(61.2, :kg)
+      expect(quantity - other).to eql(Quantity.new(0.0, :lbs))
+    end
+
+    it 'subtracts lbs from kgs' do
+      quantity = Quantity.new(61.2, :kg)
+      other = Quantity.new(135.0, :lbs)
+      expect(quantity - other).to eql(Quantity.new(0.0, :kg))
+    end
+
+    it 'subtracts a float from lbs' do
+      quantity = Quantity.new(135.0, :lbs)
+      other = 135.0
+      expect(quantity - other).to eql(Quantity.new(0.0, :lbs))
+    end
+  end
+
+  describe "#/" do
+    it 'divides lbs from lbs' do
+      quantity = Quantity.new(135.0, :lbs)
+      other = Quantity.new(135.0, :lbs)
+      expect(quantity / other).to eql(Quantity.new(1.0, :lbs))
+    end
+
+    it 'divides kgs from lbs' do
+      quantity = Quantity.new(135.0, :lbs)
+      other = Quantity.new(61.2, :kg)
+      expect(quantity / other).to eql(Quantity.new(1.0, :lbs))
+    end
+
+    it 'divides lbs from kgs' do
+      quantity = Quantity.new(61.2, :kg)
+      other = Quantity.new(135.0, :lbs)
+      expect(quantity / other).to eql(Quantity.new(1.0, :kg))
+    end
+
+    it 'divides a float from lbs' do
+      quantity = Quantity.new(135.0, :lbs)
+      other = 135.0
+      expect(quantity / other).to eql(Quantity.new(1.0, :lbs))
+    end
+  end
+
+  describe "#*" do
+    it 'multiples lbs with lbs' do
+      quantity = Quantity.new(135.0, :lbs)
+      other = Quantity.new(135.0, :lbs)
+      expect(quantity * other).to eql(Quantity.new(18_225.0, :lbs))
+    end
+
+    it 'multiples a float with lbs' do
+      quantity = Quantity.new(135.0, :lbs)
+      other = 135.0
+      expect(quantity * other).to eql(Quantity.new(18_225.0, :lbs))
+    end
+  end
+
   describe "#>" do
     it 'compares lbs with lbs' do
       quantity = Quantity.new(135.1, :lbs)
spec/factories.rb
@@ -2,6 +2,12 @@ FactoryGirl.define do
   factory :exercise do
     name { FFaker::Internet.user_name }
   end
+  factory :exercise_set do
+    association :exercise
+    association :workout
+    target_repetitions { rand(12) }
+    target_weight { rand(400) }
+  end
   factory :program do
     name { FFaker::Internet.user_name }
   end