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
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Kaminari::ActiveRecordRelationMethods do
4
- describe '#total_count' do
5
- before do
6
- @author = User.create! :name => 'author'
7
- @author2 = User.create! :name => 'author2'
8
- @author3 = User.create! :name => 'author3'
9
- @books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
10
- @books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
11
- @books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
12
- @readers = 4.times.map { User.create! :name => 'reader' }
13
- @books.each {|book| book.readers << @readers }
14
- end
15
-
16
- context "when the scope includes an order which references a generated column" do
17
- it "should successfully count the results" do
18
- @author.readers.by_read_count.page(1).total_count.should == @readers.size
19
- end
20
- end
21
- context "when the scope use conditions on includes" do
22
- it "should keep includes and successfully count the results" do
23
- # Only @author and @author2 have books titled with the title00x partern
24
- User.includes(:books_authored).where("books.title LIKE 'title00%'").page(1).total_count.should == 2
25
- end
26
- end
27
- end
28
- end
@@ -1,181 +0,0 @@
1
- require 'spec_helper'
2
- require 'dm-core'
3
- require 'dm-migrations'
4
- require 'dm-aggregates'
5
- require 'kaminari/models/data_mapper_extension'
6
-
7
- describe Kaminari::DataMapperExtension do
8
- before :all do
9
- DataMapper.setup(:default, 'sqlite::memory:')
10
-
11
- class Worker
12
- include ::DataMapper::Resource
13
-
14
- property :id, Serial
15
- property :name, String, :required => true
16
- property :age, Integer, :required => true
17
-
18
- has n, :projects, :through => Resource
19
- end
20
-
21
- class Project
22
- include ::DataMapper::Resource
23
-
24
- property :id, Serial
25
- property :name, String, :required => true
26
-
27
- has n, :workers, :through => Resource
28
- end
29
-
30
- DataMapper.finalize
31
- DataMapper.auto_migrate!
32
-
33
- 300.times do |i|
34
- Worker.create(:name => "Worker#{i}", :age => i)
35
- end
36
-
37
- worker0 = Worker[0]
38
- 50.times do |i|
39
- worker0.projects << Project.create(:name => "Project#{i}")
40
- end
41
- worker0.projects.save
42
- end
43
-
44
- describe 'Collection' do
45
- subject{ Worker.all }
46
- it { should respond_to(:page) }
47
- it { should_not respond_to(:per) }
48
- end
49
-
50
- describe 'Model' do
51
- subject{ Worker }
52
- it { should respond_to(:page) }
53
- it { should respond_to(:default_per_page) }
54
- it { should_not respond_to(:per) }
55
- end
56
-
57
- describe '#page' do
58
- context 'page 0' do
59
- subject { Worker.all(:age.gte => 200).page 0 }
60
- it { should be_a DataMapper::Collection }
61
- its(:current_page) { should == 1 }
62
- its('query.limit') { should == 25 }
63
- its('query.offset') { should == 0 }
64
- its(:total_count) { should == Worker.count(:age.gte => 200) }
65
- its(:num_pages) { should == 4 }
66
- end
67
-
68
- context 'page 1' do
69
- subject { Worker.all(:age.gte => 0).page 1 }
70
- it { should be_a DataMapper::Collection }
71
- its(:current_page) { should == 1 }
72
- its('query.limit') { should == 25 }
73
- its('query.offset') { should == 0 }
74
- its(:total_count) { should == 300 }
75
- its(:num_pages) { should == 12 }
76
- end
77
-
78
- context 'page 2' do
79
- subject { Worker.page 2 }
80
- it { should be_a DataMapper::Collection }
81
- its(:current_page) { should == 2 }
82
- its(:limit_value) { should == 25 }
83
- its('query.limit') { should == 25 }
84
- its('query.offset') { should == 25 }
85
- its(:total_count) { should == 300 }
86
- its(:num_pages) { should == 12 }
87
- end
88
-
89
- context 'page "foobar"' do
90
- subject { Worker.page 'foobar' }
91
- it { should be_a DataMapper::Collection }
92
- its(:current_page) { should == 1 }
93
- its('query.limit') { should == 25 }
94
- its('query.offset') { should == 0 }
95
- its(:total_count) { should == 300 }
96
- its(:num_pages) { should == 12 }
97
- end
98
-
99
- context 'with criteria before' do
100
- subject { Worker.all(:age.gt => 100).page 2 }
101
- it { should be_a DataMapper::Collection }
102
- its(:current_page) { should == 2 }
103
- its('query.limit') { should == 25 }
104
- its('query.offset') { should == 25 }
105
- its(:total_count) { should == Worker.count(:age.gt => 100) }
106
- its(:num_pages) { should == 8 }
107
- end
108
-
109
- context 'with criteria after' do
110
- subject { Worker.page(2).all(:age.gt => 100) }
111
- it { should be_a DataMapper::Collection }
112
- its(:current_page) { should == 2 }
113
- its('query.limit') { should == 25 }
114
- its('query.offset') { should == 25 }
115
- its(:total_count) { should == Worker.count(:age.gt => 100) }
116
- its(:num_pages) { should == 8 }
117
- end
118
- end
119
-
120
- describe '#per' do
121
- context 'on simple query' do
122
- subject { Worker.page(2).per(10) }
123
- it { should be_a DataMapper::Collection }
124
- its(:current_page) { should == 2 }
125
- its('query.limit') { should == 10 }
126
- its(:limit_value) { should == 10 }
127
- its('query.offset') { should == 10 }
128
- its(:total_count) { should == 300 }
129
- its(:num_pages) { should == 30 }
130
- end
131
-
132
- context 'on query with condition' do
133
- subject { Worker.page(5).all(:age.lte => 100).per(13) }
134
- its(:current_page) { should == 5 }
135
- its('query.limit') { should == 13 }
136
- its('query.offset') { should == 52 }
137
- its(:total_count) { should == 101 }
138
- its(:num_pages) { should == 8 }
139
- end
140
-
141
- context 'on query with order' do
142
- subject { Worker.page(5).all(:age.lte => 100, :order => [:age.asc]).per(13) }
143
- it('includes worker with age 52') { should include(Worker.first(:age => 52)) }
144
- it('does not include worker with age 51') { should_not include(Worker.first(:age => 51)) }
145
- it('includes worker with age 52') { should include(Worker.first(:age => 64)) }
146
- it('does not include worker with age 51') { should_not include(Worker.first(:age => 65)) }
147
- its(:current_page) { should == 5 }
148
- its('query.limit') { should == 13 }
149
- its('query.offset') { should == 52 }
150
- its(:total_count) { should == 101 }
151
- its(:num_pages) { should == 8 }
152
- end
153
-
154
- context 'on chained queries' do
155
- subject { Worker.all(:age.gte => 50).page(3).all(:age.lte => 100).per(13) }
156
- its(:current_page) { should == 3 }
157
- its('query.limit') { should == 13 }
158
- its('query.offset') { should == 26 }
159
- its(:total_count) { should == 51 }
160
- its(:num_pages) { should == 4 }
161
- end
162
-
163
- context 'on query on association' do
164
- subject { Worker[0].projects.page(3).all(:name.like => 'Project%').per(5) }
165
- its(:current_page) { should == 3 }
166
- its('query.limit') { should == 5 }
167
- its('query.offset') { should == 10 }
168
- its(:total_count) { should == 50 }
169
- its(:num_pages) { should == 10 }
170
- end
171
-
172
- context 'on query with association conditions' do
173
- subject { Worker.page(3).all(:projects => Project.all).per(5) }
174
- its(:current_page) { should == 3 }
175
- its('query.limit') { should == 5 }
176
- its('query.offset') { should == 10 }
177
- its(:total_count) { should == 50 }
178
- its(:num_pages) { should == 10 }
179
- end
180
- end
181
- end
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'default per_page' do
4
- describe 'AR::Base' do
5
- subject { ActiveRecord::Base }
6
- it { should_not respond_to :paginates_per }
7
- end
8
-
9
- subject { User.page 0 }
10
-
11
- context 'by default' do
12
- its(:limit_value) { should == 25 }
13
- end
14
-
15
- context 'when explicitly set via paginates_per' do
16
- before { User.paginates_per 1326 }
17
- its(:limit_value) { should == 1326 }
18
- after { User.paginates_per nil }
19
- end
20
-
21
- describe "default per_page value's independency per model" do
22
- context "when User's default per_page was changed" do
23
- before { User.paginates_per 1326 }
24
- subject { Book.page 0 }
25
- its(:limit_value) { should == 25 }
26
- after { User.paginates_per nil }
27
- end
28
- end
29
- end
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
- require 'mongo_mapper'
3
- require 'kaminari/models/mongo_mapper_extension'
4
-
5
- describe Kaminari::MongoMapperExtension do
6
- before do
7
- begin
8
- MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
9
- MongoMapper.database = "kaminari_test"
10
- class Developer
11
- include ::MongoMapper::Document
12
- key :salary, Integer
13
- end
14
-
15
- stub(subject).count { 300 } # in order to avoid DB access...
16
- rescue Mongo::ConnectionFailure
17
- pending 'can not connect to MongoDB'
18
- end
19
- end
20
-
21
- describe '#page' do
22
- context 'page 1' do
23
- subject { Developer.page(1) }
24
- it { should be_a Plucky::Query }
25
- its(:current_page) { should == 1 }
26
- its(:limit_value) { should == 25 }
27
- its(:num_pages) { should == 12 }
28
- it { should skip(0) }
29
- end
30
-
31
- context 'page 2' do
32
- subject { Developer.page 2 }
33
- it { should be_a Plucky::Query }
34
- its(:current_page) { should == 2 }
35
- its(:limit_value) { should == 25 }
36
- its(:num_pages) { should == 12 }
37
- it { should skip 25 }
38
- end
39
-
40
- context 'page "foobar"' do
41
- subject { Developer.page 'foobar' }
42
- it { should be_a Plucky::Query }
43
- its(:current_page) { should == 1 }
44
- its(:limit_value) { should == 25 }
45
- its(:num_pages) { should == 12 }
46
- it { should skip 0 }
47
- end
48
-
49
- context 'with criteria before' do
50
- it "should have the proper criteria source" do
51
- Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
52
- end
53
-
54
- subject { Developer.where(:salary => 1).page 2 }
55
- its(:current_page) { should == 2 }
56
- its(:limit_value) { should == 25 }
57
- its(:num_pages) { should == 12 }
58
- it { should skip 25 }
59
- end
60
-
61
- context 'with criteria after' do
62
- it "should have the proper criteria source" do
63
- Developer.where(:salary => 1).page(2).criteria.source.should == {:salary => 1}
64
- end
65
-
66
- subject { Developer.page(2).where(:salary => 1) }
67
- its(:current_page) { should == 2 }
68
- its(:limit_value) { should == 25 }
69
- its(:num_pages) { should == 12 }
70
- it { should skip 25 }
71
- end
72
- end
73
-
74
- describe '#per' do
75
- subject { Developer.page(2).per(10) }
76
- it { should be_a Plucky::Query }
77
- its(:current_page) { should == 2 }
78
- its(:limit_value) { should == 10 }
79
- its(:num_pages) { should == 30 }
80
- it { should skip 10 }
81
- end
82
- end
@@ -1,129 +0,0 @@
1
- require 'spec_helper'
2
- require 'mongoid'
3
- require 'kaminari/models/mongoid_extension'
4
-
5
- describe Kaminari::MongoidExtension do
6
- before :all do
7
- class Developer
8
- include ::Mongoid::Document
9
- field :salary, :type => Integer
10
- end
11
- end
12
-
13
- describe '#page' do
14
- before do
15
- stub(subject).count { 300 } # in order to avoid DB access...
16
- end
17
-
18
- context 'page 1' do
19
- subject { Developer.page 1 }
20
- it { should be_a Mongoid::Criteria }
21
- its(:current_page) { should == 1 }
22
- its(:limit_value) { should == 25 }
23
- its(:num_pages) { should == 12 }
24
- it { should skip(0) }
25
- end
26
-
27
- context 'page 2' do
28
- subject { Developer.page 2 }
29
- it { should be_a Mongoid::Criteria }
30
- its(:current_page) { should == 2 }
31
- its(:limit_value) { should == 25 }
32
- its(:num_pages) { should == 12 }
33
- it { should skip 25 }
34
- end
35
-
36
- context 'page "foobar"' do
37
- subject { Developer.page 'foobar' }
38
- it { should be_a Mongoid::Criteria }
39
- its(:current_page) { should == 1 }
40
- its(:limit_value) { should == 25 }
41
- its(:num_pages) { should == 12 }
42
- it { should skip 0 }
43
- end
44
-
45
- context 'with criteria before' do
46
- subject { Developer.where(:salary => 1).page 2 }
47
- its(:selector) { should == {:salary => 1} }
48
- its(:current_page) { should == 2 }
49
- its(:limit_value) { should == 25 }
50
- its(:num_pages) { should == 12 }
51
- it { should skip 25 }
52
- end
53
-
54
- context 'with criteria after' do
55
- subject { Developer.page(2).where(:salary => 1) }
56
- its(:selector) { should == {:salary => 1} }
57
- its(:current_page) { should == 2 }
58
- its(:limit_value) { should == 25 }
59
- its(:num_pages) { should == 12 }
60
- it { should skip 25 }
61
- end
62
- end
63
-
64
- describe '#per' do
65
- before do
66
- stub(subject).count { 300 } # in order to avoid DB access...
67
- end
68
-
69
- subject { Developer.page(2).per(10) }
70
- it { should be_a Mongoid::Criteria }
71
- its(:current_page) { should == 2 }
72
- its(:limit_value) { should == 10 }
73
- its(:num_pages) { should == 30 }
74
- it { should skip 10 }
75
- end
76
-
77
- describe '#page in embedded documents' do
78
- before :all do
79
- class MongoDeveloper
80
- include ::Mongoid::Document
81
- field :salary, :type => Integer
82
- embeds_many :frameworks
83
- end
84
-
85
- class Framework
86
- include ::Mongoid::Document
87
- field :name, :type => String
88
- field :language, :type => String
89
- embedded_in :mongo_developer
90
- end
91
- end
92
-
93
- before :all do
94
- @mongo_developer = MongoDeveloper.new
95
- @mongo_developer.frameworks.new(:name => "rails", :language => "ruby")
96
- @mongo_developer.frameworks.new(:name => "merb", :language => "ruby")
97
- @mongo_developer.frameworks.new(:name => "sinatra", :language => "ruby")
98
- @mongo_developer.frameworks.new(:name => "cakephp", :language => "php")
99
- @mongo_developer.frameworks.new(:name => "tornado", :language => "python")
100
- end
101
-
102
- context 'page 1' do
103
- subject { @mongo_developer.frameworks.page(1).per(1) }
104
- it { should be_a Mongoid::Criteria }
105
- its(:total_count) { should == 5 }
106
- its(:limit_value) { should == 1 }
107
- its(:current_page) { should == 1 }
108
- its(:num_pages) { should == 5 }
109
- end
110
-
111
- context 'with criteria after' do
112
- subject { @mongo_developer.frameworks.page(1).per(2).where(:language => "ruby") }
113
- it { should be_a Mongoid::Criteria }
114
- its(:total_count) { should == 3 }
115
- its(:limit_value) { should == 2 }
116
- its(:current_page) { should == 1 }
117
- its(:num_pages) { should == 2 }
118
- end
119
-
120
- context 'with criteria before' do
121
- subject { @mongo_developer.frameworks.where(:language => "ruby").page(1).per(2) }
122
- it { should be_a Mongoid::Criteria }
123
- its(:total_count) { should == 3 }
124
- its(:limit_value) { should == 2 }
125
- its(:current_page) { should == 1 }
126
- its(:num_pages) { should == 2 }
127
- end
128
- end
129
- end