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
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)
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