]> source.dussan.org Git - redmine.git/commitdiff
Adaptive display of "Per page" links (#7720).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 10 May 2012 18:51:11 +0000 (18:51 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Thu, 10 May 2012 18:51:11 +0000 (18:51 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9664 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/application_helper.rb
test/unit/helpers/application_helper_test.rb

index 02b8be63a55d1c2e63319b7d87ae7aa7fb2e9dfe..a84d4a9a1a655de486ba282ae82d5d525726603d 100644 (file)
@@ -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)
index 4f67de4d62f056055c7f73b706d1e7ef7e382ea3..a99bddc572600c95c041cf929b5640e420e5d4ac 100644 (file)
@@ -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