]> git.openstreetmap.org Git - rails.git/commitdiff
Move common pagination code to mixin
authorAnton Khorev <tony29@yandex.ru>
Thu, 14 Sep 2023 00:55:20 +0000 (03:55 +0300)
committerAnton Khorev <tony29@yandex.ru>
Mon, 25 Mar 2024 23:36:28 +0000 (02:36 +0300)
app/controllers/concerns/pagination_methods.rb [new file with mode: 0644]
app/controllers/diary_entries_controller.rb
app/controllers/traces_controller.rb
app/views/traces/index.html.erb

diff --git a/app/controllers/concerns/pagination_methods.rb b/app/controllers/concerns/pagination_methods.rb
new file mode 100644 (file)
index 0000000..58b5225
--- /dev/null
@@ -0,0 +1,27 @@
+module PaginationMethods
+  extend ActiveSupport::Concern
+
+  private
+
+  ##
+  # limit selected items to one page, get ids of first item before/after the page
+  def get_page_items(items, includes)
+    id_column = "#{items.table_name}.id"
+    page_items = if params[:before]
+                   items.where("#{id_column} < ?", params[:before]).order(:id => :desc)
+                 elsif params[:after]
+                   items.where("#{id_column} > ?", params[:after]).order(:id => :asc)
+                 else
+                   items.order(:id => :desc)
+                 end
+
+    page_items = page_items.limit(20)
+    page_items = page_items.includes(includes)
+    page_items = page_items.sort.reverse
+
+    newer_items_id = page_items.first.id if page_items.count.positive? && items.exists?(["#{id_column} > ?", page_items.first.id])
+    older_items_id = page_items.last.id if page_items.count.positive? && items.exists?(["#{id_column} < ?", page_items.last.id])
+
+    [page_items, newer_items_id, older_items_id]
+  end
+end
index 1a3d648f460a4f089ca362ac00c9ac88efc7915e..ff668644894e9e2a972809c4d0fd7b20e94bf51a 100644 (file)
@@ -1,5 +1,6 @@
 class DiaryEntriesController < ApplicationController
   include UserMethods
+  include PaginationMethods
 
   layout "site", :except => :rss
 
@@ -282,24 +283,4 @@ class DiaryEntriesController < ApplicationController
       @zoom = 12
     end
   end
-
-  def get_page_items(items, includes)
-    id_column = "#{items.table_name}.id"
-    page_items = if params[:before]
-                   items.where("#{id_column} < ?", params[:before]).order(:id => :desc)
-                 elsif params[:after]
-                   items.where("#{id_column} > ?", params[:after]).order(:id => :asc)
-                 else
-                   items.order(:id => :desc)
-                 end
-
-    page_items = page_items.limit(20)
-    page_items = page_items.includes(includes)
-    page_items = page_items.sort.reverse
-
-    newer_items_id = page_items.first.id if page_items.count.positive? && items.exists?(["#{id_column} > ?", page_items.first.id])
-    older_items_id = page_items.last.id if page_items.count.positive? && items.exists?(["#{id_column} < ?", page_items.last.id])
-
-    [page_items, newer_items_id, older_items_id]
-  end
 end
index df6337147566538b89d9fa7b3c76f36b127ce182..3539b6032a35624911725b11ba9fc5fdd6591f4f 100644 (file)
@@ -1,5 +1,6 @@
 class TracesController < ApplicationController
   include UserMethods
+  include PaginationMethods
 
   layout "site", :except => :georss
 
@@ -60,20 +61,7 @@ class TracesController < ApplicationController
 
     @params = params.permit(:display_name, :tag, :before, :after)
 
-    @traces = if params[:before]
-                traces.where("gpx_files.id < ?", params[:before]).order(:id => :desc)
-              elsif params[:after]
-                traces.where("gpx_files.id > ?", params[:after]).order(:id => :asc)
-              else
-                traces.order(:id => :desc)
-              end
-
-    @traces = @traces.limit(20)
-    @traces = @traces.includes(:user, :tags)
-    @traces = @traces.sort.reverse
-
-    @newer_traces = @traces.count.positive? && traces.exists?(["gpx_files.id > ?", @traces.first.id])
-    @older_traces = @traces.count.positive? && traces.exists?(["gpx_files.id < ?", @traces.last.id])
+    @traces, @newer_traces_id, @older_traces_id = get_page_items(traces, [:user, :tags])
 
     # final helper vars for view
     @target_user = target_user
index 447716a18230412186b63f3a4b53e7377599d2cb..5ec9127a8663cb8365580cf34e4571792fb9cb8f 100644 (file)
@@ -68,8 +68,8 @@
   <%= render "shared/pagination",
              :newer_key => "traces.trace_paging_nav.newer",
              :older_key => "traces.trace_paging_nav.older",
-             :newer_id => @newer_traces && @traces.first.id,
-             :older_id => @older_traces && @traces.last.id %>
+             :newer_id => @newer_traces_id,
+             :older_id => @older_traces_id %>
 
   <table id="trace_list" class="table table-borderless table-striped">
     <tbody>
@@ -80,8 +80,8 @@
   <%= render "shared/pagination",
              :newer_key => "traces.trace_paging_nav.newer",
              :older_key => "traces.trace_paging_nav.older",
-             :newer_id => @newer_traces && @traces.first.id,
-             :older_id => @older_traces && @traces.last.id %>
+             :newer_id => @newer_traces_id,
+             :older_id => @older_traces_id %>
 <% else %>
   <h2><%= t ".empty_title" %></h2>
   <p><%= t ".empty_upload_html", :upload_link => link_to(t(".upload_new"), new_trace_path),