Commit 60bc351

mo khan <mo@mokhan.ca>
2017-02-27 03:25:36
load data for each month as json.
1 parent fb76437
Changed files (3)
app/controllers/workouts_controller.rb
@@ -9,7 +9,19 @@ class WorkoutsController < ApplicationController
   end
 
   def calendar
-    @workouts = current_user.workouts.recent.includes(:routine)
+    respond_to do |format|
+      format.html
+      format.json do
+        workouts = current_user.workouts.recent.before(params[:end]).after(params[:start]).includes(:routine)
+        results = workouts.map do |workout|
+          {
+            title: workout.routine.name,
+            start: workout.occurred_at.strftime("%Y-%m-%d")
+          }
+        end
+        render json: results.to_json
+      end
+    end
   end
 
   def new
app/models/workout.rb
@@ -9,7 +9,9 @@ class Workout < ApplicationRecord
   delegate :name, to: :routine
   alias_method :sets, :exercise_sets
 
-  scope :since, ->(date) { where('occurred_at > ?', date) }
+  scope :since, ->(date) { after(date) }
+  scope :before, ->(date) { where('occurred_at < ?', date) }
+  scope :after, ->(date) { where('occurred_at > ?', date) }
   scope :recent, -> { order(occurred_at: :desc) }
   scope :with_exercise, ->(exercise) do
     joins(:exercises).where(exercises: { id: exercise.id }).distinct
app/views/workouts/calendar.html.erb
@@ -9,7 +9,7 @@
 <% content_for :javascript do %>
   $(function(){
     $('#calendar').fullCalendar({
-      events: <%= raw @workouts.map { |x| { title: x.routine.name, start: x.occurred_at.strftime("%Y-%m-%d") } }.to_json %>
+      events: '<%= calendar_workouts_path(format: :json) %>'
     });
   });
 <% end %>