Commit 1977d2b

mo khan <mo@mokhan.ca>
2016-12-31 21:36:05
cast from hash to quantity.
1 parent 465ab6a
Changed files (4)
app/controllers/api/workouts_controller.rb
@@ -8,10 +8,10 @@ class Api::WorkoutsController < Api::Controller
   end
 
   def create
-    workout = current_user.workouts.build(secure_params)
-    workout.occurred_at = DateTime.now
-    workout.save!
-    render nothing: true, status: :created
+    @workout = current_user.workouts.build(secure_params)
+    @workout.occurred_at = DateTime.now
+    @workout.save!
+    render status: :created
   end
 
   private
@@ -19,13 +19,13 @@ class Api::WorkoutsController < Api::Controller
   def secure_params
     params.require(:workout).permit(
       :routine_id,
-      :body_weight,
+      body_weight: [:amount, :unit],
       exercise_sets_attributes: [
         :exercise_id,
         :target_duration,
         :target_repetitions,
-        :target_weight,
         :type,
+        target_weight: [:amount, :unit],
       ]
     )
   end
app/views/api/workouts/create.json.jbuilder
@@ -0,0 +1,12 @@
+json.body_weight @workout.body_weight
+json.routine do |routine|
+  json.id @workout.routine.id
+  json.name @workout.routine.name
+end
+json.exercises @workout.sets.group_by(&:exercise) do |exercise, sets|
+  json.id exercise.id
+  json.name exercise.name
+end
+json.sets @workout.sets do |set|
+  json.partial! 'api/sets/set', set: set
+end
lib/quantity_type.rb
@@ -4,6 +4,8 @@ class QuantityType < ActiveRecord::Type::Float
 
     if value.is_a? Quantity
       value.to(:lbs)
+    elsif value.is_a? Hash
+      Quantity.new(value[:amount].to_f, value[:unit].to_sym)
     else
       Quantity.new(value.to_f, :lbs)
     end
spec/controllers/api/workouts_controller_spec.rb
@@ -72,11 +72,11 @@ describe Api::WorkoutsController do
       post :create, params: {
         workout: {
           routine_id: routine_b.id,
-          body_weight: body_weight,
+          body_weight: { amount: body_weight.amount, unit: body_weight.unit },
           exercise_sets_attributes: [{
             exercise_id: squat.id,
             target_repetitions: 5,
-            target_weight: 200,
+            target_weight: { amount: 200, unit: 'lbs' },
             type: "WorkSet",
           }]
         }