kaminari 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kaminari might be problematic. Click here for more details.

Files changed (87) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +8 -1
  3. data/CHANGELOG +46 -10
  4. data/README.rdoc +14 -3
  5. data/Rakefile +22 -2
  6. data/app/views/kaminari/_first_page.html.erb +1 -1
  7. data/app/views/kaminari/_first_page.html.haml +1 -1
  8. data/app/views/kaminari/_first_page.html.slim +1 -1
  9. data/app/views/kaminari/_gap.html.erb +1 -1
  10. data/app/views/kaminari/_gap.html.haml +1 -1
  11. data/app/views/kaminari/_gap.html.slim +1 -1
  12. data/app/views/kaminari/_last_page.html.erb +1 -1
  13. data/app/views/kaminari/_last_page.html.haml +1 -1
  14. data/app/views/kaminari/_last_page.html.slim +1 -1
  15. data/app/views/kaminari/_next_page.html.erb +1 -1
  16. data/app/views/kaminari/_next_page.html.haml +1 -1
  17. data/app/views/kaminari/_next_page.html.slim +1 -1
  18. data/app/views/kaminari/_page.html.erb +2 -2
  19. data/app/views/kaminari/_page.html.haml +1 -1
  20. data/app/views/kaminari/_page.html.slim +1 -1
  21. data/app/views/kaminari/_paginator.html.erb +1 -1
  22. data/app/views/kaminari/_paginator.html.haml +1 -1
  23. data/app/views/kaminari/_paginator.html.slim +1 -1
  24. data/app/views/kaminari/_prev_page.html.erb +1 -1
  25. data/app/views/kaminari/_prev_page.html.haml +1 -1
  26. data/app/views/kaminari/_prev_page.html.slim +1 -1
  27. data/config/locales/kaminari.yml +9 -0
  28. data/gemfiles/active_record_30.gemfile +7 -0
  29. data/gemfiles/active_record_31.gemfile +7 -0
  30. data/gemfiles/active_record_32.gemfile +7 -0
  31. data/gemfiles/data_mapper_12.gemfile +12 -0
  32. data/gemfiles/mongo_mapper.gemfile +7 -0
  33. data/gemfiles/mongoid_24.gemfile +7 -0
  34. data/gemfiles/mongoid_30.gemfile +7 -0
  35. data/gemfiles/sinatra.gemfile +10 -0
  36. data/kaminari.gemspec +6 -18
  37. data/lib/generators/kaminari/templates/kaminari_config.rb +1 -0
  38. data/lib/kaminari.rb +22 -57
  39. data/lib/kaminari/config.rb +7 -1
  40. data/lib/kaminari/grape.rb +4 -0
  41. data/lib/kaminari/helpers/action_view_extension.rb +84 -13
  42. data/lib/kaminari/helpers/paginator.rb +8 -5
  43. data/lib/kaminari/helpers/sinatra_helpers.rb +25 -6
  44. data/lib/kaminari/helpers/tags.rb +1 -1
  45. data/lib/kaminari/hooks.rb +12 -10
  46. data/lib/kaminari/models/active_record_relation_methods.rb +0 -3
  47. data/lib/kaminari/models/configuration_methods.rb +15 -1
  48. data/lib/kaminari/models/page_scope_methods.rb +6 -2
  49. data/lib/kaminari/models/plucky_criteria_methods.rb +3 -0
  50. data/lib/kaminari/railtie.rb +2 -2
  51. data/lib/kaminari/sinatra.rb +2 -10
  52. data/lib/kaminari/version.rb +1 -1
  53. data/spec/config/config_spec.rb +15 -0
  54. data/spec/fake_app/active_record/config.rb +3 -0
  55. data/spec/{fake_app.rb → fake_app/active_record/models.rb} +7 -34
  56. data/spec/fake_app/data_mapper/config.rb +1 -0
  57. data/spec/fake_app/data_mapper/models.rb +27 -0
  58. data/spec/fake_app/mongo_mapper/config.rb +2 -0
  59. data/spec/fake_app/mongo_mapper/models.rb +9 -0
  60. data/spec/fake_app/mongoid/config.rb +16 -0
  61. data/spec/fake_app/mongoid/models.rb +22 -0
  62. data/spec/fake_app/rails_app.rb +55 -0
  63. data/spec/fake_app/sinatra_app.rb +22 -0
  64. data/spec/fake_gem.rb +0 -2
  65. data/spec/helpers/action_view_extension_spec.rb +192 -45
  66. data/spec/helpers/helpers_spec.rb +8 -8
  67. data/spec/helpers/sinatra_helpers_spec.rb +129 -133
  68. data/spec/helpers/tags_spec.rb +8 -8
  69. data/spec/models/active_record/active_record_relation_methods_spec.rb +30 -0
  70. data/spec/models/active_record/default_per_page_spec.rb +32 -0
  71. data/spec/models/active_record/max_per_page_spec.rb +32 -0
  72. data/spec/models/active_record/scopes_spec.rb +166 -0
  73. data/spec/models/array_spec.rb +7 -7
  74. data/spec/models/data_mapper/data_mapper_spec.rb +155 -0
  75. data/spec/models/mongo_mapper/mongo_mapper_spec.rb +72 -0
  76. data/spec/models/mongoid/mongoid_spec.rb +110 -0
  77. data/spec/requests/users_spec.rb +0 -1
  78. data/spec/spec_helper.rb +20 -18
  79. data/spec/spec_helper_for_sinatra.rb +22 -0
  80. data/spec/support/database_cleaner.rb +4 -1
  81. metadata +93 -141
  82. data/spec/models/active_record_relation_methods_spec.rb +0 -28
  83. data/spec/models/data_mapper_spec.rb +0 -181
  84. data/spec/models/default_per_page_spec.rb +0 -29
  85. data/spec/models/mongo_mapper_spec.rb +0 -82
  86. data/spec/models/mongoid_spec.rb +0 -129
  87. data/spec/models/scopes_spec.rb +0 -163
@@ -68,7 +68,7 @@ module Kaminari
68
68
  class LastPage < Tag
69
69
  include Link
70
70
  def page #:nodoc:
71
- @options[:num_pages]
71
+ @options[:total_pages]
72
72
  end
73
73
  end
74
74
 
@@ -1,29 +1,31 @@
1
1
  module Kaminari
2
2
  class Hooks
3
- def self.init!
3
+ def self.init
4
4
  ActiveSupport.on_load(:active_record) do
5
5
  require 'kaminari/models/active_record_extension'
6
6
  ::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
7
7
  end
8
8
 
9
+ begin; require 'data_mapper'; rescue LoadError; end
10
+ if defined? ::DataMapper
11
+ require 'dm-aggregates'
12
+ require 'kaminari/models/data_mapper_extension'
13
+ ::DataMapper::Collection.send :include, Kaminari::DataMapperExtension::Collection
14
+ ::DataMapper::Model.append_extensions Kaminari::DataMapperExtension::Model
15
+ # ::DataMapper::Model.send :extend, Kaminari::DataMapperExtension::Model
16
+ end
17
+
18
+ begin; require 'mongoid'; rescue LoadError; end
9
19
  if defined? ::Mongoid
10
20
  require 'kaminari/models/mongoid_extension'
11
- ::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
12
21
  ::Mongoid::Criteria.send :include, Kaminari::MongoidExtension::Criteria
22
+ ::Mongoid::Document.send :include, Kaminari::MongoidExtension::Document
13
23
  end
14
24
 
15
25
  ActiveSupport.on_load(:mongo_mapper) do
16
26
  require 'kaminari/models/mongo_mapper_extension'
17
27
  ::MongoMapper::Document.send :include, Kaminari::MongoMapperExtension::Document
18
28
  ::Plucky::Query.send :include, Kaminari::PluckyCriteriaMethods
19
- ::Plucky::Query.send :include, Kaminari::PageScopeMethods
20
- end
21
-
22
- if defined? ::DataMapper
23
- require 'kaminari/models/data_mapper_extension'
24
- ::DataMapper::Collection.send :include, Kaminari::DataMapperExtension::Collection
25
- ::DataMapper::Model.append_extensions Kaminari::DataMapperExtension::Model
26
- # ::DataMapper::Model.send :extend, Kaminari::DataMapperExtension::Model
27
29
  end
28
30
  require 'kaminari/models/array_extension'
29
31
 
@@ -13,9 +13,6 @@ module Kaminari
13
13
  @total_count ||= begin
14
14
  c = except(:offset, :limit, :order)
15
15
 
16
- # a workaround for 3.1.beta1 bug. see: https://github.com/rails/rails/issues/406
17
- c = c.reorder nil
18
-
19
16
  # Remove includes only if they are irrelevant
20
17
  c = c.except(:includes) unless references_eager_loaded_tables?
21
18
 
@@ -13,7 +13,21 @@ module Kaminari
13
13
  # This model's default +per_page+ value
14
14
  # returns +default_per_page+ value unless explicitly overridden via <tt>paginates_per</tt>
15
15
  def default_per_page
16
- @_default_per_page || Kaminari.config.default_per_page
16
+ @_default_per_page ||= Kaminari.config.default_per_page
17
+ end
18
+
19
+ # Overrides the max +per_page+ value per model
20
+ # class Article < ActiveRecord::Base
21
+ # max_paginates_per 100
22
+ # end
23
+ def max_paginates_per(val)
24
+ @_max_per_page = val
25
+ end
26
+
27
+ # This model's max +per_page+ value
28
+ # returns +max_per_page+ value unless explicitly overridden via <tt>max_paginates_per</tt>
29
+ def max_per_page
30
+ @_max_per_page ||= Kaminari.config.max_per_page
17
31
  end
18
32
  end
19
33
  end
@@ -5,6 +5,8 @@ module Kaminari
5
5
  def per(num)
6
6
  if (n = num.to_i) <= 0
7
7
  self
8
+ elsif max_per_page && max_per_page < n
9
+ limit(max_per_page).offset(offset_value / limit_value * max_per_page)
8
10
  else
9
11
  limit(n).offset(offset_value / limit_value * n)
10
12
  end
@@ -15,9 +17,11 @@ module Kaminari
15
17
  end
16
18
 
17
19
  # Total number of pages
18
- def num_pages
20
+ def total_pages
19
21
  (total_count.to_f / limit_value).ceil
20
22
  end
23
+ #FIXME for compatibility. remove num_pages at some time in the future
24
+ alias num_pages total_pages
21
25
 
22
26
  # Current page number
23
27
  def current_page
@@ -31,7 +35,7 @@ module Kaminari
31
35
 
32
36
  # Last page of the collection?
33
37
  def last_page?
34
- current_page >= num_pages
38
+ current_page >= total_pages
35
39
  end
36
40
  end
37
41
  end
@@ -1,5 +1,8 @@
1
1
  module Kaminari
2
2
  module PluckyCriteriaMethods
3
+ include Kaminari::PageScopeMethods
4
+ include Kaminari::ConfigurationMethods::ClassMethods
5
+
3
6
  def limit_value #:nodoc:
4
7
  options[:limit]
5
8
  end
@@ -1,7 +1,7 @@
1
1
  module Kaminari
2
2
  class Railtie < ::Rails::Railtie #:nodoc:
3
- initializer 'kaminari' do |app|
4
- Kaminari::Hooks.init!
3
+ initializer 'kaminari' do |_app|
4
+ Kaminari::Hooks.init
5
5
  end
6
6
  end
7
7
  end
@@ -1,13 +1,5 @@
1
- begin
2
- require 'sinatra/base'
3
- rescue LoadError
4
- raise LoadError, "couldn't load `sinatra/base`, check out if appropriately bundled sinatra gem?"
5
- end
6
-
1
+ require 'sinatra/base'
7
2
  require 'kaminari'
8
- module Kaminari::Helpers
9
- end
10
3
  require 'kaminari/helpers/sinatra_helpers'
11
4
 
12
- Kaminari::Hooks.init!
13
-
5
+ Kaminari::Hooks.init
@@ -1,3 +1,3 @@
1
1
  module Kaminari
2
- VERSION = '0.13.0'
2
+ VERSION = '0.14.0'
3
3
  end
@@ -17,6 +17,21 @@ describe Kaminari::Configuration do
17
17
  end
18
18
  end
19
19
 
20
+ describe 'max_per_page' do
21
+ context 'by default' do
22
+ its(:max_per_page) { should == nil }
23
+ end
24
+ context 'configure via config block' do
25
+ before do
26
+ Kaminari.configure {|c| c.max_per_page = 100}
27
+ end
28
+ its(:max_per_page) { should == 100 }
29
+ after do
30
+ Kaminari.configure {|c| c.max_per_page = nil}
31
+ end
32
+ end
33
+ end
34
+
20
35
  describe 'window' do
21
36
  context 'by default' do
22
37
  its(:window) { should == 4 }
@@ -0,0 +1,3 @@
1
+ # database
2
+ ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
3
+ ActiveRecord::Base.establish_connection('test')
@@ -1,29 +1,10 @@
1
- require 'active_record'
2
- require 'action_controller/railtie'
3
- require 'action_view/railtie'
4
-
5
- # database
6
- ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
7
- ActiveRecord::Base.establish_connection('test')
8
-
9
- # config
10
- app = Class.new(Rails::Application)
11
- app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
12
- app.config.session_store :cookie_store, :key => "_myapp_session"
13
- app.config.active_support.deprecation = :log
14
- app.initialize!
15
-
16
- # routes
17
- app.routes.draw do
18
- resources :users
19
- end
20
-
21
1
  # models
22
2
  class User < ActiveRecord::Base
23
3
  has_many :authorships
24
4
  has_many :readerships
25
5
  has_many :books_authored, :through => :authorships, :source => :book
26
6
  has_many :books_read, :through => :readerships, :source => :book
7
+ has_many :addresses, :class_name => 'User::Address'
27
8
 
28
9
  def readers
29
10
  User.joins(:books_read => :authors).where(:authors_books => {:id => self})
@@ -56,22 +37,11 @@ end
56
37
  # a model that is a descendant of AR::Base but doesn't directly inherit AR::Base
57
38
  class Admin < User
58
39
  end
59
-
60
- # controllers
61
- class ApplicationController < ActionController::Base; end
62
- class UsersController < ApplicationController
63
- def index
64
- @users = User.page params[:page]
65
- render :inline => <<-ERB
66
- <%= @users.map(&:name).join("\n") %>
67
- <%= paginate @users %>
68
- ERB
69
- end
40
+ # a model with namespace
41
+ class User::Address < ActiveRecord::Base
42
+ belongs_to :user
70
43
  end
71
44
 
72
- # helpers
73
- Object.const_set(:ApplicationHelper, Module.new)
74
-
75
45
  #migrations
76
46
  class CreateAllTables < ActiveRecord::Migration
77
47
  def self.up
@@ -80,5 +50,8 @@ class CreateAllTables < ActiveRecord::Migration
80
50
  create_table(:books) {|t| t.string :title}
81
51
  create_table(:readerships) {|t| t.integer :user_id; t.integer :book_id }
82
52
  create_table(:authorships) {|t| t.integer :user_id; t.integer :book_id }
53
+ create_table(:user_addresses) {|t| t.string :street; t.integer :user_id }
83
54
  end
84
55
  end
56
+ ActiveRecord::Migration.verbose = false
57
+ CreateAllTables.up
@@ -0,0 +1 @@
1
+ DataMapper.setup(:default, 'sqlite::memory:')
@@ -0,0 +1,27 @@
1
+ class User
2
+ include ::DataMapper::Resource
3
+
4
+ property :id, Serial
5
+ property :name, String, :required => true
6
+ property :age, Integer
7
+
8
+ has n, :projects, :through => Resource
9
+ end
10
+
11
+ class User::Address
12
+ include ::DataMapper::Resource
13
+
14
+ property :id, Serial
15
+ end
16
+
17
+ class Project
18
+ include ::DataMapper::Resource
19
+
20
+ property :id, Serial
21
+ property :name, String, :required => true
22
+
23
+ has n, :users, :through => Resource
24
+ end
25
+
26
+ DataMapper.finalize
27
+ DataMapper.auto_migrate!
@@ -0,0 +1,2 @@
1
+ MongoMapper.connection = Mongo::Connection.new 'localhost', 27017
2
+ MongoMapper.database = 'kaminari_test'
@@ -0,0 +1,9 @@
1
+ class User
2
+ include ::MongoMapper::Document
3
+ key :name, String
4
+ key :age, Integer
5
+ end
6
+
7
+ class User::Address
8
+ include ::MongoMapper::Document
9
+ end
@@ -0,0 +1,16 @@
1
+ # Ensure we use 'syck' instead of 'psych' in 1.9.2
2
+ # RubyGems >= 1.5.0 uses 'psych' on 1.9.2, but
3
+ # Psych does not yet support YAML 1.1 merge keys.
4
+ # Merge keys is often used in mongoid.yml
5
+ # See: http://redmine.ruby-lang.org/issues/show/4300
6
+ if RUBY_VERSION >= '1.9.2'
7
+ YAML::ENGINE.yamler = 'syck'
8
+ end
9
+
10
+ Mongoid.configure do |config|
11
+ if Mongoid::VERSION =~ /^3/
12
+ config.sessions = {:default => {:hosts => ['localhost:27017'], :database => 'kaminari_test'}}
13
+ else
14
+ config.master = Mongo::Connection.new.db('kaminari_test')
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ class User
2
+ include ::Mongoid::Document
3
+ field :name, :type => String
4
+ field :age, :type => Integer
5
+ end
6
+
7
+ class User::Address
8
+ include ::Mongoid::Document
9
+ end
10
+
11
+ class MongoMongoidExtensionDeveloper
12
+ include ::Mongoid::Document
13
+ field :salary, :type => Integer
14
+ embeds_many :frameworks
15
+ end
16
+
17
+ class Framework
18
+ include ::Mongoid::Document
19
+ field :name, :type => String
20
+ field :language, :type => String
21
+ embedded_in :mongo_mongoid_extension_developer
22
+ end
@@ -0,0 +1,55 @@
1
+ # require 'rails/all'
2
+ require 'action_controller/railtie'
3
+ require 'action_view/railtie'
4
+
5
+ require 'fake_app/active_record/config' if defined? ActiveRecord
6
+ require 'fake_app/data_mapper/config' if defined? DataMapper
7
+ require 'fake_app/mongoid/config' if defined? Mongoid
8
+ require 'fake_app/mongo_mapper/config' if defined? MongoMapper
9
+ # config
10
+ app = Class.new(Rails::Application)
11
+ app.config.secret_token = '3b7cd727ee24e8444053437c36cc66c4'
12
+ app.config.session_store :cookie_store, :key => '_myapp_session'
13
+ app.config.active_support.deprecation = :log
14
+ # Rais.root
15
+ app.config.root = File.dirname(__FILE__)
16
+ Rails.backtrace_cleaner.remove_silencers!
17
+ app.initialize!
18
+
19
+ # routes
20
+ app.routes.draw do
21
+ resources :users
22
+ end
23
+
24
+ #models
25
+ require 'fake_app/active_record/models' if defined? ActiveRecord
26
+ require 'fake_app/data_mapper/models' if defined? DataMapper
27
+ require 'fake_app/mongoid/models' if defined? Mongoid
28
+ require 'fake_app/mongo_mapper/models' if defined? MongoMapper
29
+
30
+ # controllers
31
+ class ApplicationController < ActionController::Base; end
32
+ class UsersController < ApplicationController
33
+ def index
34
+ @users = User.page params[:page]
35
+ render :inline => <<-ERB
36
+ <%= @users.map(&:name).join("\n") %>
37
+ <%= paginate @users %>
38
+ ERB
39
+ end
40
+ end
41
+
42
+ if defined? ActiveRecord
43
+ class AddressesController < ApplicationController
44
+ def index
45
+ @addresses = User::Address.page params[:page]
46
+ render :inline => <<-ERB
47
+ <%= @addresses.map(&:street).join("\n") %>
48
+ <%= paginate @addresses %>
49
+ ERB
50
+ end
51
+ end
52
+ end
53
+
54
+ # helpers
55
+ Object.const_set(:ApplicationHelper, Module.new)
@@ -0,0 +1,22 @@
1
+ require 'fake_app/active_record/config' if defined? ActiveRecord
2
+ require 'fake_app/data_mapper/config' if defined? DataMapper
3
+ require 'fake_app/mongoid/config' if defined? Mongoid
4
+ require 'fake_app/mongo_mapper/config' if defined? MongoMapper
5
+
6
+ #models
7
+ require 'fake_app/active_record/models' if defined? ActiveRecord
8
+ require 'fake_app/data_mapper/models' if defined? DataMapper
9
+ require 'fake_app/mongoid/models' if defined? Mongoid
10
+ require 'fake_app/mongo_mapper/models' if defined? MongoMapper
11
+
12
+ class SinatraApp < Sinatra::Base
13
+ register Kaminari::Helpers::SinatraHelpers
14
+
15
+ get '/users' do
16
+ @users = User.page params[:page]
17
+ erb <<-ERB
18
+ <%= @users.map(&:name).join("\n") %>
19
+ <%= paginate @users %>
20
+ ERB
21
+ end
22
+ end
@@ -1,6 +1,4 @@
1
1
  # Simulate a gem providing a subclass of ActiveRecord::Base before the Railtie is loaded.
2
2
 
3
- require 'active_record'
4
-
5
3
  class GemDefinedModel < ActiveRecord::Base
6
4
  end
@@ -11,11 +11,38 @@ describe 'Kaminari::ActionViewExtension' do
11
11
 
12
12
  context 'escaping the pagination for javascript' do
13
13
  it 'should escape for javascript' do
14
- lambda { escape_javascript(helper.paginate @users, :params => {:controller => 'users', :action => 'index'}) }.should_not raise_error
14
+ lambda { helper.escape_javascript(helper.paginate @users, :params => {:controller => 'users', :action => 'index'}) }.should_not raise_error
15
15
  end
16
16
  end
17
17
  end
18
18
 
19
+ describe '#link_to_previous_page' do
20
+ before do
21
+ 50.times {|i| User.create! :name => "user#{i}"}
22
+ end
23
+ context 'having previous pages' do
24
+ before do
25
+ @users = User.page(50)
26
+ end
27
+ context 'the default behaviour' do
28
+ subject { helper.link_to_previous_page @users, 'Previous', :params => {:controller => 'users', :action => 'index'} }
29
+ it { should be_a String }
30
+ it { should match(/rel="previous"/) }
31
+ end
32
+ context 'overriding rel=' do
33
+ subject { helper.link_to_previous_page @users, 'Previous', :rel => 'external', :params => {:controller => 'users', :action => 'index'} }
34
+ it { should match(/rel="external"/) }
35
+ end
36
+ end
37
+ context 'the first page' do
38
+ before do
39
+ @users = User.page(1)
40
+ end
41
+ subject { helper.link_to_previous_page @users, 'Previous', :params => {:controller => 'users', :action => 'index'} }
42
+ it { should_not be }
43
+ end
44
+ end
45
+
19
46
  describe '#link_to_next_page' do
20
47
  before do
21
48
  50.times {|i| User.create! :name => "user#{i}"}
@@ -27,11 +54,11 @@ describe 'Kaminari::ActionViewExtension' do
27
54
  context 'the default behaviour' do
28
55
  subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
29
56
  it { should be_a String }
30
- it { should match /rel="next"/ }
57
+ it { should match(/rel="next"/) }
31
58
  end
32
59
  context 'overriding rel=' do
33
60
  subject { helper.link_to_next_page @users, 'More', :rel => 'external', :params => {:controller => 'users', :action => 'index'} }
34
- it { should match /rel="external"/ }
61
+ it { should match(/rel="external"/) }
35
62
  end
36
63
  end
37
64
  context 'the last page' do
@@ -44,72 +71,192 @@ describe 'Kaminari::ActionViewExtension' do
44
71
  end
45
72
 
46
73
  describe '#page_entries_info' do
47
- before do
48
- @users = User.page(1).per(25)
49
- end
50
- context 'having no entries' do
51
- subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
52
- it { should == 'No entries found' }
53
- end
54
-
55
- context 'having 1 entry' do
56
- before do
57
- User.create!
58
- @users = User.page(1).per(25)
59
- end
60
- subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
61
- it { should == 'Displaying <b>1</b> user' }
62
-
63
- context 'setting the entry name option to "member"' do
64
- subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
65
- it { should == 'Displaying <b>1</b> member' }
66
- end
67
- end
68
-
69
- context 'having more than 1 but less than a page of entries' do
74
+ context 'on a model without namespace' do
70
75
  before do
71
- 10.times {|i| User.create!}
72
76
  @users = User.page(1).per(25)
73
77
  end
74
- subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
75
- it { should == 'Displaying <b>all 10</b> users' }
76
-
77
- context 'setting the entry name option to "member"' do
78
- subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
79
- it { should == 'Displaying <b>all 10</b> members' }
80
- end
81
- end
78
+ context 'having no entries' do
79
+ subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
80
+ it { should == 'No entries found' }
82
81
 
83
- context 'having more than one page of entries' do
84
- before do
85
- 50.times {|i| User.create!}
82
+ context 'setting the entry name option to "member"' do
83
+ subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
84
+ it { should == 'No members found' }
85
+ end
86
86
  end
87
87
 
88
- describe 'the first page' do
88
+ context 'having 1 entry' do
89
89
  before do
90
+ User.create! :name => 'user1'
90
91
  @users = User.page(1).per(25)
91
92
  end
92
93
  subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
93
- it { should == 'Displaying users <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
94
+ it { should == 'Displaying <b>1</b> user' }
94
95
 
95
96
  context 'setting the entry name option to "member"' do
96
97
  subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
97
- it { should == 'Displaying members <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
98
+ it { should == 'Displaying <b>1</b> member' }
98
99
  end
99
100
  end
100
101
 
101
- describe 'the next page' do
102
+ context 'having more than 1 but less than a page of entries' do
102
103
  before do
103
- @users = User.page(2).per(25)
104
+ 10.times {|i| User.create! :name => "user#{i}"}
105
+ @users = User.page(1).per(25)
104
106
  end
105
107
  subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
106
- it { should == 'Displaying users <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
108
+ it { should == 'Displaying <b>all 10</b> users' }
107
109
 
108
110
  context 'setting the entry name option to "member"' do
109
111
  subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
110
- it { should == 'Displaying members <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
112
+ it { should == 'Displaying <b>all 10</b> members' }
113
+ end
114
+ end
115
+
116
+ context 'having more than one page of entries' do
117
+ before do
118
+ 50.times {|i| User.create! :name => "user#{i}"}
119
+ end
120
+
121
+ describe 'the first page' do
122
+ before do
123
+ @users = User.page(1).per(25)
124
+ end
125
+ subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
126
+ it { should == 'Displaying users <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
127
+
128
+ context 'setting the entry name option to "member"' do
129
+ subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
130
+ it { should == 'Displaying members <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
131
+ end
132
+ end
133
+
134
+ describe 'the next page' do
135
+ before do
136
+ @users = User.page(2).per(25)
137
+ end
138
+ subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
139
+ it { should == 'Displaying users <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
140
+
141
+ context 'setting the entry name option to "member"' do
142
+ subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
143
+ it { should == 'Displaying members <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
144
+ end
111
145
  end
112
146
  end
113
147
  end
148
+ context 'on a model with namespace' do
149
+ before do
150
+ @addresses = User::Address.page(1).per(25)
151
+ end
152
+ context 'having no entries' do
153
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
154
+ it { should == 'No entries found' }
155
+ end
156
+
157
+ context 'having 1 entry' do
158
+ before do
159
+ User::Address.create!
160
+ @addresses = User::Address.page(1).per(25)
161
+ end
162
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
163
+ it { should == 'Displaying <b>1</b> address' }
164
+
165
+ context 'setting the entry name option to "place"' do
166
+ subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
167
+ it { should == 'Displaying <b>1</b> place' }
168
+ end
169
+ end
170
+
171
+ context 'having more than 1 but less than a page of entries' do
172
+ before do
173
+ 10.times {|i| User::Address.create!}
174
+ @addresses = User::Address.page(1).per(25)
175
+ end
176
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
177
+ it { should == 'Displaying <b>all 10</b> addresses' }
178
+
179
+ context 'setting the entry name option to "place"' do
180
+ subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
181
+ it { should == 'Displaying <b>all 10</b> places' }
182
+ end
183
+ end
184
+
185
+ context 'having more than one page of entries' do
186
+ before do
187
+ 50.times {|i| User::Address.create!}
188
+ end
189
+
190
+ describe 'the first page' do
191
+ before do
192
+ @addresses = User::Address.page(1).per(25)
193
+ end
194
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
195
+ it { should == 'Displaying addresses <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
196
+
197
+ context 'setting the entry name option to "place"' do
198
+ subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
199
+ it { should == 'Displaying places <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
200
+ end
201
+ end
202
+
203
+ describe 'the next page' do
204
+ before do
205
+ @addresses = User::Address.page(2).per(25)
206
+ end
207
+ subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
208
+ it { should == 'Displaying addresses <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
209
+
210
+ context 'setting the entry name option to "place"' do
211
+ subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
212
+ it { should == 'Displaying places <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
213
+ end
214
+ end
215
+ end
216
+ end
217
+
218
+ context 'on a PaginatableArray' do
219
+ before do
220
+ @numbers = Kaminari.paginate_array(%w{one two three}).page(1)
221
+ end
222
+ subject { helper.page_entries_info @numbers }
223
+ it { should == 'Displaying <b>all 3</b> entries' }
224
+ end
225
+ end
226
+
227
+ describe '#rel_next_prev_link_tags' do
228
+ before do
229
+ 75.times {|i| User.create! :name => "user#{i}"}
230
+ end
231
+ context 'the first page' do
232
+ before do
233
+ @users = User.page(1).per(25)
234
+ end
235
+
236
+ subject { helper.rel_next_prev_link_tags @users, :params => {:controller => 'users', :action => 'index'} }
237
+ it { should be_a String }
238
+ it { should match(/rel="next"/) }
239
+ it { should_not match(/rel="prev"/) }
240
+ end
241
+ context 'the middle page' do
242
+ before do
243
+ @users = User.page(2).per(25)
244
+ end
245
+
246
+ subject { helper.rel_next_prev_link_tags @users, :params => {:controller => 'users', :action => 'index'} }
247
+ it { should be_a String }
248
+ it { should match(/rel="next"/) }
249
+ it { should match(/rel="prev"/) }
250
+ end
251
+ context 'the last page' do
252
+ before do
253
+ @users = User.page(3).per(25)
254
+ end
255
+
256
+ subject { helper.rel_next_prev_link_tags @users, :params => {:controller => 'users', :action => 'index'} }
257
+ it { should be_a String }
258
+ it { should_not match(/rel="next"/) }
259
+ it { should match(/rel="prev"/) }
260
+ end
114
261
  end
115
262
  end