Commit 2127ede

mo khan <mo@mokhan.ca>
2017-02-18 03:32:01
remove pagination and load based on time.
1 parent b59191d
Changed files (4)
app/controllers/workouts_controller.rb
@@ -2,9 +2,10 @@ class WorkoutsController < ApplicationController
   before_action { @search_path = workouts_path }
 
   def index
+    @ranges = [5.years, 1.year, 6.months, 3.months, 1.month, 2.weeks, 1.week].reverse
     @exercise = Exercise.find_by(name: params[:exercise])
     @primary_exercises = Exercise.primary.order_by_name.to_a
-    @workouts = paginate(recent_workouts(@exercise), per_page: 24)
+    @workouts = recent_workouts(@exercise)
   end
 
   def new
@@ -40,8 +41,8 @@ class WorkoutsController < ApplicationController
     )
   end
 
-  def recent_workouts(exercise)
-    workouts = current_user.workouts.recent.includes(:routine)
+  def recent_workouts(exercise, since = (params[:since] || 30.days.to_i).to_i.seconds.ago)
+    workouts = current_user.workouts.since(since).recent.includes(:routine)
     exercise ? workouts.with_exercise(exercise) : workouts
   end
 
app/models/workout.rb
@@ -9,6 +9,7 @@ class Workout < ApplicationRecord
   delegate :name, to: :routine
   alias_method :sets, :exercise_sets
 
+  scope :since, ->(since) { where('occurred_at > ?', since) }
   scope :recent, -> { order(occurred_at: :desc) }
   scope :with_exercise, ->(exercise) do
     joins(:exercises).where(exercises: { id: exercise.id }).distinct
app/views/workouts/index.html.erb
@@ -9,9 +9,18 @@
           </div>
           <div class="level-item">
             <p class="subtitle is-5">
-            <strong><%= @workouts.total_count %></strong> workouts
+            <strong><%= @workouts.count %></strong> workouts
             </p>
           </div>
+          <div class="level-item">
+            <%= form_tag @search_path, method: :get do %>
+              <p class="control">
+                <span class="select">
+                  <%= select_tag :since, options_for_select(@ranges.map { |x| [time_ago_in_words(x.ago), x.to_i] }) %>
+                </span>
+              </p>
+            <% end %>
+          </div>
         </div>
 
         <!-- Right side -->
@@ -77,7 +86,13 @@
   </div>
   <div class="columns">
     <div class="column is-12">
-      <%= paginate @workouts, remote: false %>
+      <%# paginate @workouts, remote: false %>
     </div>
   </div>
 </div>
+
+<% content_for :javascript do %>
+  $('.select').on('change', function(event) {
+    $(event.target).parents('form:first').submit();
+  });
+<% end %>
spec/controllers/workouts_controller_spec.rb
@@ -9,13 +9,18 @@ describe WorkoutsController do
 
   describe "#index" do
     include_context "stronglifts_program"
-    let!(:workout_a) { create(:workout, user: user, routine: routine_a) }
-    let!(:workout_b) { create(:workout, user: user, routine: routine_b) }
+    let!(:workout_a) { create(:workout, user: user, routine: routine_a, occurred_at: 1.week.ago) }
+    let!(:workout_b) { create(:workout, user: user, routine: routine_b, occurred_at: 1.day.ago) }
 
     it "loads all my workouts" do
       get :index
       expect(assigns(:workouts)).to match_array([workout_a, workout_b])
     end
+
+    it "loads all works since a given time" do
+      get :index, since: 2.days.to_i
+      expect(assigns(:workouts)).to match_array([workout_b])
+    end
   end
 
   describe "#new" do