Commit 9e1b067

mo khan <mo@mokhan.ca>
2016-07-23 22:57:56
extract methods.
1 parent 8acaaaa
app/models/training_history.rb
@@ -21,10 +21,10 @@ class TrainingHistory
     sets.where(workout: last_workout).count
   end
 
-  def deload?
+  def deload?(number_of_workouts: 3)
     recent_workouts = user.workouts.recent.with_exercise(exercise)
-    if recent_workouts.count >= 3
-      recent_workouts.any? do |workout|
+    if recent_workouts.count >= number_of_workouts
+      recent_workouts.last(number_of_workouts).any? do |workout|
         workout.sets.work.any?(&:failed?)
       end
     else
app/models/user_recommendation.rb
@@ -35,28 +35,34 @@ class UserRecommendation
     recommended_sets > 0 ? recommended_sets : recommendation.sets
   end
 
-  def next_weight(deload_percentage: 0.10)
+  def next_weight
     history = user.history_for(exercise)
     if history.deload?
-      last_weight = history.last_weight
-      ten_percent_decrease = (last_weight * deload_percentage).to(:lbs).amount
-      weight_to_deduct = if (ten_percent_decrease % 5) > 0
-        ten_percent_decrease - (ten_percent_decrease % 5) + 5
-      else
-        ten_percent_decrease - (ten_percent_decrease % 5)
-      end
-      last_weight - weight_to_deduct.lbs
+      deload(history.last_weight)
     else
-      last_weight = history.last_weight(successfull_only: true)
-      if last_weight.present? && last_weight > 0
-        5.lbs + last_weight
-      else
-        45.lbs
-      end
+      increase_weight(history.last_weight(successfull_only: true))
     end
   end
 
   def recommendation
     @recommendation ||= program.recommendations.find_by(exercise: exercise)
   end
+
+  def increase_weight(last_weight)
+    if last_weight.present? && last_weight > 0
+      5.lbs + last_weight
+    else
+      45.lbs
+    end
+  end
+
+  def deload(last_weight, percentage: 0.10)
+    ten_percent_decrease = (last_weight * percentage).to(:lbs).amount
+    weight_to_deduct = if (ten_percent_decrease % 5) > 0
+                         ten_percent_decrease - (ten_percent_decrease % 5) + 5
+                       else
+                         ten_percent_decrease - (ten_percent_decrease % 5)
+                       end
+    last_weight - weight_to_deduct.lbs
+  end
 end