Commit 188cd5d

mo khan <mo@mokhan.ca>
2017-02-12 22:15:24
load graph one at a time using xhr.
1 parent ebd397d
Changed files (6)
app/controllers/graphs_controller.rb
@@ -0,0 +1,5 @@
+class GraphsController < ApplicationController
+  def show
+    @training_history = current_user.history_for(Exercise.find_by(id: params[:id]))
+  end
+end
app/models/training_history.rb
@@ -7,6 +7,10 @@ class TrainingHistory
     @exercise = exercise
   end
 
+  def cache_key
+    [ user.to_param, exercise.to_param, sets.count ]
+  end
+
   def personal_record
     sets.successful.maximum(:target_weight)
   end
app/views/graphs/show.js.erb
@@ -0,0 +1,1 @@
+$('#graph-panel').html("<%= escape_javascript(render @training_history) %>");
app/views/profiles/show.html.erb
@@ -26,7 +26,7 @@
           <% personal_record = history.personal_record %>
           <div class="level-item has-text-centered">
             <div>
-              <p class="heading"><%= history.exercise.name %></p>
+              <p class="heading"><%= link_to history.exercise.name, graph_path(history.exercise), remote: true %></p>
               <p class="title"><%= personal_record ? "#{personal_record} lbs" : t('.not_available') %></p>
             </div>
           </div>
@@ -36,8 +36,7 @@
   </div>
 
   <div class="columns">
-    <div class="column is-12 content">
-      <%= render @training_histories %>
+    <div id="graph-panel" class="column is-12 content">
     </div>
   </div>
 </div>
app/views/training_histories/_training_history.html.erb
@@ -1,4 +1,6 @@
-<% if training_history.completed_any? %>
-  <p class="has-text-centered"> <%= training_history.exercise.name %> </p>
-  <%= line_chart training_history.to_line_chart %>
+<% cache training_history do %>
+  <% if training_history.completed_any? %>
+    <p class="has-text-centered"> <%= training_history.exercise.name %> </p>
+    <%= line_chart training_history.to_line_chart %>
+  <% end %>
 <% end %>
config/routes.rb
@@ -7,6 +7,7 @@ Rails.application.routes.draw do
   resources :programs, only: [:show]
   resources :profiles, only: [:new, :create, :show, :edit, :update], constraints: { id: /[^\/]+/ }
   resources :gyms, only: [:index, :show, :new, :create]
+  resources :graphs, only: [:show]
   get "/u/:id" => "profiles#show", constraints: { id: /[^\/]+/ }
   get "/dashboard" => "workouts#index", as: :dashboard
   get "/terms" => "static_pages#terms"