From e6aad782deabe957051bfe22fddc0edd587fe67c Mon Sep 17 00:00:00 2001 From: Ceithir Date: Wed, 5 Feb 2025 17:23:21 +0100 Subject: [PATCH 01/16] Replace will_paginate with pagy for works Raw first attempt --- Gemfile | 1 + Gemfile.lock | 2 ++ app/controllers/application_controller.rb | 5 +++++ app/controllers/works_controller.rb | 4 ++++ app/helpers/application_helper.rb | 1 + app/views/works/index.html.erb | 4 ++-- config/initializers/gem-plugin_config/pagy.rb | 5 +++++ 7 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 config/initializers/gem-plugin_config/pagy.rb diff --git a/Gemfile b/Gemfile index d7ca8502b3b..06d98920da7 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..ce765617039 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -42,6 +42,11 @@ def sanitize_ac_params end end + include Pagy::Backend + def pagy_query_result(query_result, vars = {}) + Pagy.new(count: query_result.total_entries, page: params[:page], **vars) + end + def display_auth_error respond_to do |format| format.html do diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index d87f5fb0596..02f293d212c 100755 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -131,6 +131,10 @@ def index @works = Work.latest.for_blurb.to_a end set_own_works + + if @works.respond_to?(:total_pages) + @pagy = pagy_query_result(@works) + end end def collected diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2a2cf4b6f0f..8ba187f8f15 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,7 @@ # Methods added to this helper will be available to all templates in the application. module ApplicationHelper include HtmlCleaner + include Pagy::Frontend # TODO: Official recommendation from Rails indicates we should switch to # unobtrusive JavaScript instead of using anything like `link_to_function` diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index bf4bec88222..9a5c5ec0d87 100755 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -64,6 +64,6 @@ <% end %> -<% if @works.respond_to?(:total_pages) %> - <%= will_paginate @works %> +<% if @pagy %> + <%== pagy_nav @pagy %> <% end %> diff --git a/config/initializers/gem-plugin_config/pagy.rb b/config/initializers/gem-plugin_config/pagy.rb new file mode 100644 index 00000000000..a4789152b99 --- /dev/null +++ b/config/initializers/gem-plugin_config/pagy.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +Pagy::DEFAULT[:limit] = ArchiveConfig.ITEMS_PER_PAGE + +Pagy::DEFAULT.freeze From 0d3b62809e7bacc17cccbeff184de9f42c481a58 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Wed, 5 Feb 2025 18:42:43 +0100 Subject: [PATCH 02/16] Reproduce previous HTML --- app/helpers/application_helper.rb | 47 ++++++++++++++++++- app/views/works/index.html.erb | 4 +- config/initializers/gem-plugin_config/pagy.rb | 2 + config/locales/views/en.yml | 5 ++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 8ba187f8f15..0418ab051db 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,7 +1,6 @@ # Methods added to this helper will be available to all templates in the application. module ApplicationHelper include HtmlCleaner - include Pagy::Frontend # TODO: Official recommendation from Rails indicates we should switch to # unobtrusive JavaScript instead of using anything like `link_to_function` @@ -529,6 +528,52 @@ def will_paginate(collection_or_options = nil, options = {}) super(*[collection_or_options, options].compact) end + include Pagy::Frontend + # Cf https://github.com/ddnexus/pagy/blob/master/gem/lib/pagy/frontend.rb + def pagy_nav(pagy, id: nil, aria_label: nil, **vars) + id = %( id="#{id}") if id + a = pagy_anchor(pagy, **vars) + + html = %(

#{t("pagination.title")}

) + + html << %() + + prev_text = t("pagination.previous") + prev_a = + if (p_prev = pagy.prev) + a.(p_prev, prev_text) + else + %(#{prev_text}) + end + html << %() + + pagy.series(**vars).each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] + html << %(
  • ) + html << case item + when Integer + a.(item) + when String + %(#{pagy.label_for(item)}) + when :gap + %(#{pagy_t('pagy.gap')}) + else + raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}" + end + html << %(
  • ) + end + + next_text = t("pagination.next") + next_a = + if (p_next = pagy.next) + a.(p_next, next_text) + else + %(#{next_text}) + end + html << %() + + html << %() + 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/views/works/index.html.erb b/app/views/works/index.html.erb index 9a5c5ec0d87..59c6145b43b 100755 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -46,8 +46,8 @@

    <%= 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 %> +<% if @pagy %> + <%== pagy_nav @pagy %> <% end %> diff --git a/config/initializers/gem-plugin_config/pagy.rb b/config/initializers/gem-plugin_config/pagy.rb index a4789152b99..809f8717ed2 100644 --- a/config/initializers/gem-plugin_config/pagy.rb +++ b/config/initializers/gem-plugin_config/pagy.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +# See https://ddnexus.github.io/pagy/docs/api/pagy#variables Pagy::DEFAULT[:limit] = ArchiveConfig.ITEMS_PER_PAGE +Pagy::DEFAULT[:size] = 9 Pagy::DEFAULT.freeze diff --git a/config/locales/views/en.yml b/config/locales/views/en.yml index 4cbc8c0438a..5da03e262db 100644 --- a/config/locales/views/en.yml +++ b/config/locales/views/en.yml @@ -1565,6 +1565,11 @@ en: orphan_user: orphaning_bylines_only_message_html: Orphaning will automatically remove your personal information from bylines only. It will not automatically remove your personal information from anywhere else in the work(s). Social media accounts, contact information, email addresses, names, and any other personal information posted in the title, summary, notes, tags, work body, or comment text will not be automatically removed. Comments posted by other users will also not be affected by Orphaning. If you want information removed from these places, you should edit or delete it prior to Orphaning. Once you Orphan the work(s), you will no longer be able to delete information in these places yourself. orphaning_works_message_html: 'Orphaning will permanently remove your username and/or pseud from the bylines of: the following work(s), their chapters, associated series, and any comments you may have left on them while logged into this account.' + pagination: + aria-label: Pagination + next: Next → + previous: "← Previous" + title: Pages Navigation preferences: index: allow_collection_invitation: Allow others to invite my works to collections. From abf1a5196a4007decf1cc6bd4860d4d8cce4ddc7 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Thu, 6 Feb 2025 17:58:21 +0100 Subject: [PATCH 03/16] Simpler use code --- app/helpers/application_helper.rb | 5 +++++ app/views/works/index.html.erb | 8 ++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0418ab051db..a2aff4c8714 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -531,6 +531,11 @@ def will_paginate(collection_or_options = nil, options = {}) include Pagy::Frontend # Cf https://github.com/ddnexus/pagy/blob/master/gem/lib/pagy/frontend.rb 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) diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 59c6145b43b..8ff441f4172 100755 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -46,9 +46,7 @@

    <%= 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 @pagy %> - <%== pagy_nav @pagy %> -<% end %> +<%== pagy_nav @pagy %>

    <%= ts("Listing Works") %>

    @@ -64,6 +62,4 @@ <% end %> -<% if @pagy %> - <%== pagy_nav @pagy %> -<% end %> +<%== pagy_nav @pagy %> From 83a85304526cdc2256977ad8c196f0e8a36e06da Mon Sep 17 00:00:00 2001 From: Ceithir Date: Thu, 6 Feb 2025 18:07:15 +0100 Subject: [PATCH 04/16] Use for bookmarks --- app/controllers/bookmarks_controller.rb | 9 +++++++++ app/views/bookmarks/index.html.erb | 12 ++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 32658a750d1..55376d5f07e 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -145,6 +145,15 @@ 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) + else + nil + end end # GET /:locale/bookmark/:id diff --git a/app/views/bookmarks/index.html.erb b/app/views/bookmarks/index.html.erb index 0eac4c5be70..2ada8707eb7 100755 --- a/app/views/bookmarks/index.html.erb +++ b/app/views/bookmarks/index.html.erb @@ -49,11 +49,7 @@

    <%= 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("List of Bookmarks") %>

    @@ -71,9 +67,5 @@ -<% if @bookmarks.respond_to?(:total_pages) %> - <%= will_paginate @bookmarks %> -<% elsif @bookmarkable_items.respond_to?(:total_pages) %> - <%= will_paginate @bookmarkable_items %> -<% end %> +<%== pagy_nav @pagy %> From dd58c8e3b29491cf4a4a1f316038e1d081f03c64 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Thu, 6 Feb 2025 18:19:53 +0100 Subject: [PATCH 05/16] Add pagy to history --- app/controllers/readings_controller.rb | 3 ++- app/views/readings/index.html.erb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/readings_controller.rb b/app/controllers/readings_controller.rb index 12e1676bdcb..0c1cd5ff903 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.order("last_viewed DESC") + @pagy, @readings = pagy(@readings) end def destroy diff --git a/app/views/readings/index.html.erb b/app/views/readings/index.html.erb index 9d0e236cd50..90d1969dcd3 100644 --- a/app/views/readings/index.html.erb +++ b/app/views/readings/index.html.erb @@ -30,5 +30,5 @@ -<%= will_paginate @readings %> +<%== pagy_nav @pagy %> From 18fd9f6466b585c94db86758d99aba5bbc513dca Mon Sep 17 00:00:00 2001 From: Ceithir Date: Thu, 6 Feb 2025 19:42:43 +0100 Subject: [PATCH 06/16] Use query_result values, not defaults --- app/controllers/application_controller.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ce765617039..3d47d3ab54d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -44,7 +44,12 @@ def sanitize_ac_params include Pagy::Backend def pagy_query_result(query_result, vars = {}) - Pagy.new(count: query_result.total_entries, page: params[:page], **vars) + Pagy.new( + count: query_result.total_entries, + page: query_result.current_page, + limit: query_result.per_page, + **vars + ) end def display_auth_error From 3fd4490d8ff9a65a7028675c8c949e1edc9d3211 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Fri, 7 Feb 2025 07:06:13 +0100 Subject: [PATCH 07/16] No ! means immutable ... Most of the time --- app/controllers/readings_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/readings_controller.rb b/app/controllers/readings_controller.rb index 0c1cd5ff903..bfe189f0a93 100644 --- a/app/controllers/readings_controller.rb +++ b/app/controllers/readings_controller.rb @@ -16,7 +16,7 @@ def index @readings = @readings.where(toread: true) @page_subtitle = ts("Marked For Later") end - @readings.order("last_viewed DESC") + @readings = @readings.order("last_viewed DESC") @pagy, @readings = pagy(@readings) end From c06b383a6c7cc346c6c408e6c7f655c656c24a3c Mon Sep 17 00:00:00 2001 From: Ceithir Date: Fri, 7 Feb 2025 07:32:32 +0100 Subject: [PATCH 08/16] Rescue overflow --- app/controllers/application_controller.rb | 28 ++++++++++++++++++----- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3d47d3ab54d..558c78d0976 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -43,13 +43,29 @@ def sanitize_ac_params end include Pagy::Backend + def pagy(collection, **vars) + pagy_overflow_handler do + super + end + end + def pagy_query_result(query_result, vars = {}) - Pagy.new( - count: query_result.total_entries, - page: query_result.current_page, - limit: query_result.per_page, - **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(&block) + begin + yield + rescue Pagy::OverflowError + nil + end end def display_auth_error From 2e7f96b1ee7aa38a73421b7eaf256e8eba6858e2 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Fri, 7 Feb 2025 07:41:42 +0100 Subject: [PATCH 09/16] Consistency --- app/controllers/application_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 558c78d0976..0124857f832 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -49,7 +49,7 @@ def pagy(collection, **vars) end end - def pagy_query_result(query_result, vars = {}) + def pagy_query_result(query_result, **vars) pagy_overflow_handler do Pagy.new( count: query_result.total_entries, From c52785f902aa89b9711ab574ce9a68de46e99191 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Fri, 7 Feb 2025 07:57:15 +0100 Subject: [PATCH 10/16] Cop --- Gemfile | 2 +- app/controllers/application_controller.rb | 10 ++++------ app/controllers/bookmarks_controller.rb | 2 -- app/controllers/works_controller.rb | 4 +--- app/helpers/application_helper.rb | 10 +++++----- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Gemfile b/Gemfile index 06d98920da7..ed8007fb5b2 100644 --- a/Gemfile +++ b/Gemfile @@ -37,7 +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 "pagy", "~> 9.3" gem 'acts_as_list', '~> 0.9.7' gem 'akismetor' diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0124857f832..f1592640c8e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -60,12 +60,10 @@ def pagy_query_result(query_result, **vars) end end - def pagy_overflow_handler(&block) - begin - yield - rescue Pagy::OverflowError - nil - end + def pagy_overflow_handler(*) + yield + rescue Pagy::OverflowError + nil end def display_auth_error diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index 55376d5f07e..cdccaa91606 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -151,8 +151,6 @@ def index pagy_query_result(@bookmarks) elsif @bookmarkable_items.respond_to?(:total_pages) pagy_query_result(@bookmarkable_items) - else - nil end end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 02f293d212c..8e73e26464f 100755 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -132,9 +132,7 @@ def index end set_own_works - if @works.respond_to?(:total_pages) - @pagy = pagy_query_result(@works) - end + @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 a2aff4c8714..12549e58f30 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -539,14 +539,14 @@ def pagy_nav(pagy, id: nil, aria_label: nil, **vars) id = %( id="#{id}") if id a = pagy_anchor(pagy, **vars) - html = %(

    #{t("pagination.title")}

    ) + html = %(

    #{t('pagination.title')}

    ) - html << %() + html << %() prev_text = t("pagination.previous") prev_a = if (p_prev = pagy.prev) - a.(p_prev, prev_text) + a.call(p_prev, prev_text) else %(#{prev_text}) end @@ -556,7 +556,7 @@ def pagy_nav(pagy, id: nil, aria_label: nil, **vars) html << %(
  • ) html << case item when Integer - a.(item) + a.call(item) when String %(#{pagy.label_for(item)}) when :gap @@ -570,7 +570,7 @@ def pagy_nav(pagy, id: nil, aria_label: nil, **vars) next_text = t("pagination.next") next_a = if (p_next = pagy.next) - a.(p_next, next_text) + a.call(p_next, next_text) else %(#{next_text}) end From 2708f8528529aec7bb45b65fdf6f3114ed0b1a25 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Sun, 9 Feb 2025 09:07:16 +0100 Subject: [PATCH 11/16] Ignore Brakeman's warning https://ddnexus.github.io/pagy/docs/how-to/#ignore-brakeman-unescapedoutputs-false-positives-warnings --- config/brakeman.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/brakeman.yml b/config/brakeman.yml index 7890bda2d90..28e6a3b34c0 100644 --- a/config/brakeman.yml +++ b/config/brakeman.yml @@ -1,3 +1,4 @@ --- :safe_methods: +- :pagy_nav - :sanitize_field From f1c217b17e1de745da44f831bc8c1f91936604ab Mon Sep 17 00:00:00 2001 From: Ceithir Date: Sun, 9 Feb 2025 09:12:43 +0100 Subject: [PATCH 12/16] Move to own module --- app/helpers/application_helper.rb | 63 ------------------------------ app/helpers/pagination_helper.rb | 65 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 63 deletions(-) create mode 100644 app/helpers/pagination_helper.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 12549e58f30..554e8c5863f 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -516,69 +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 - - include Pagy::Frontend - # Cf https://github.com/ddnexus/pagy/blob/master/gem/lib/pagy/frontend.rb - 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 = %(

    #{t('pagination.title')}

    ) - - html << %() - - prev_text = t("pagination.previous") - prev_a = - if (p_prev = pagy.prev) - a.call(p_prev, prev_text) - else - %(#{prev_text}) - end - html << %(
  • ) - - pagy.series(**vars).each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] - html << %(
  • ) - html << case item - when Integer - a.call(item) - when String - %(#{pagy.label_for(item)}) - when :gap - %(#{pagy_t('pagy.gap')}) - else - raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}" - end - html << %(
  • ) - end - - next_text = t("pagination.next") - next_a = - if (p_next = pagy.next) - a.call(p_next, next_text) - else - %(#{next_text}) - end - html << %() - - html << %() - 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..a91bcceeef0 --- /dev/null +++ b/app/helpers/pagination_helper.rb @@ -0,0 +1,65 @@ +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 + unless options[:renderer] + options = options.merge renderer: PaginationListLinkRenderer + end + super(*[collection_or_options, options].compact) + end + + # Cf https://github.com/ddnexus/pagy/blob/master/gem/lib/pagy/frontend.rb + 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 = %(

    #{t('pagination.title')}

    ) + + html << %() + + prev_text = t("pagination.previous") + prev_a = + if (p_prev = pagy.prev) + a.call(p_prev, prev_text) + else + %(#{prev_text}) + end + html << %() + + pagy.series(**vars).each do |item| # series example: [1, :gap, 7, 8, "9", 10, 11, :gap, 36] + html << %(
  • ) + html << case item + when Integer + a.call(item) + when String + %(#{pagy.label_for(item)}) + when :gap + %(#{pagy_t('pagy.gap')}) + else + raise InternalError, "expected item types in series to be Integer, String or :gap; got #{item.inspect}" + end + html << %(
  • ) + end + + next_text = t("pagination.next") + next_a = + if (p_next = pagy.next) + a.call(p_next, next_text) + else + %(#{next_text}) + end + html << %() + + html << %() + end +end From 955dd4a4eb0c249295b656816d270d0657f85bb7 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Sun, 9 Feb 2025 09:32:19 +0100 Subject: [PATCH 13/16] Keep closer to Pagy's original syntax --- app/helpers/pagination_helper.rb | 8 ++++---- config/initializers/gem-plugin_config/pagy.rb | 3 +++ config/locales/views/en.yml | 9 +++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb index a91bcceeef0..47127283fb1 100644 --- a/app/helpers/pagination_helper.rb +++ b/app/helpers/pagination_helper.rb @@ -23,11 +23,11 @@ def pagy_nav(pagy, id: nil, aria_label: nil, **vars) id = %( id="#{id}") if id a = pagy_anchor(pagy, **vars) - html = %(

    #{t('pagination.title')}

    ) + html = %(

    #{t('pagy.h4')}

    ) - html << %() + html << %() - prev_text = t("pagination.previous") + prev_text = pagy_t("pagy.prev") prev_a = if (p_prev = pagy.prev) a.call(p_prev, prev_text) @@ -51,7 +51,7 @@ def pagy_nav(pagy, id: nil, aria_label: nil, **vars) html << %() end - next_text = t("pagination.next") + next_text = pagy_t("pagy.next") next_a = if (p_next = pagy.next) a.call(p_next, next_text) diff --git a/config/initializers/gem-plugin_config/pagy.rb b/config/initializers/gem-plugin_config/pagy.rb index 809f8717ed2..839cc4cbbd7 100644 --- a/config/initializers/gem-plugin_config/pagy.rb +++ b/config/initializers/gem-plugin_config/pagy.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +# https://ddnexus.github.io/pagy/docs/extras/i18n/ +require 'pagy/extras/i18n' + # See https://ddnexus.github.io/pagy/docs/api/pagy#variables Pagy::DEFAULT[:limit] = ArchiveConfig.ITEMS_PER_PAGE Pagy::DEFAULT[:size] = 9 diff --git a/config/locales/views/en.yml b/config/locales/views/en.yml index 5da03e262db..84729447be0 100644 --- a/config/locales/views/en.yml +++ b/config/locales/views/en.yml @@ -1565,11 +1565,12 @@ en: orphan_user: orphaning_bylines_only_message_html: Orphaning will automatically remove your personal information from bylines only. It will not automatically remove your personal information from anywhere else in the work(s). Social media accounts, contact information, email addresses, names, and any other personal information posted in the title, summary, notes, tags, work body, or comment text will not be automatically removed. Comments posted by other users will also not be affected by Orphaning. If you want information removed from these places, you should edit or delete it prior to Orphaning. Once you Orphan the work(s), you will no longer be able to delete information in these places yourself. orphaning_works_message_html: 'Orphaning will permanently remove your username and/or pseud from the bylines of: the following work(s), their chapters, associated series, and any comments you may have left on them while logged into this account.' - pagination: - aria-label: Pagination + pagy: + aria_label: + nav: Pagination + h4: Pages Navigation next: Next → - previous: "← Previous" - title: Pages Navigation + prev: "← Previous" preferences: index: allow_collection_invitation: Allow others to invite my works to collections. From e2e990ba48f610ab9abd2699d47cd0db30cd402e Mon Sep 17 00:00:00 2001 From: Ceithir Date: Sun, 9 Feb 2025 09:50:19 +0100 Subject: [PATCH 14/16] Cop --- app/helpers/pagination_helper.rb | 6 ++---- config/initializers/gem-plugin_config/pagy.rb | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb index 47127283fb1..084063bde6b 100644 --- a/app/helpers/pagination_helper.rb +++ b/app/helpers/pagination_helper.rb @@ -7,9 +7,7 @@ def will_paginate(collection_or_options = nil, options = {}) options = collection_or_options collection_or_options = nil end - unless options[:renderer] - options = options.merge renderer: PaginationListLinkRenderer - end + options = options.merge renderer: PaginationListLinkRenderer unless options[:renderer] super(*[collection_or_options, options].compact) end @@ -25,7 +23,7 @@ def pagy_nav(pagy, id: nil, aria_label: nil, **vars) html = %(

    #{t('pagy.h4')}

    ) - html << %() + html << %() prev_text = pagy_t("pagy.prev") prev_a = diff --git a/config/initializers/gem-plugin_config/pagy.rb b/config/initializers/gem-plugin_config/pagy.rb index 839cc4cbbd7..62acb3685c2 100644 --- a/config/initializers/gem-plugin_config/pagy.rb +++ b/config/initializers/gem-plugin_config/pagy.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # https://ddnexus.github.io/pagy/docs/extras/i18n/ -require 'pagy/extras/i18n' +require "pagy/extras/i18n" # See https://ddnexus.github.io/pagy/docs/api/pagy#variables Pagy::DEFAULT[:limit] = ArchiveConfig.ITEMS_PER_PAGE From 51f30bdf29ee932a414c43cce63e07a87024e485 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Sun, 9 Feb 2025 15:51:20 +0100 Subject: [PATCH 15/16] Explicit Pagy i18n keys --- app/helpers/pagination_helper.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb index 084063bde6b..f1ec661394b 100644 --- a/app/helpers/pagination_helper.rb +++ b/app/helpers/pagination_helper.rb @@ -12,6 +12,9 @@ def will_paginate(collection_or_options = nil, options = {}) 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 From 1d4704e20027aa84cdf9169cbd559cc07284a6b9 Mon Sep 17 00:00:00 2001 From: Ceithir Date: Tue, 11 Feb 2025 13:49:49 +0100 Subject: [PATCH 16/16] Use existing key --- app/helpers/pagination_helper.rb | 2 +- config/locales/views/en.yml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb index f1ec661394b..cee1c0bfe95 100644 --- a/app/helpers/pagination_helper.rb +++ b/app/helpers/pagination_helper.rb @@ -24,7 +24,7 @@ def pagy_nav(pagy, id: nil, aria_label: nil, **vars) id = %( id="#{id}") if id a = pagy_anchor(pagy, **vars) - html = %(

    #{t('pagy.h4')}

    ) + html = %(

    #{t('a11y.navigation')}

    ) html << %() diff --git a/config/locales/views/en.yml b/config/locales/views/en.yml index 84729447be0..bcd303d2711 100644 --- a/config/locales/views/en.yml +++ b/config/locales/views/en.yml @@ -1568,7 +1568,6 @@ en: pagy: aria_label: nav: Pagination - h4: Pages Navigation next: Next → prev: "← Previous" preferences: