From be33ca651626b8b20581bd17113c99b06d2ab501 Mon Sep 17 00:00:00 2001 From: Jeff Iacono Date: Sat, 23 Jul 2011 04:24:49 -0500 Subject: [PATCH] page_entries_info view helper Renders a helpful message with numbers of displayed vs. total entries. Ported from mislav/will_paginate Basic usage: <%= page_entries_info @posts %> # => Displaying posts 6 - 10 of 26 in total --- lib/kaminari/helpers/action_view_extension.rb | 33 ++++++++ spec/helpers/action_view_extension_spec.rb | 77 ++++++++++++++++++- 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/lib/kaminari/helpers/action_view_extension.rb b/lib/kaminari/helpers/action_view_extension.rb index c19dc8b..e271021 100644 --- a/lib/kaminari/helpers/action_view_extension.rb +++ b/lib/kaminari/helpers/action_view_extension.rb @@ -45,6 +45,39 @@ module Kaminari block.call if block end end + + # Renders a helpful message with numbers of displayed vs. total entries. + # Ported from mislav/will_paginate + # + # ==== Examples + # Basic usage: + # + # <%= page_entries_info @posts %> + # #-> Displaying posts 6 - 10 of 26 in total + # + # By default, the message will use the humanized class name of objects + # in collection: for instance, "project types" for ProjectType models. + # Override this with the :entry_name parameter: + # + # <%= 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('_', ' ')) + if collection.num_pages < 2 + case collection.total_count + when 0; "No #{entry_name.pluralize} found" + when 1; "Displaying 1 #{entry_name}" + else; "Displaying all #{collection.total_count} #{entry_name.pluralize}" + end + else + offset = (collection.current_page - 1) * collection.limit_value + %{Displaying #{entry_name.pluralize} %d - %d of %d in total} % [ + offset + 1, + offset + collection.count, + collection.total_count + ] + end + end end end end diff --git a/spec/helpers/action_view_extension_spec.rb b/spec/helpers/action_view_extension_spec.rb index e00e266..76afb47 100644 --- a/spec/helpers/action_view_extension_spec.rb +++ b/spec/helpers/action_view_extension_spec.rb @@ -1,11 +1,9 @@ require File.expand_path('../spec_helper', File.dirname(__FILE__)) describe 'Kaminari::ActionViewExtension' do - before do - 50.times {|i| User.create! :name => "user#{i}"} - end describe '#paginate' do before do + 50.times {|i| User.create! :name => "user#{i}"} @users = User.page(1) end subject { helper.paginate @users, :params => {:controller => 'users', :action => 'index'} } @@ -19,6 +17,9 @@ describe 'Kaminari::ActionViewExtension' do end describe '#link_to_next_page' do + before do + 50.times {|i| User.create! :name => "user#{i}"} + end context 'having more page' do before do @users = User.page(1) @@ -34,4 +35,74 @@ describe 'Kaminari::ActionViewExtension' do it { should_not be } end end + + describe '#page_entries_info' do + before do + @users = User.page(1).per(25) + end + context 'having no entries' do + subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} } + it { should == 'No entries found' } + end + + context 'having 1 entry' do + before do + User.create! + @users = User.page(1).per(25) + end + subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} } + it { should == 'Displaying 1 user' } + + context 'setting the entry name option to "member"' do + subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} } + it { should == 'Displaying 1 member' } + end + end + + context 'having more than 1 but less than a page of entries' do + before do + 10.times {|i| User.create!} + @users = User.page(1).per(25) + end + subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} } + it { should == 'Displaying all 10 users' } + + context 'setting the entry name option to "member"' do + subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} } + it { should == 'Displaying all 10 members' } + end + end + + context 'having more than one page of entries' do + before do + 50.times {|i| User.create!} + end + + describe 'the first page' do + before do + @users = User.page(1).per(25) + end + subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} } + it { should == 'Displaying users 1 - 25 of 50 in total' } + + context 'setting the entry name option to "member"' do + subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} } + it { should == 'Displaying members 1 - 25 of 50 in total' } + end + end + + describe 'the next page' do + before do + @users = User.page(2).per(25) + end + subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} } + it { should == 'Displaying users 26 - 50 of 50 in total' } + + context 'setting the entry name option to "member"' do + subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} } + it { should == 'Displaying members 26 - 50 of 50 in total' } + end + end + end + end end