1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

refactor page_entries_info not to use buggy current_page_count method

This commit is contained in:
Akira Matsuda 2012-05-15 22:10:53 +09:00
parent 36bcc21db3
commit 93053d26ee
8 changed files with 6 additions and 54 deletions

View file

@ -85,23 +85,18 @@ module Kaminari
# <%= page_entries_info @posts, :entry_name => 'item' %>
# #-> Displaying items 6 - 10 of 26 in total
def page_entries_info(collection, options = {})
entry_name = options[:entry_name] || (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
output = ""
entry_name = options[:entry_name] || (collection.empty? ? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
if collection.num_pages < 2
output = case collection.total_count
case collection.total_count
when 0; "No #{entry_name.pluralize} found"
when 1; "Displaying <b>1</b> #{entry_name}"
else; "Displaying <b>all #{collection.total_count}</b> #{entry_name.pluralize}"
end
else
offset = (collection.current_page - 1) * collection.limit_value
output = %{Displaying #{entry_name.pluralize} <b>%d&nbsp;-&nbsp;%d</b> of <b>%d</b> in total} % [
offset + 1,
offset + collection.current_page_count,
collection.total_count
]
end
output.html_safe
page_start = collection.offset_value + 1
page_end = collection.last_page? ? collection.total_count : collection.offset_value + collection.limit_value
%{Displaying #{entry_name.pluralize} <b>#{page_start}&nbsp;-&nbsp;#{page_end}</b> of <b>#{collection.total_count}</b> in total}
end.html_safe
end
end
end

View file

@ -8,10 +8,6 @@ module Kaminari
end
end
def current_page_count #:nodoc:
count
end
def total_count #:nodoc:
# #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
@total_count ||= begin

View file

@ -43,10 +43,6 @@ module Kaminari
@_total_count || @_original_array.count
end
def current_page_count #:nodoc:
count
end
# returns another chunk of the original array
def offset(num)
self.class.new @_original_array, :limit => @_limit_value, :offset => num, :total_count => @_total_count

View file

@ -11,10 +11,5 @@ module Kaminari
def total_count #:nodoc:
model.count(query.options.except(:limit, :offset, :order))
end
def current_page_count #:nodoc:
count
end
end
end

View file

@ -12,15 +12,6 @@ module Kaminari
embedded? ? unpage.count : count
end
def current_page_count #:nodoc:
# TODO: this needs a better fix, count comes from Mongoid::Context::Mongo or Enumerable which have different signatures
begin
count(true)
rescue ArgumentError
count
end
end
private
def unpage
clone.tap do |crit|

View file

@ -11,11 +11,5 @@ module Kaminari
def total_count #:nodoc:
count
end
def current_page_count #:nodoc:
# TODO: this needs a better fix, ie. pass skip_and_limit = true
# into http://api.mongodb.org/ruby/1.2.0/Mongo/Cursor.html#count-instance_method
to_a.count
end
end
end

View file

@ -29,7 +29,6 @@ describe Kaminari::MongoMapperExtension do
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 25 }
it { should skip(0) }
end
@ -39,7 +38,6 @@ describe Kaminari::MongoMapperExtension do
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
@ -49,7 +47,6 @@ describe Kaminari::MongoMapperExtension do
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 25 }
it { should skip 0 }
end
@ -62,7 +59,6 @@ describe Kaminari::MongoMapperExtension do
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
@ -75,7 +71,6 @@ describe Kaminari::MongoMapperExtension do
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
end
@ -86,7 +81,6 @@ describe Kaminari::MongoMapperExtension do
its(:current_page) { should == 2 }
its(:limit_value) { should == 10 }
its(:num_pages) { should == 5 }
its(:current_page_count) { should == 10 }
it { should skip 10 }
end
end

View file

@ -33,7 +33,6 @@ describe Kaminari::MongoidExtension do
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 25 }
it { should skip(0) }
end
@ -43,7 +42,6 @@ describe Kaminari::MongoidExtension do
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
@ -53,7 +51,6 @@ describe Kaminari::MongoidExtension do
its(:current_page) { should == 1 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 25 }
it { should skip 0 }
end
@ -63,7 +60,6 @@ describe Kaminari::MongoidExtension do
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
@ -73,7 +69,6 @@ describe Kaminari::MongoidExtension do
its(:current_page) { should == 2 }
its(:limit_value) { should == 25 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 16 }
it { should skip 25 }
end
end
@ -84,7 +79,6 @@ describe Kaminari::MongoidExtension do
its(:current_page) { should == 2 }
its(:limit_value) { should == 10 }
its(:num_pages) { should == 5 }
its(:current_page_count) { should == 10 }
it { should skip 10 }
end
@ -120,7 +114,6 @@ describe Kaminari::MongoidExtension do
its(:limit_value) { should == 1 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 5 }
its(:current_page_count) { should == 1 }
end
context 'with criteria after' do
@ -130,7 +123,6 @@ describe Kaminari::MongoidExtension do
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 2 }
end
context 'with criteria before' do
@ -140,7 +132,6 @@ describe Kaminari::MongoidExtension do
its(:limit_value) { should == 2 }
its(:current_page) { should == 1 }
its(:num_pages) { should == 2 }
its(:current_page_count) { should == 2 }
end
end
end