Commit 8916647

mo khan <mo@mokhan.ca>
2017-02-12 22:37:41
bring back the search form and sort lifts.
1 parent fb6c991
Changed files (4)
app/controllers/workouts_controller.rb
@@ -1,6 +1,7 @@
 class WorkoutsController < ApplicationController
   def index
     @exercise = Exercise.find_by(name: params[:exercise])
+    @primary_exercises = Exercise.primary.order_by_name.to_a
     @workouts = paginate(recent_workouts(@exercise), per_page: 24)
   end
 
app/helpers/application_helper.rb
@@ -18,7 +18,7 @@ module ApplicationHelper
     remote: @remote_search
   )
     form_tag path, id: id, method: :get, remote: remote do
-      search_field_tag :q, params[:q], placeholder: t(:search)
+      search_field_tag :q, params[:q], placeholder: t(:search), class: 'input'
     end
   end
 
app/models/exercise.rb
@@ -1,7 +1,8 @@
 class Exercise < ApplicationRecord
   PRIMARY_LIFTS=["Squat", "Bench Press", "Barbell Row", "Overhead Press", "Deadlift"]
 
-  scope :primary, ->() { where(name: PRIMARY_LIFTS).order(name: :desc) }
+  scope :primary, ->() { where(name: PRIMARY_LIFTS) }
+  scope :order_by_name, ->() { order(name: :asc) }
 
   def short_name
     name.gsub(/[^A-Z]/, "")
app/views/workouts/index.html.erb
@@ -2,26 +2,53 @@
   <div class="container">
     <div class="columns">
       <div class="column is-12">
-        <div class="tabs">
-          <ul>
-            <li class="<%= 'is-active' if @exercise.nil? %>">
-              <%= link_to 'All', workouts_path %>
-            </li>
+        <nav class="level">
+          <!-- Left side -->
+          <div class="level-left">
+            <div class="level-item">
+              <%= search_form %>
+            </div>
+            <div class="level-item">
+              <p class="subtitle is-5">
+                <strong><%= @workouts.total_count %></strong> workouts
+              </p>
+            </div>
+          </div>
+
+          <!-- Right side -->
+          <div class="level-right">
+            <p class="level-item">
+              <% if @exercise.nil? %>
+                <strong> <%= link_to 'All', workouts_path %> </strong>
+              <% else %>
+                <%= link_to 'All', workouts_path %>
+              <% end %>
+            </p>
             <% Exercise::PRIMARY_LIFTS.each do |name| %>
-              <li class="<%= 'is-active' if name == @exercise.try(:name) %>">
+              <p class="level-item">
+              <% if name == @exercise.try(:name) %>
+                <strong><%= link_to name, workouts_path(exercise: name) %></strong>
+              <% else %>
                 <%= link_to name, workouts_path(exercise: name) %>
-              </li>
+              <% end %>
+              </p>
             <% end %>
-          </ul>
-        </div>
+            <p class="level-item"><a class="button is-success">New</a></p>
+          </div>
+        </nav>
+      </div>
+    </div>
+
+    <div class="columns">
+      <div class="column is-12">
         <table class="table is-striped">
           <thead>
             <tr>
               <th><abbr title="Routine"></abbr>Routine</th>
               <th><abbr title="Date"></abbr>Date</th>
               <th><abbr title="Body weight"></abbr>Body weight</th>
-              <% Exercise::PRIMARY_LIFTS.each do |exercise_name| %>
-                <th><abbr title="<%= exercise_name %>"></abbr><%= exercise_name %></th>
+              <% @primary_exercises.each do |exercise| %>
+                <th><abbr title="<%= exercise.name %>"></abbr><%= exercise.name %></th>
               <% end %>
             </tr>
           </thead>
@@ -32,7 +59,7 @@
                   <td> <%= workout.name %> </td>
                   <td> <%= I18n.l workout.occurred_at, format: :short %> </td>
                   <td> <%= workout.body_weight.to(:lbs) %> lbs </td>
-                  <% Exercise.primary.find_each do |exercise| %>
+                  <% @primary_exercises.each do |exercise| %>
                     <td> <%= workout.progress_for(exercise).status %> </td>
                   <% end %>
                 </tr>