From 8b0028c2f559209ec75018cda7c6b06e60ad5841 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Fri, 8 Aug 2025 19:10:51 +0300 Subject: [PATCH] Allow customizing active page id --- app/helpers/numbered_pagination_helper.rb | 16 ++++---- app/views/elements/show.html.erb | 2 +- app/views/old_elements/_actions.html.erb | 2 +- app/views/old_elements/index.html.erb | 2 +- .../numbered_pagination_helper_test.rb | 40 +++++++++---------- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/app/helpers/numbered_pagination_helper.rb b/app/helpers/numbered_pagination_helper.rb index 3f8a1a17a..1b504e811 100644 --- a/app/helpers/numbered_pagination_helper.rb +++ b/app/helpers/numbered_pagination_helper.rb @@ -1,11 +1,11 @@ module NumberedPaginationHelper - def numbered_pagination(top_page, active_page: top_page + 1, window_half_size: 50, step_size: 50, &) + def numbered_pagination(top_page, active_id, active_page: top_page + 1, window_half_size: 50, step_size: 50, &) lists = [] if top_page <= 5 lists << tag.ul(:class => "pagination pagination-sm mt-1") do (1..top_page).each do |page| - concat numbered_pagination_item(page, **yield(page), :active => page == active_page) + concat numbered_pagination_item(page, **yield(page), :active_id => (active_id if page == active_page)) end end else @@ -33,7 +33,7 @@ module NumberedPaginationHelper lists << tag.ul(:id => "versions-navigation-list-start", :class => "pagination pagination-sm mt-1") do start_list_pages.each do |page| - concat numbered_pagination_item(page, **yield(page), :active => page == active_page, + concat numbered_pagination_item(page, **yield(page), :active_id => (active_id if page == active_page), :edge => [false, page == start_list_pages.last], :edge_border => true) end @@ -50,14 +50,15 @@ module NumberedPaginationHelper if page == :gap concat numbered_pagination_item("...", :edge => edge) else - concat numbered_pagination_item(page, **yield(page), :active => page == active_page, :edge => edge) + concat numbered_pagination_item(page, **yield(page), :active_id => (active_id if page == active_page), + :edge => edge) end end end lists << tag.ul(:id => "versions-navigation-list-end", :class => "pagination pagination-sm mt-1") do end_list_pages.each do |page| - concat numbered_pagination_item(page, **yield(page), :active => page == active_page, + concat numbered_pagination_item(page, **yield(page), :active_id => (active_id if page == active_page), :edge => [page == end_list_pages.first, false], :edge_border => true) end @@ -69,7 +70,7 @@ module NumberedPaginationHelper private - def numbered_pagination_item(body, href: nil, title: nil, active: false, edge: [false, false], edge_border: false) + def numbered_pagination_item(body, href: nil, title: nil, active_id: nil, edge: [false, false], edge_border: false) link_class = ["page-link", { "rounded-start-0" => edge.first, "border-start-0" => edge.first && !edge_border, "rounded-end-0" => edge.last, @@ -79,7 +80,6 @@ module NumberedPaginationHelper else tag.span body, :class => link_class end - tag.li link, :id => ("versions-navigation-active-page-item" if active), - :class => ["page-item", { "disabled" => !href, "active" => active }] + tag.li link, :id => active_id, :class => ["page-item", { "disabled" => !href, "active" => !!active_id }] end end diff --git a/app/views/elements/show.html.erb b/app/views/elements/show.html.erb index 9c65bef75..371e03266 100644 --- a/app/views/elements/show.html.erb +++ b/app/views/elements/show.html.erb @@ -29,7 +29,7 @@ - <%= numbered_pagination(@feature.version, :active_page => @feature.version) do |v| + <%= numbered_pagination(@feature.version, "versions-navigation-active-page-item", :active_page => @feature.version) do |v| { :href => { :controller => "old_#{@type.pluralize}", :action => :show, :version => v }, :title => t("browse.versions_navigation.version", :version => v) } end %> diff --git a/app/views/old_elements/_actions.html.erb b/app/views/old_elements/_actions.html.erb index 222ec078d..8ac0b7cc3 100644 --- a/app/views/old_elements/_actions.html.erb +++ b/app/views/old_elements/_actions.html.erb @@ -25,7 +25,7 @@ - <%= numbered_pagination(@current_feature.version, :active_page => @feature.version) do |v| + <%= numbered_pagination(@current_feature.version, "versions-navigation-active-page-item", :active_page => @feature.version) do |v| { :href => { :version => v }, :title => t("browse.versions_navigation.version", :version => v) } end %> diff --git a/app/views/old_elements/index.html.erb b/app/views/old_elements/index.html.erb index 53436dc2c..7a043aa46 100644 --- a/app/views/old_elements/index.html.erb +++ b/app/views/old_elements/index.html.erb @@ -57,7 +57,7 @@ - <%= numbered_pagination(@current_feature.version) do |v| + <%= numbered_pagination(@current_feature.version, "versions-navigation-active-page-item") do |v| { :href => { :action => :show, :version => v }, :title => t("browse.versions_navigation.version", :version => v) } end %> diff --git a/test/helpers/numbered_pagination_helper_test.rb b/test/helpers/numbered_pagination_helper_test.rb index dd4f83af2..b62123db4 100644 --- a/test/helpers/numbered_pagination_helper_test.rb +++ b/test/helpers/numbered_pagination_helper_test.rb @@ -2,7 +2,7 @@ require "test_helper" class NumberedPaginationHelperTest < ActionView::TestCase def test_numbered_pagination1 - pagination = numbered_pagination(1) { |n| sample_item_data n } + pagination = numbered_pagination(1, "active_page") { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 1 do assert_dom "> li", 1 do @@ -12,7 +12,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination1_active1 - pagination = numbered_pagination(1, :active_page => 1) { |n| sample_item_data n } + pagination = numbered_pagination(1, "active_page", :active_page => 1) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 1 do assert_dom "> li", 1 do @@ -22,7 +22,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination5 - pagination = numbered_pagination(5) { |n| sample_item_data n } + pagination = numbered_pagination(5, "active_page") { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 1 do assert_dom "> li", 5 do |items| @@ -34,7 +34,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination6 - pagination = numbered_pagination(6) { |n| sample_item_data n } + pagination = numbered_pagination(6, "active_page") { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do @@ -52,7 +52,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination6_active1 - pagination = numbered_pagination(6, :active_page => 1) { |n| sample_item_data n } + pagination = numbered_pagination(6, "active_page", :active_page => 1) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 2 do |items| @@ -71,7 +71,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination6_active2 - pagination = numbered_pagination(6, :active_page => 2) { |n| sample_item_data n } + pagination = numbered_pagination(6, "active_page", :active_page => 2) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 3 do |items| @@ -90,7 +90,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination6_active3 - pagination = numbered_pagination(6, :active_page => 3) { |n| sample_item_data n } + pagination = numbered_pagination(6, "active_page", :active_page => 3) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -109,7 +109,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination6_active4 - pagination = numbered_pagination(6, :active_page => 4) { |n| sample_item_data n } + pagination = numbered_pagination(6, "active_page", :active_page => 4) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -128,7 +128,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination6_active5 - pagination = numbered_pagination(6, :active_page => 5) { |n| sample_item_data n } + pagination = numbered_pagination(6, "active_page", :active_page => 5) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -147,7 +147,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination6_active6 - pagination = numbered_pagination(6, :active_page => 6) { |n| sample_item_data n } + pagination = numbered_pagination(6, "active_page", :active_page => 6) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -166,7 +166,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_window_start_include - pagination = numbered_pagination(50, :window_half_size => 3, :active_page => 3) { |n| sample_item_data n } + pagination = numbered_pagination(50, "active_page", :window_half_size => 3, :active_page => 3) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -187,7 +187,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_window_start_touch - pagination = numbered_pagination(50, :window_half_size => 3, :active_page => 5) { |n| sample_item_data n } + pagination = numbered_pagination(50, "active_page", :window_half_size => 3, :active_page => 5) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -210,7 +210,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_window_start_touch_almost - pagination = numbered_pagination(50, :window_half_size => 3, :active_page => 6) { |n| sample_item_data n } + pagination = numbered_pagination(50, "active_page", :window_half_size => 3, :active_page => 6) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -234,7 +234,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_window_middle - pagination = numbered_pagination(50, :window_half_size => 3, :active_page => 43) { |n| sample_item_data n } + pagination = numbered_pagination(50, "active_page", :window_half_size => 3, :active_page => 43) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -258,7 +258,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_window_end_touch - pagination = numbered_pagination(50, :window_half_size => 3, :active_page => 46) { |n| sample_item_data n } + pagination = numbered_pagination(50, "active_page", :window_half_size => 3, :active_page => 46) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -281,7 +281,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_window_end_beyond - pagination = numbered_pagination(50, :window_half_size => 3) { |n| sample_item_data n } + pagination = numbered_pagination(50, "active_page", :window_half_size => 3) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -299,7 +299,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_step - pagination = numbered_pagination(35, :step_size => 10, :window_half_size => 0) { |n| sample_item_data n } + pagination = numbered_pagination(35, "active_page", :step_size => 10, :window_half_size => 0) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -321,7 +321,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_step_end_touch - pagination = numbered_pagination(31, :step_size => 10, :window_half_size => 0) { |n| sample_item_data n } + pagination = numbered_pagination(31, "active_page", :step_size => 10, :window_half_size => 0) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -342,7 +342,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_step_window - pagination = numbered_pagination(35, :active_page => 15, :step_size => 10, :window_half_size => 1) { |n| sample_item_data n } + pagination = numbered_pagination(35, "active_page", :active_page => 15, :step_size => 10, :window_half_size => 1) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| @@ -368,7 +368,7 @@ class NumberedPaginationHelperTest < ActionView::TestCase end def test_numbered_pagination_step_window_touch - pagination = numbered_pagination(35, :active_page => 12, :step_size => 10, :window_half_size => 1) { |n| sample_item_data n } + pagination = numbered_pagination(35, "active_page", :active_page => 12, :step_size => 10, :window_half_size => 1) { |n| sample_item_data n } pagination_dom = Rails::Dom::Testing.html_document_fragment.parse(pagination) assert_dom pagination_dom, "ul", :count => 3 do |lists| assert_dom lists[0], "> li", 1 do |items| -- 2.39.5