Commit b863966

mo khan <mo@mokhan.ca>
2017-02-18 17:30:36
load chart via xhr.
1 parent 639a64e
Changed files (5)
app/controllers/charts_controller.rb
@@ -0,0 +1,16 @@
+class ChartsController < ApplicationController
+  def index
+    exercise = Exercise.find_by(id: params[:exercise])
+    respond_to do |format|
+      format.html { @training_history = current_user.history_for(exercise) }
+      format.json { render json: recent_workouts(exercise).to_line_chart }
+    end
+  end
+
+  private
+
+  def recent_workouts(exercise, since = (params[:since] || 7.days).to_i.seconds.ago)
+    workouts = current_user.workouts.since(since.beginning_of_day).recent
+    exercise ? workouts.with_exercise(exercise) : workouts
+  end
+end
app/controllers/graphs_controller.rb
@@ -1,5 +0,0 @@
-class GraphsController < ApplicationController
-  def show
-    @training_history = current_user.history_for(Exercise.find_by(id: params[:id]))
-  end
-end
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"><%= link_to history.exercise.name, graph_path(history.exercise), remote: true %></p>
+              <p class="heading"><%= link_to history.exercise.name, charts_path(exercise: history.exercise), remote: true %></p>
               <p class="title"><%= personal_record ? "#{personal_record} lbs" : t('.not_available') %></p>
             </div>
           </div>
app/views/workouts/index.html.erb
@@ -13,8 +13,11 @@
             </p>
           </div>
           <div class="level-item">
-            <%= form_tag workouts_path(filter_params_hash.except(:since)), method: :get do %>
+            <%= form_tag workouts_path(filter_params_hash), method: :get do %>
               <p class="control">
+                <% filter_params_hash.each do |key, value| %>
+                  <%= hidden_field_tag key, value %>
+                <% end %>
                 <span class="select">
                   <%= select_tag :since, options_for_select(@ranges.map { |x| [time_ago_in_words(x.ago), x.to_i] }, params[:since]) %>
                 </span>
@@ -49,7 +52,7 @@
   <% if @exercise %>
     <div class="columns">
       <div class="column is-12">
-        <%= line_chart [{ name: @exercise.name, data: @workouts.with_exercise(@exercise).to_line_chart }] %>
+        <%= line_chart charts_path(filter_params_hash(exercise: @exercise.to_param)) %>
       </div>
     </div>
   <% end %>
config/routes.rb
@@ -7,7 +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]
+  resources :charts, only: [:index]
   resource :dashboards, only: [:show]
   get "/u/:id" => "profiles#show", constraints: { id: /[^\/]+/ }
   get "/dashboard" => "dashboards#show", as: :dashboard