Permalink
Browse files

Merge branch 'master' of https://github.com/plribeiro3000/kaminari in…

…to plribeiro3000-master

Conflicts:
	lib/kaminari/helpers/action_view_extension.rb
  • Loading branch information...
2 parents 93053d2 + e8bdf83 commit 6828d85b91289a7390c2e2984040abd8e5937fa8 @amatsuda amatsuda committed May 15, 2012
@@ -8,3 +8,12 @@ en:
previous: "‹ Prev"
next: "Next ›"
truncate: "..."
+ helpers:
+ page_entries_info:
+ one_page:
+ display_entries:
+ zero: "No %{entry_name} found"
+ one: "Displaying <b>1</b> %{entry_name}"
+ other: "Displaying <b>all %{count}</b> %{entry_name}"
+ more_pages:
+ display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
@@ -80,22 +80,29 @@ def link_to_next_page(scope, name, options = {}, &block)
#
# By default, the message will use the humanized class name of objects
# in collection: for instance, "project types" for ProjectType models.
+ # The namespace will be cutted out and only the last name will be used.
# 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('_', ' '))
+
+ entry_name = if collection.empty?
+ 'entry'
+ elsif options[:entry_name]
+ options[:entry_name]
+ else
+ collection.first.class.model_name.human.downcase
+ end
+ entry_name = entry_name.pluralize unless collection.total_count == 1
+
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
+ t('helpers.page_entries_info.one_page.display_entries', :entry_name => entry_name, :count => collection.total_count)
else
- 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}
+ first = collection.offset_value + 1
+ last = collection.last_page? ? collection.total_count : collection.offset_value + collection.limit_value
+ t('helpers.page_entries_info.more_pages.display_entries', :entry_name => entry_name, :first => first, :last => last, :total => collection.total_count)
end.html_safe
end
end
View
@@ -24,6 +24,7 @@ class User < ActiveRecord::Base
has_many :readerships
has_many :books_authored, :through => :authorships, :source => :book
has_many :books_read, :through => :readerships, :source => :book
+ has_many :addresses, :class_name => 'User::Address'
def readers
User.joins(:books_read => :authors).where(:authors_books => {:id => self})
@@ -56,6 +57,10 @@ class Book < ActiveRecord::Base
# a model that is a descendant of AR::Base but doesn't directly inherit AR::Base
class Admin < User
end
+# a model with namespace
+class User::Address < ActiveRecord::Base
+ belongs_to :user
+end
# controllers
class ApplicationController < ActionController::Base; end
@@ -68,6 +73,15 @@ def index
ERB
end
end
+class AddressesController < ApplicationController
+ def index
+ @addresses = User::Address.page params[:page]
+ render :inline => <<-ERB
+<%= @addresses.map(&:street).join("\n") %>
+<%= paginate @addresses %>
+ERB
+ end
+end
# helpers
Object.const_set(:ApplicationHelper, Module.new)
@@ -80,5 +94,6 @@ def self.up
create_table(:books) {|t| t.string :title}
create_table(:readerships) {|t| t.integer :user_id; t.integer :book_id }
create_table(:authorships) {|t| t.integer :user_id; t.integer :book_id }
+ create_table(:user_addresses) {|t| t.string :street; t.integer :user_id }
end
end
@@ -71,72 +71,143 @@
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
+ context 'on a model without namespace' 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 'having no entries' do
+ subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
+ it { should == 'No entries found' }
+ end
- 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' }
+ 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
- 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)
+ 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
- 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' }
+ 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
-
- context 'having more than one page of entries' do
+ context 'on a model with namespace' do
before do
- 50.times {|i| User.create!}
+ @addresses = User::Address.page(1).per(25)
+ end
+ context 'having no entries' do
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
+ it { should == 'No entries found' }
end
- describe 'the first page' do
+ context 'having 1 entry' do
before do
- @users = User.page(1).per(25)
+ User::Address.create!
+ @addresses = User::Address.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' }
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
+ it { should == 'Displaying <b>1</b> address' }
- 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' }
+ context 'setting the entry name option to "place"' do
+ subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
+ it { should == 'Displaying <b>1</b> place' }
end
end
- describe 'the next page' do
+ context 'having more than 1 but less than a page of entries' do
before do
- @users = User.page(2).per(25)
+ 10.times {|i| User::Address.create!}
+ @addresses = User::Address.page(1).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' }
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
+ it { should == 'Displaying <b>all 10</b> addresses' }
- 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' }
+ context 'setting the entry name option to "place"' do
+ subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
+ it { should == 'Displaying <b>all 10</b> places' }
+ end
+ end
+
+ context 'having more than one page of entries' do
+ before do
+ 50.times {|i| User::Address.create!}
+ end
+
+ describe 'the first page' do
+ before do
+ @addresses = User::Address.page(1).per(25)
+ end
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
+ it { should == 'Displaying addresses <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
+
+ context 'setting the entry name option to "place"' do
+ subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
+ it { should == 'Displaying places <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
+ end
+ end
+
+ describe 'the next page' do
+ before do
+ @addresses = User::Address.page(2).per(25)
+ end
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
+ it { should == 'Displaying addresses <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
+
+ context 'setting the entry name option to "place"' do
+ subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
+ it { should == 'Displaying places <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
+ end
end
end
end
end
-end
+end

0 comments on commit 6828d85

Please sign in to comment.