From 274a516bc6b84b8f0f907bbc129983035411d877 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Tue, 26 Mar 2024 04:20:59 +0300 Subject: [PATCH 1/1] Make "include" and "limit" keyword args of pagination method --- app/controllers/concerns/pagination_methods.rb | 4 ++-- app/controllers/diary_entries_controller.rb | 4 ++-- app/controllers/traces_controller.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/concerns/pagination_methods.rb b/app/controllers/concerns/pagination_methods.rb index 58b522516..3dc9f52aa 100644 --- a/app/controllers/concerns/pagination_methods.rb +++ b/app/controllers/concerns/pagination_methods.rb @@ -5,7 +5,7 @@ module PaginationMethods ## # limit selected items to one page, get ids of first item before/after the page - def get_page_items(items, includes) + def get_page_items(items, includes: [], limit: 20) id_column = "#{items.table_name}.id" page_items = if params[:before] items.where("#{id_column} < ?", params[:before]).order(:id => :desc) @@ -15,7 +15,7 @@ module PaginationMethods items.order(:id => :desc) end - page_items = page_items.limit(20) + page_items = page_items.limit(limit) page_items = page_items.includes(includes) page_items = page_items.sort.reverse diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb index ff6686448..8da0842eb 100644 --- a/app/controllers/diary_entries_controller.rb +++ b/app/controllers/diary_entries_controller.rb @@ -58,7 +58,7 @@ class DiaryEntriesController < ApplicationController @params = params.permit(:display_name, :friends, :nearby, :language) - @entries, @newer_entries_id, @older_entries_id = get_page_items(entries, [:user, :language]) + @entries, @newer_entries_id, @older_entries_id = get_page_items(entries, :includes => [:user, :language]) end def show @@ -247,7 +247,7 @@ class DiaryEntriesController < ApplicationController @params = params.permit(:display_name, :before, :after) - @comments, @newer_comments_id, @older_comments_id = get_page_items(comments, [:user]) + @comments, @newer_comments_id, @older_comments_id = get_page_items(comments, :includes => [:user]) end private diff --git a/app/controllers/traces_controller.rb b/app/controllers/traces_controller.rb index 3539b6032..42aea8299 100644 --- a/app/controllers/traces_controller.rb +++ b/app/controllers/traces_controller.rb @@ -61,7 +61,7 @@ class TracesController < ApplicationController @params = params.permit(:display_name, :tag, :before, :after) - @traces, @newer_traces_id, @older_traces_id = get_page_items(traces, [:user, :tags]) + @traces, @newer_traces_id, @older_traces_id = get_page_items(traces, :includes => [:user, :tags]) # final helper vars for view @target_user = target_user -- 2.39.5