Commit 5eb775c

mo khan <mo@mokhan.ca>
2017-02-12 02:22:30
sort by exercise.
1 parent f72ecaa
Changed files (2)
app
app/controllers/workouts_controller.rb
@@ -1,6 +1,7 @@
 class WorkoutsController < ApplicationController
   def index
-    @workouts = paginate(recent_workouts, per_page: 24)
+    @exercise = Exercise.find_by(name: params[:exercise])
+    @workouts = paginate(recent_workouts(@exercise), per_page: 24)
   end
 
   def new
@@ -36,8 +37,12 @@ class WorkoutsController < ApplicationController
     )
   end
 
-  def recent_workouts
-    current_user.workouts.recent.includes(:routine)
+  def recent_workouts(exercise)
+    if exercise.present?
+      current_user.workouts.recent.includes(:routine).with_exercise(exercise)
+    else
+      current_user.workouts.recent.includes(:routine)
+    end
   end
 
   def find_routine(routine_id)
app/views/workouts/index.html.erb
@@ -3,6 +3,18 @@
     <div class="columns">
       <div class="column is-12">
         <% exercises = ["Squat", "Bench Press", "Barbell Row", "Overhead Press", "Deadlift"]  %>
+        <div class="tabs">
+          <ul>
+            <li class="<%= 'is-active' if @exercise.nil? %>">
+              <%= link_to 'All', workouts_path %>
+            </li>
+            <% exercises.each do |name| %>
+              <li class="<%= 'is-active' if name == @exercise.try(:name) %>">
+                <%= link_to name, workouts_path(exercise: name) %>
+              </li>
+            <% end %>
+          </ul>
+        </div>
         <table class="table is-striped">
           <thead>
             <tr>