From 5bd90548b0a3b5d7d8019b57607cd2fe63f8b42f Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Thu, 10 May 2012 18:51:11 +0000 Subject: [PATCH] Adaptive display of "Per page" links (#7720). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9664 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/application_helper.rb | 20 ++++++++++++++++---- test/unit/helpers/application_helper_test.rb | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 02b8be63a..a84d4a9a1 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -399,7 +399,7 @@ module ApplicationHelper unless count.nil? html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})" - if per_page_links != false && links = per_page_links(paginator.items_per_page) + if per_page_links != false && links = per_page_links(paginator.items_per_page, count) html << " | #{links}" end end @@ -407,11 +407,23 @@ module ApplicationHelper html.html_safe end - def per_page_links(selected=nil) - links = Setting.per_page_options_array.collect do |n| + def per_page_links(selected=nil, item_count=nil) + values = Setting.per_page_options_array + if item_count && values.any? + if item_count > values.first + max = values.detect {|value| value >= item_count} || item_count + else + max = item_count + end + values = values.select {|value| value <= max || value == selected} + end + if values.empty? || (values.size == 1 && values.first == selected) + return nil + end + links = values.collect do |n| n == selected ? n : link_to_content_update(n, params.merge(:per_page => n)) end - links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil + l(:label_display_per_page, links.join(', ')) end def reorder_links(name, url, method = :post) diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index 4f67de4d6..a99bddc57 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -1088,4 +1088,19 @@ RAW def test_javascript_include_tag_for_plugin_should_pick_the_plugin_javascript assert_match 'src="/plugin_assets/foo/javascripts/scripts.js"', javascript_include_tag("scripts", :plugin => :foo) end + + def test_per_page_links_should_show_usefull_values + set_language_if_valid 'en' + stubs(:link_to).returns("[link]") + + with_settings :per_page_options => '10, 25, 50, 100' do + assert_nil per_page_links(10, 3) + assert_nil per_page_links(25, 3) + assert_equal "Per page: 10, [link]", per_page_links(10, 22) + assert_equal "Per page: [link], 25", per_page_links(25, 22) + assert_equal "Per page: [link], [link], 50", per_page_links(50, 22) + assert_equal "Per page: [link], 25, [link]", per_page_links(25, 26) + assert_equal "Per page: [link], 25, [link], [link]", per_page_links(25, 120) + end + end end -- 2.39.5