diff --git a/Gemfile b/Gemfile index d7ca8502b3b..ed8007fb5b2 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,7 @@ gem 'unicode_utils', '>=1.4.0' gem "lograge" # https://github.com/roidrage/lograge gem 'will_paginate', '>=3.0.2' +gem "pagy", "~> 9.3" gem 'acts_as_list', '~> 0.9.7' gem 'akismetor' diff --git a/Gemfile.lock b/Gemfile.lock index d354f0821ce..8cb4ce7f6da 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -402,6 +402,7 @@ GEM mini_portile2 (~> 2.8.2) racc (~> 1.4) orm_adapter (0.5.0) + pagy (9.3.3) parallel (1.25.1) parser (3.3.0.5) ast (~> 2.4.1) @@ -701,6 +702,7 @@ DEPENDENCIES mysql2 n_plus_one_control nokogiri (>= 1.8.5) + pagy (~> 9.3) permit_yo phraseapp-in-context-editor-ruby (>= 1.0.6) pickle diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 89a98aa6b0c..f1592640c8e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -42,6 +42,30 @@ def sanitize_ac_params end end + include Pagy::Backend + def pagy(collection, **vars) + pagy_overflow_handler do + super + end + end + + def pagy_query_result(query_result, **vars) + pagy_overflow_handler do + Pagy.new( + count: query_result.total_entries, + page: query_result.current_page, + limit: query_result.per_page, + **vars + ) + end + end + + def pagy_overflow_handler(*) + yield + rescue Pagy::OverflowError + nil + end + def display_auth_error respond_to do |format| format.html do diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 32658a750d1..cdccaa91606 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -145,6 +145,13 @@ def index end end set_own_bookmarks + + @pagy = + if @bookmarks.respond_to?(:total_pages) + pagy_query_result(@bookmarks) + elsif @bookmarkable_items.respond_to?(:total_pages) + pagy_query_result(@bookmarkable_items) + end end # GET /:locale/bookmark/:id diff --git a/app/controllers/readings_controller.rb b/app/controllers/readings_controller.rb index 12e1676bdcb..bfe189f0a93 100644 --- a/app/controllers/readings_controller.rb +++ b/app/controllers/readings_controller.rb @@ -16,7 +16,8 @@ def index @readings = @readings.where(toread: true) @page_subtitle = ts("Marked For Later") end - @readings = @readings.order("last_viewed DESC").page(params[:page]) + @readings = @readings.order("last_viewed DESC") + @pagy, @readings = pagy(@readings) end def destroy diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index d87f5fb0596..8e73e26464f 100755 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -131,6 +131,8 @@ def index @works = Work.latest.for_blurb.to_a end set_own_works + + @pagy = pagy_query_result(@works) if @works.respond_to?(:total_pages) end def collected diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2a2cf4b6f0f..554e8c5863f 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -516,18 +516,6 @@ def first_paragraph(full_text, placeholder_text = 'No preview available.') end end - # change the default link renderer for will_paginate - def will_paginate(collection_or_options = nil, options = {}) - if collection_or_options.is_a? Hash - options = collection_or_options - collection_or_options = nil - end - unless options[:renderer] - options = options.merge renderer: PaginationListLinkRenderer - end - super(*[collection_or_options, options].compact) - end - # spans for nesting a checkbox or radio button inside its label to make custom # checkbox or radio designs def label_indicator_and_text(text) diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb new file mode 100644 index 00000000000..cee1c0bfe95 --- /dev/null +++ b/app/helpers/pagination_helper.rb @@ -0,0 +1,66 @@ +module PaginationHelper + include Pagy::Frontend + + # change the default link renderer for will_paginate + def will_paginate(collection_or_options = nil, options = {}) + if collection_or_options.is_a? Hash + options = collection_or_options + collection_or_options = nil + end + options = options.merge renderer: PaginationListLinkRenderer unless options[:renderer] + super(*[collection_or_options, options].compact) + end + + # Cf https://github.com/ddnexus/pagy/blob/master/gem/lib/pagy/frontend.rb + # i18n-tasks-use t("pagy.prev") + # i18n-tasks-use t("pagy.next") + # i18n-tasks-use t("pagy.aria_label.nav") + def pagy_nav(pagy, id: nil, aria_label: nil, **vars) + return nil unless pagy + + # Keep will_paginate behavior of showing nothing if only one page + return nil if pagy.series.length <= 1 + + id = %( id="#{id}") if id + a = pagy_anchor(pagy, **vars) + + html = %(
<%= ts("These are some of the latest bookmarks created on the Archive. To find more bookmarks, #{link_to 'choose a fandom', media_path} or #{link_to 'try our advanced search', search_bookmarks_path}.").html_safe %> <% end %> -<% if @bookmarks.respond_to?(:total_pages) %> - <%= will_paginate @bookmarks %> -<% elsif @bookmarkable_items.respond_to?(:total_pages) %> - <%= will_paginate @bookmarkable_items %> -<% end %> +<%== pagy_nav @pagy %>
<%= ts("These are some of the latest works posted to the Archive. To find more works, #{link_to 'choose a fandom', media_path} or #{link_to 'try our advanced search', search_works_path}.").html_safe %> <% end %> -<% if @works.respond_to?(:total_pages) %> - <%= will_paginate @works %> -<% end %> +<%== pagy_nav @pagy %>