Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jeffreyiacono/kaminari in…
Browse files Browse the repository at this point in the history
…to jeffreyiacono-master

Conflicts:
	spec/helpers/action_view_extension_spec.rb
  • Loading branch information
amatsuda committed Dec 17, 2011
2 parents 56df995 + be33ca6 commit c2b6815
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
33 changes: 33 additions & 0 deletions lib/kaminari/helpers/action_view_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ def link_to_next_page(scope, name, options = {}, &block)
link_to_unless scope.last_page?, name, params.merge(param_name => (scope.current_page + 1)), options.reverse_merge(:rel => 'next') do
block.call if block
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 <tt>:entry_name</tt> 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 <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
%{Displaying #{entry_name.pluralize} <b>%d&nbsp;-&nbsp;%d</b> of <b>%d</b> in total} % [
offset + 1,
offset + collection.count,
collection.total_count
]
end
end
end
end
end
78 changes: 74 additions & 4 deletions spec/helpers/action_view_extension_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
require 'spec_helper'

describe 'Kaminari::ActionViewExtension' do
before do
User.delete_all
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'} }
Expand All @@ -20,6 +17,9 @@
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)
Expand All @@ -42,4 +42,74 @@
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 <b>1</b> 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 <b>1</b> 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 <b>all 10</b> 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 <b>all 10</b> 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 <b>1&nbsp;-&nbsp;25</b> of <b>50</b> 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 <b>1&nbsp;-&nbsp;25</b> of <b>50</b> 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 <b>26&nbsp;-&nbsp;50</b> of <b>50</b> 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 <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
end
end
end
end
end

0 comments on commit c2b6815

Please sign in to comment.