From addf99f1911ec617ba258e075a53d774dee2a672 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Thu, 14 Sep 2023 03:55:20 +0300 Subject: [PATCH] Move common pagination code to mixin --- .../concerns/pagination_methods.rb | 27 +++++++++++++++++++ app/controllers/diary_entries_controller.rb | 21 +-------------- app/controllers/traces_controller.rb | 16 ++--------- app/views/traces/index.html.erb | 8 +++--- 4 files changed, 34 insertions(+), 38 deletions(-) create mode 100644 app/controllers/concerns/pagination_methods.rb diff --git a/app/controllers/concerns/pagination_methods.rb b/app/controllers/concerns/pagination_methods.rb new file mode 100644 index 000000000..58b522516 --- /dev/null +++ b/app/controllers/concerns/pagination_methods.rb @@ -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 diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb index 1a3d648f4..ff6686448 100644 --- a/app/controllers/diary_entries_controller.rb +++ b/app/controllers/diary_entries_controller.rb @@ -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 diff --git a/app/controllers/traces_controller.rb b/app/controllers/traces_controller.rb index df6337147..3539b6032 100644 --- a/app/controllers/traces_controller.rb +++ b/app/controllers/traces_controller.rb @@ -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 diff --git a/app/views/traces/index.html.erb b/app/views/traces/index.html.erb index 447716a18..5ec9127a8 100644 --- a/app/views/traces/index.html.erb +++ b/app/views/traces/index.html.erb @@ -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 %> @@ -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 %>

<%= t ".empty_title" %>

<%= t ".empty_upload_html", :upload_link => link_to(t(".upload_new"), new_trace_path), -- 2.39.5