Commit 9317278

mo khan <mo@mokhan.ca>
2016-06-25 14:41:51
display as accordion.
1 parent 2eca7d1
app/controllers/workouts_controller.rb
@@ -1,24 +1,15 @@
 class WorkoutsController < ApplicationController
   def index
-    @workouts = paginate(
-      current_user.
-      workouts.
-      includes(:routine, :program, exercise_sets: [:exercise]).
-      order(occurred_at: :desc)
-    )
+    @workouts = paginate(recent_workouts)
   end
 
   def new
-    @routine = current_user.next_routine
+    @routine = find_routine(params[:routine_id])
+    @all_routines = current_program.routines - [@routine]
     @workout = current_user.next_workout_for(@routine)
   end
 
   def create
-    secure_params = params.require(:workout).permit(:routine_id, :body_weight, exercise_sets_attributes: [
-      :target_repetitions,
-      :target_weight,
-      :exercise_id
-    ])
     secure_params.merge!(occurred_at: DateTime.now)
     workout = current_user.workouts.create!(secure_params)
     redirect_to edit_workout_path(workout)
@@ -27,4 +18,30 @@ class WorkoutsController < ApplicationController
   def edit
     @workout = current_user.workouts.find(params[:id])
   end
+
+  private
+
+  def secure_params
+    params.require(:workout).permit(
+      :routine_id,
+      :body_weight,
+      exercise_sets_attributes: [
+        :exercise_id,
+        :target_repetitions,
+        :target_weight,
+      ]
+    )
+  end
+
+  def recent_workouts
+    current_user.workouts.recent.includes(:routine, :program, exercise_sets: [:exercise])
+  end
+
+  def find_routine(routine_id)
+    current_program.routines.find_by(id: routine_id) || current_user.next_routine
+  end
+
+  def current_program
+    current_user.current_program
+  end
 end
app/models/progress.rb
@@ -7,7 +7,7 @@ class Progress
   end
 
   def to_sets
-    sets.pluck(:actual_repetitions)
+    sets.pluck(:actual_repetitions).compact
   end
 
   def max_weight
app/models/workout.rb
@@ -5,6 +5,10 @@ class Workout < ActiveRecord::Base
   has_many :exercises, through: :exercise_sets
   has_many :exercise_sets, dependent: :destroy
   accepts_nested_attributes_for :exercise_sets
+  delegate :name, to: :routine
+
+  scope :recent, -> { order(occurred_at: :desc) }
+
 
   def body_weight
     Quantity.new(read_attribute(:body_weight), :lbs)
app/views/workouts/_workout.html.erb
@@ -1,17 +0,0 @@
-<tr>
-  <td>
-    <%= link_to workout.routine, program_path(workout.program, anchor: workout.routine) %>
-  </td>
-  <td><%= I18n.l workout.occurred_at, format: :short %></td>
-  <td><%= workout.body_weight.to(:lbs) %> lbs</td>
-  <td>
-  <% workout.exercises.order(:created_at).uniq.each do |exercise| %>
-    <strong><%= exercise.name %></strong> @ <%= workout.progress_for(exercise).max_weight %> lbs
-  <% end %>
-  </td>
-  <td>
-    <% if feature_enabled?(:csv_import) %>
-    <%= link_to "edit", edit_workout_path(workout) %>
-    <% end %>
-  </td>
-</tr>
app/views/workouts/index.html.erb
@@ -2,26 +2,36 @@
   <div class="row">
     <div class="large-12 columns">
       <% if feature_enabled?(:csv_import) %>
-      <p class="text-center"> <a href="#" data-reveal-id="sendToStrongLiftsModal"><%= User.human_attribute_name(:import_address) %></a> </p>
+        <p class="text-center">
+          <a href="#" data-reveal-id="sendToStrongLiftsModal">
+            <%= User.human_attribute_name(:import_address) %>
+          </a>
+        </p>
       <% end %>
-      <table>
-        <thead>
-          <tr>
-          <td></td>
-          <td>Date</td>
-          <td>Weight</td>
-          <td></td>
-          <td></td>
-          </tr>
-        </thead>
-        <tbody>
-          <%= render @workouts %>
-        </tbody>
-      </table>
+      <ul class="accordion" data-accordion>
+        <% @workouts.each do |workout| %>
+        <li class="accordion-navigation">
+          <a href="#panel-<%= workout.id %>">
+            <p class="text-center">
+              <%= workout.name %> - <strong><%= I18n.l workout.occurred_at, format: :short %></strong> - <%= workout.body_weight.to(:lbs) %> lbs
+            </p>
+          </a>
+          <div id="panel-<%= workout.id %>" class="content">
+            <% workout.exercises.order(:created_at).uniq.each do |exercise| %>
+              <% progress = workout.progress_for(exercise) %>
+              <p class="text-center">
+              <strong><%= exercise.name %></strong>
+              <%= progress.to_sets.join("/") %> @ <%= progress.max_weight %> lbs
+              </p>
+            <% end %>
+          </div>
+        </li>
+        <% end %>
+      </ul>
     </div>
   </div>
   <div class="row">
-    <div class="large-12 text-center columns">
+    <div class="large-12 columns">
       <%= paginate @workouts, remote: false %>
     </div>
   </div>
app/views/workouts/new.html.erb
@@ -1,6 +1,13 @@
 <div class="row">
   <div class="small-12 columns">
+    <h1>Routine <%= @routine.name %></h1>
+    <% @all_routines.each do |routine| %>
+      <p class="text-right">
+        <%= link_to "Switch to Routine #{routine.name}", new_workout_path(routine_id: routine.id) %>
+      </p>
+    <% end %>
     <%= form_for @workout do |f| %>
+      <%= f.hidden_field :routine_id %>
       <fieldset>
         <legend><%= Workout.human_attribute_name(:body_weight) %></legend>
         <%= f.number_field :body_weight %>
@@ -27,11 +34,8 @@
                 </fieldset>
               <% end %>
             <% end %>
-
-            <%= f.hidden_field :routine_id %>
           </fieldset>
       <% end %>
-      <%= f.hidden_field :routine_id %>
       <%= f.submit "Start", class: "button round right" %>
     <% end %>
   </div>
config/initializers/kaminari.rb
@@ -0,0 +1,10 @@
+Kaminari.configure do |config|
+  # config.default_per_page = 25
+  # config.max_per_page = nil
+  # config.window = 4
+  # config.outer_window = 0
+  # config.left = 0
+  # config.right = 0
+  # config.page_method_name = :page
+  # config.param_name = :page
+end