Repository: kaminari/kaminari (Ruby)

Issue:
Upgrading to 1.2.0 breaks "total" in "display_entries" when total count is > 1 but only 1 result per page
Hello. It looks like maybe [this change](https://github.com/kaminari/kaminari/commit/011d6ac3b866d5e1836c246f9a598c79d1213fe5#diff-5687b4595ab8fdf7d48554e7384e25f2) is causing the `last` parameter to be `0` instead of `1` when there is only 1 result per page. So, if there are 3 pages, each with 1 result per page, instead of saying `Displaying 1 - 1 of 3 results`, it says `Displaying 1 - 0 of 3 results`.

When I upgrade from 1.1.1 to 1.2.0, this test fails as described above:
https://github.com/codeforamerica/ohana-web-search/blob/master/spec/features/search/pagination_spec.rb#L67

This is how I am calling `Kaminara.paginate_array`:
https://github.com/codeforamerica/ohana-web-search/blob/master/app/services/paginator.rb#L14-L16

And this is how the view is using it:
https://github.com/codeforamerica/ohana-web-search/blob/ecd8dae5864f2a5ede24674f2df041a04323e984/app/views/component/locations/results/_header_summary.html.haml#L19

And the localization entry:
https://github.com/codeforamerica/ohana-web-search/blob/1225c221f289700dd888089a8ede8bf0fde0f4ba/config/locales/en.yml#L62-L64

Any ideas?


Discussion:
Thanks you for reporting.  This is a bit surprising - the keyword argument change should not change any behaviour. I'll look into your code, but in the mean time it would be great if you could come up with a test case that replicates the issue.
I looked through the changes between 1.1.1 and 1.2.0 here: https://github.com/kaminari/kaminari/compare/v1.1.1...v1.2.0

and I found the bug. This is the breaking change in `page_entries_info` in `helper_methods.rb`:

from this:
```ruby
 t(
  'helpers.page_entries_info.more_pages.display_entries', 
  entry_name: entry_name, 
  first: collection.offset_value + 1, 
  last: [collection.offset_value + collection.limit_value, collection.total_count].min, 
  total: collection.total_count
)
```

to this:
```ruby
from = collection.offset_value + 1
to   = collection.offset_value + (collection.respond_to?(:records) ? collection.records : collection.to_a).size

t(
  'helpers.page_entries_info.more_pages.display_entries', 
  entry_name: entry_name, 
  first: from, 
  last: to, 
  total: collection.total_count
)
```

This fails in the scenario where you use `paginate_array` with an empty array and the total count option. For example:
```ruby
test 'with total_count option' do
  users = Kaminari.paginate_array([], total_count: 50).page(1).per(1)
  assert_equal 'Displaying Benutzer <b>1&nbsp;-&nbsp;1</b> of <b>50</b> in total', view.page_entries_info(users, entry_name: 'Benutzer')
end
```
@monfresh Thanks for taking your time on providing us with a test. I should be able to look into later this week.
@yuki24 Were you ever able to look into this?
It looks like in 1.2.1, this is now causing an exception: 
```
ActionView::Template::Error:
  Array can't be coerced into Integer
    # /Users/moncef/.gem/ruby/2.6.6/gems/kaminari-core-1.2.1/lib/kaminari/helpers/helper_methods.rb:208:in `+'
    # /Users/moncef/.gem/ruby/2.6.6/gems/kaminari-core-1.2.1/lib/kaminari/helpers/helper_methods.rb:208:in `page_entries_info'
    # ./app/views/component/locations/results/_header_summary.html.haml:19:in `_app_views_component_locations_results__header_summary_html_haml___645785241305980862_70318242620360'
```



Patch:
diff --git a/kaminari-core/lib/kaminari/helpers/helper_methods.rb b/kaminari-core/lib/kaminari/helpers/helper_methods.rb
index a333a6ab..283bbd85 100644
--- a/kaminari-core/lib/kaminari/helpers/helper_methods.rb
+++ b/kaminari-core/lib/kaminari/helpers/helper_methods.rb
@@ -210,7 +210,13 @@ def page_entries_info(collection, entry_name: nil)
           t('helpers.page_entries_info.one_page.display_entries', entry_name: entry_name, count: collection.total_count)
         else
           from = collection.offset_value + 1
-          to   = collection.offset_value + page_size
+          records = collection.respond_to?(:records) ? collection.records : collection.to_a
+          to =
+            if records.empty?
+              [collection.offset_value + collection.limit_value, collection.total_count].min
+            else
+              collection.offset_value + records.size
+            end
 
           t('helpers.page_entries_info.more_pages.display_entries', entry_name: entry_name, first: from, last: to, total: collection.total_count)
         end.html_safe


Test patch:
diff --git a/kaminari-core/test/helpers/action_view_extension_test.rb b/kaminari-core/test/helpers/action_view_extension_test.rb
index f629565a..a0454283 100644
--- a/kaminari-core/test/helpers/action_view_extension_test.rb
+++ b/kaminari-core/test/helpers/action_view_extension_test.rb
@@ -473,10 +473,17 @@ def total_count
         end
       end
 
-      test 'on a PaginatableArray' do
-        numbers = Kaminari.paginate_array(%w{one two three}).page(1)
+      sub_test_case 'on a PaginatableArray' do
+        test 'with a non-empty array' do
+          numbers = Kaminari.paginate_array(%w{one two three}).page(1)
 
-        assert_equal 'Displaying <b>all 3</b> entries', view.page_entries_info(numbers)
+          assert_equal 'Displaying <b>all 3</b> entries', view.page_entries_info(numbers)
+        end
+
+        test 'with an empty array and total_count option' do
+          users = Kaminari.paginate_array([], total_count: 50).page(1).per(1)
+          assert_equal 'Displaying users <b>1&nbsp;-&nbsp;1</b> of <b>50</b> in total', view.page_entries_info(users, entry_name: 'user')
+        end
       end
     end