kaminari 0.17.0 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +19 -0
  3. data/.github/workflows/main.yml +107 -0
  4. data/.gitignore +2 -1
  5. data/CHANGELOG.md +650 -0
  6. data/CONTRIBUTING.md +22 -0
  7. data/Gemfile +2 -0
  8. data/README.md +588 -0
  9. data/Rakefile +25 -25
  10. data/gemfiles/active_record_41.gemfile +17 -12
  11. data/gemfiles/active_record_42.gemfile +17 -12
  12. data/gemfiles/active_record_50.gemfile +26 -0
  13. data/gemfiles/active_record_51.gemfile +26 -0
  14. data/gemfiles/active_record_52.gemfile +26 -0
  15. data/gemfiles/active_record_60.gemfile +28 -0
  16. data/gemfiles/active_record_61.gemfile +28 -0
  17. data/gemfiles/active_record_70.gemfile +28 -0
  18. data/gemfiles/active_record_edge.gemfile +22 -17
  19. data/kaminari.gemspec +23 -27
  20. data/lib/kaminari/version.rb +3 -1
  21. data/lib/kaminari.rb +4 -38
  22. data/misc/bug_report_template.rb +57 -0
  23. metadata +54 -190
  24. data/.gemtest +0 -0
  25. data/.rspec +0 -2
  26. data/.travis.yml +0 -64
  27. data/CHANGELOG.rdoc +0 -480
  28. data/README.rdoc +0 -336
  29. data/app/views/kaminari/_first_page.html.erb +0 -11
  30. data/app/views/kaminari/_first_page.html.haml +0 -9
  31. data/app/views/kaminari/_first_page.html.slim +0 -10
  32. data/app/views/kaminari/_gap.html.erb +0 -8
  33. data/app/views/kaminari/_gap.html.haml +0 -8
  34. data/app/views/kaminari/_gap.html.slim +0 -9
  35. data/app/views/kaminari/_last_page.html.erb +0 -11
  36. data/app/views/kaminari/_last_page.html.haml +0 -9
  37. data/app/views/kaminari/_last_page.html.slim +0 -10
  38. data/app/views/kaminari/_next_page.html.erb +0 -11
  39. data/app/views/kaminari/_next_page.html.haml +0 -9
  40. data/app/views/kaminari/_next_page.html.slim +0 -10
  41. data/app/views/kaminari/_page.html.erb +0 -12
  42. data/app/views/kaminari/_page.html.haml +0 -10
  43. data/app/views/kaminari/_page.html.slim +0 -11
  44. data/app/views/kaminari/_paginator.html.erb +0 -23
  45. data/app/views/kaminari/_paginator.html.haml +0 -18
  46. data/app/views/kaminari/_paginator.html.slim +0 -19
  47. data/app/views/kaminari/_prev_page.html.erb +0 -11
  48. data/app/views/kaminari/_prev_page.html.haml +0 -9
  49. data/app/views/kaminari/_prev_page.html.slim +0 -10
  50. data/config/locales/kaminari.yml +0 -19
  51. data/gemfiles/active_record_30.gemfile +0 -28
  52. data/gemfiles/active_record_31.gemfile +0 -26
  53. data/gemfiles/active_record_32.gemfile +0 -25
  54. data/gemfiles/active_record_40.gemfile +0 -26
  55. data/gemfiles/data_mapper_12.gemfile +0 -32
  56. data/gemfiles/mongo_mapper.gemfile +0 -25
  57. data/gemfiles/mongoid_30.gemfile +0 -22
  58. data/gemfiles/mongoid_31.gemfile +0 -21
  59. data/gemfiles/mongoid_40.gemfile +0 -19
  60. data/gemfiles/mongoid_50.gemfile +0 -23
  61. data/gemfiles/sinatra_13.gemfile +0 -36
  62. data/gemfiles/sinatra_14.gemfile +0 -33
  63. data/lib/generators/kaminari/config_generator.rb +0 -16
  64. data/lib/generators/kaminari/templates/kaminari_config.rb +0 -10
  65. data/lib/generators/kaminari/views_generator.rb +0 -119
  66. data/lib/kaminari/config.rb +0 -51
  67. data/lib/kaminari/engine.rb +0 -4
  68. data/lib/kaminari/grape.rb +0 -4
  69. data/lib/kaminari/helpers/action_view_extension.rb +0 -132
  70. data/lib/kaminari/helpers/paginator.rb +0 -195
  71. data/lib/kaminari/helpers/sinatra_helpers.rb +0 -176
  72. data/lib/kaminari/helpers/tags.rb +0 -108
  73. data/lib/kaminari/hooks.rb +0 -53
  74. data/lib/kaminari/models/active_record_extension.rb +0 -22
  75. data/lib/kaminari/models/active_record_model_extension.rb +0 -22
  76. data/lib/kaminari/models/active_record_relation_methods.rb +0 -42
  77. data/lib/kaminari/models/array_extension.rb +0 -66
  78. data/lib/kaminari/models/configuration_methods.rb +0 -48
  79. data/lib/kaminari/models/data_mapper_collection_methods.rb +0 -19
  80. data/lib/kaminari/models/data_mapper_extension.rb +0 -51
  81. data/lib/kaminari/models/mongo_mapper_extension.rb +0 -18
  82. data/lib/kaminari/models/mongoid_criteria_methods.rb +0 -40
  83. data/lib/kaminari/models/mongoid_extension.rb +0 -26
  84. data/lib/kaminari/models/page_scope_methods.rb +0 -73
  85. data/lib/kaminari/models/plucky_criteria_methods.rb +0 -23
  86. data/lib/kaminari/railtie.rb +0 -7
  87. data/lib/kaminari/sinatra.rb +0 -7
  88. data/spec/config/config_spec.rb +0 -91
  89. data/spec/fake_app/active_record/config.rb +0 -3
  90. data/spec/fake_app/active_record/models.rb +0 -65
  91. data/spec/fake_app/data_mapper/config.rb +0 -1
  92. data/spec/fake_app/data_mapper/models.rb +0 -27
  93. data/spec/fake_app/mongo_mapper/config.rb +0 -2
  94. data/spec/fake_app/mongo_mapper/models.rb +0 -9
  95. data/spec/fake_app/mongoid/config.rb +0 -20
  96. data/spec/fake_app/mongoid/models.rb +0 -38
  97. data/spec/fake_app/rails_app.rb +0 -56
  98. data/spec/fake_app/sinatra_app.rb +0 -22
  99. data/spec/fake_app/views/alternative/kaminari/_first_page.html.erb +0 -1
  100. data/spec/fake_app/views/alternative/kaminari/_paginator.html.erb +0 -3
  101. data/spec/fake_app/views/kaminari/bootstrap/_page.html.erb +0 -1
  102. data/spec/fake_app/views/kaminari/bootstrap/_paginator.html.erb +0 -7
  103. data/spec/fake_gem.rb +0 -20
  104. data/spec/generators/views_generator_spec.rb +0 -18
  105. data/spec/helpers/action_view_extension_spec.rb +0 -318
  106. data/spec/helpers/helpers_spec.rb +0 -144
  107. data/spec/helpers/sinatra_helpers_spec.rb +0 -223
  108. data/spec/helpers/tags_spec.rb +0 -140
  109. data/spec/models/active_record/active_record_relation_methods_spec.rb +0 -77
  110. data/spec/models/active_record/inherited_spec.rb +0 -9
  111. data/spec/models/active_record/scopes_spec.rb +0 -265
  112. data/spec/models/array_spec.rb +0 -172
  113. data/spec/models/configuration_methods_spec.rb +0 -125
  114. data/spec/models/data_mapper/data_mapper_spec.rb +0 -205
  115. data/spec/models/mongo_mapper/mongo_mapper_spec.rb +0 -84
  116. data/spec/models/mongoid/mongoid_spec.rb +0 -228
  117. data/spec/requests/users_spec.rb +0 -53
  118. data/spec/spec_helper.rb +0 -34
  119. data/spec/spec_helper_for_sinatra.rb +0 -34
  120. data/spec/support/database_cleaner.rb +0 -19
  121. data/spec/support/matchers.rb +0 -52
data/README.md ADDED
@@ -0,0 +1,588 @@
1
+ # Kaminari [![Build Status](https://github.com/kaminari/kaminari/actions/workflows/main.yml/badge.svg)](https://github.com/kaminari/kaminari/actions) [![Code Climate](https://codeclimate.com/github/kaminari/kaminari/badges/gpa.svg)](https://codeclimate.com/github/kaminari/kaminari)
2
+
3
+ A Scope & Engine based, clean, powerful, customizable and sophisticated paginator for modern web app frameworks and ORMs
4
+
5
+ ## Features
6
+
7
+ ### Clean
8
+ Does not globally pollute `Array`, `Hash`, `Object` or `AR::Base`.
9
+
10
+ ### Easy to Use
11
+ Just bundle the gem, then your models are ready to be paginated.
12
+ No configuration required.
13
+ Don't have to define anything in your models or helpers.
14
+
15
+ ### Simple Scope-based API
16
+ Everything is method chainable with less "Hasheritis". You know, that's the modern Rails way.
17
+ No special collection class or anything for the paginated values, instead using a general `AR::Relation` instance.
18
+ So, of course you can chain any other conditions before or after the paginator scope.
19
+
20
+ ### Customizable Engine-based I18n-aware Helpers
21
+ As the whole pagination helper is basically just a collection of links and non-links, Kaminari renders each of them through its own partial template inside the Engine.
22
+ So, you can easily modify their behaviour, style or whatever by overriding partial templates.
23
+
24
+ ### ORM & Template Engine Agnostic
25
+ Kaminari supports multiple ORMs (ActiveRecord, DataMapper, Mongoid, MongoMapper) multiple web frameworks (Rails, Sinatra, Grape), and multiple template engines (ERB, Haml, Slim).
26
+
27
+ ### Modern
28
+ The pagination helper outputs the HTML5 `<nav>` tag by default. Plus, the helper supports Rails unobtrusive Ajax.
29
+
30
+
31
+ ## Supported Versions
32
+
33
+ * Ruby 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1
34
+
35
+ * Rails 4.1, 4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1
36
+
37
+ * Sinatra 1.4, 2.0
38
+
39
+ * Haml 3+
40
+
41
+ * Mongoid 3+
42
+
43
+ * MongoMapper 0.9+
44
+
45
+ * DataMapper 1.1.0+
46
+
47
+
48
+ ## Installation
49
+
50
+ To install kaminari on the default Rails stack, just put this line in your Gemfile:
51
+
52
+ ```ruby
53
+ gem 'kaminari'
54
+ ```
55
+
56
+ Then bundle:
57
+
58
+ ```sh
59
+ % bundle
60
+ ```
61
+
62
+ If you're building non-Rails of non-ActiveRecord app and want the pagination feature on it, please take a look at [Other Framework/Library Support](#other-frameworklibrary-support) section.
63
+
64
+
65
+ ## Query Basics
66
+
67
+ ### The `page` Scope
68
+
69
+ To fetch the 7th page of users (default `per_page` is 25)
70
+
71
+ ```ruby
72
+ User.page(7)
73
+ ```
74
+
75
+ Note: pagination starts at page 1, not at page 0 (page(0) will return the same results as page(1)).
76
+
77
+ Kaminari does not add an `order` to queries. To avoid surprises, you should generally include an order in paginated queries. For example:
78
+
79
+ ```ruby
80
+ User.order(:name).page(7)
81
+ ```
82
+
83
+ You can get page numbers or page conditions by using below methods.
84
+ ```ruby
85
+ User.count #=> 1000
86
+ User.page(1).limit_value #=> 20
87
+ User.page(1).total_pages #=> 50
88
+ User.page(1).current_page #=> 1
89
+ User.page(1).next_page #=> 2
90
+ User.page(2).prev_page #=> 1
91
+ User.page(1).first_page? #=> true
92
+ User.page(50).last_page? #=> true
93
+ User.page(100).out_of_range? #=> true
94
+ ```
95
+
96
+ ### The `per` Scope
97
+
98
+ To show a lot more users per each page (change the `per` value)
99
+
100
+ ```ruby
101
+ User.order(:name).page(7).per(50)
102
+ ```
103
+
104
+ Note that the `per` scope is not directly defined on the models but is just a method defined on the page scope.
105
+ This is absolutely reasonable because you will never actually use `per` without specifying the `page` number.
106
+
107
+ Keep in mind that `per` internally utilizes `limit` and so it will override any `limit` that was set previously.
108
+ And if you want to get the size for all request records you can use `total_count` method:
109
+
110
+ ```ruby
111
+ User.count #=> 1000
112
+ a = User.limit(5); a.count #=> 5
113
+ a.page(1).per(20).size #=> 20
114
+ a.page(1).per(20).total_count #=> 1000
115
+ ```
116
+
117
+ ### The `padding` Scope
118
+
119
+ Occasionally you need to pad a number of records that is not a multiple of the page size.
120
+
121
+ ```ruby
122
+ User.order(:name).page(7).per(50).padding(3)
123
+ ```
124
+
125
+ Note that the `padding` scope also is not directly defined on the models.
126
+
127
+ ### Unscoping
128
+
129
+ If for some reason you need to unscope `page` and `per` methods you can call `except(:limit, :offset)`
130
+
131
+ ```ruby
132
+ users = User.order(:name).page(7).per(50)
133
+ unpaged_users = users.except(:limit, :offset) # unpaged_users will not use the kaminari scopes
134
+ ```
135
+
136
+ ## Configuring Kaminari
137
+
138
+ ### General Configuration Options
139
+
140
+ You can configure the following default values by overriding these values using `Kaminari.configure` method.
141
+
142
+ default_per_page # 25 by default
143
+ max_per_page # nil by default
144
+ max_pages # nil by default
145
+ window # 4 by default
146
+ outer_window # 0 by default
147
+ left # 0 by default
148
+ right # 0 by default
149
+ page_method_name # :page by default
150
+ param_name # :page by default
151
+ params_on_first_page # false by default
152
+
153
+ There's a handy generator that generates the default configuration file into config/initializers directory.
154
+ Run the following generator command, then edit the generated file.
155
+
156
+ ```sh
157
+ % rails g kaminari:config
158
+ ```
159
+
160
+ ### Changing `page_method_name`
161
+
162
+ You can change the method name `page` to `bonzo` or `plant` or whatever you like, in order to play nice with existing `page` method or association or scope or any other plugin that defines `page` method on your models.
163
+
164
+
165
+ ### Configuring Default per_page Value for Each Model by `paginates_per`
166
+
167
+ You can specify default `per_page` value per each model using the following declarative DSL.
168
+
169
+ ```ruby
170
+ class User < ActiveRecord::Base
171
+ paginates_per 50
172
+ end
173
+ ```
174
+
175
+ ### Configuring Max per_page Value for Each Model by `max_paginates_per`
176
+
177
+ You can specify max `per_page` value per each model using the following declarative DSL.
178
+ If the variable that specified via `per` scope is more than this variable, `max_paginates_per` is used instead of it.
179
+ Default value is nil, which means you are not imposing any max `per_page` value.
180
+
181
+ ```ruby
182
+ class User < ActiveRecord::Base
183
+ max_paginates_per 100
184
+ end
185
+ ```
186
+
187
+ ### Configuring params_on_first_page when using ransack_memory
188
+
189
+ If you are using [the `ransack_memory` gem](https://github.com/richardrails/ransack_memory) and experience problems navigating back to the previous or first page, set the `params_on_first_page` setting to `true`.
190
+
191
+ ## Controllers
192
+
193
+ ### The Page Parameter Is in `params[:page]`
194
+
195
+ Typically, your controller code will look like this:
196
+
197
+ ```ruby
198
+ @users = User.order(:name).page params[:page]
199
+ ```
200
+
201
+
202
+ ## Views
203
+
204
+ ### The Same Old Helper Method
205
+
206
+ Just call the `paginate` helper:
207
+
208
+ ```erb
209
+ <%= paginate @users %>
210
+ ```
211
+
212
+ This will render several `?page=N` pagination links surrounded by an HTML5 `<nav>` tag.
213
+
214
+
215
+ ## Helpers
216
+
217
+ ### The `paginate` Helper Method
218
+
219
+ ```erb
220
+ <%= paginate @users %>
221
+ ```
222
+
223
+ This would output several pagination links such as `« First ‹ Prev ... 2 3 4 5 6 7 8 9 10 ... Next › Last »`
224
+
225
+ ### Specifying the "inner window" Size (4 by default)
226
+
227
+ ```erb
228
+ <%= paginate @users, window: 2 %>
229
+ ```
230
+
231
+ This would output something like `... 5 6 7 8 9 ...` when 7 is the current
232
+ page.
233
+
234
+ ### Specifying the "outer window" Size (0 by default)
235
+
236
+ ```erb
237
+ <%= paginate @users, outer_window: 3 %>
238
+ ```
239
+
240
+ This would output something like `1 2 3 ...(snip)... 18 19 20` while having 20 pages in total.
241
+
242
+ ### Outer Window Can Be Separately Specified by left, right (0 by default)
243
+
244
+ ```erb
245
+ <%= paginate @users, left: 1, right: 3 %>
246
+ ```
247
+
248
+ This would output something like `1 ...(snip)... 18 19 20` while having 20 pages in total.
249
+
250
+ ### Changing the Parameter Name (`:param_name`) for the Links
251
+
252
+ ```erb
253
+ <%= paginate @users, param_name: :pagina %>
254
+ ```
255
+
256
+ This would modify the query parameter name on each links.
257
+
258
+ ### Extra Parameters (`:params`) for the Links
259
+
260
+ ```erb
261
+ <%= paginate @users, params: {controller: 'foo', action: 'bar', format: :turbo_stream} %>
262
+ ```
263
+
264
+ This would modify each link's `url_option`. :`controller` and :`action` might be the keys in common.
265
+
266
+ ### Ajax Links (crazy simple, but works perfectly!)
267
+
268
+ ```erb
269
+ <%= paginate @users, remote: true %>
270
+ ```
271
+
272
+ This would add `data-remote="true"` to all the links inside.
273
+
274
+ ### Specifying an Alternative Views Directory (default is kaminari/)
275
+
276
+ ```erb
277
+ <%= paginate @users, views_prefix: 'templates' %>
278
+ ```
279
+
280
+ This would search for partials in `app/views/templates/kaminari`.
281
+ This option makes it easier to do things like A/B testing pagination templates/themes, using new/old templates at the same time as well as better integration with other gems such as [cells](https://github.com/apotonick/cells).
282
+
283
+ ### The `link_to_next_page` and `link_to_previous_page` (aliased to `link_to_prev_page`) Helper Methods
284
+
285
+ ```erb
286
+ <%= link_to_next_page @items, 'Next Page' %>
287
+ ```
288
+
289
+ This simply renders a link to the next page. This would be helpful for creating a Twitter-like pagination feature.
290
+
291
+ The helper methods support a `params` option to further specify the link. If `format` needs to be set, inlude it in the `params` hash.
292
+
293
+ ```erb
294
+ <%= link_to_next_page @items, 'Next Page', params: {controller: 'foo', action: 'bar', format: :turbo_stream} %>
295
+ ```
296
+
297
+ ### The `page_entries_info` Helper Method
298
+
299
+ ```erb
300
+ <%= page_entries_info @posts %>
301
+ ```
302
+
303
+ This renders a helpful message with numbers of displayed vs. total entries.
304
+
305
+ By default, the message will use the humanized class name of objects in collection: for instance, "project types" for ProjectType models.
306
+ The namespace will be cut out and only the last name will be used. Override this with the `:entry_name` parameter:
307
+
308
+ ```erb
309
+ <%= page_entries_info @posts, entry_name: 'item' %>
310
+ #=> Displaying items 6 - 10 of 26 in total
311
+ ```
312
+
313
+ ### The `rel_next_prev_link_tags` Helper Method
314
+
315
+ ```erb
316
+ <%= rel_next_prev_link_tags @users %>
317
+ ```
318
+
319
+ This renders the rel next and prev link tags for the head.
320
+
321
+ ### The `path_to_next_page` Helper Method
322
+
323
+ ```erb
324
+ <%= path_to_next_page @users %>
325
+ ```
326
+
327
+ This returns the server relative path to the next page.
328
+
329
+ ### The `path_to_prev_page` Helper Method
330
+
331
+ ```erb
332
+ <%= path_to_prev_page @users %>
333
+ ```
334
+
335
+ This returns the server relative path to the previous page.
336
+
337
+
338
+ ## I18n and Labels
339
+
340
+ The default labels for 'first', 'last', 'previous', '...' and 'next' are stored in the I18n yaml inside the engine, and rendered through I18n API.
341
+ You can switch the label value per I18n.locale for your internationalized application. Keys and the default values are the following. You can override them by adding to a YAML file in your `Rails.root/config/locales` directory.
342
+
343
+ ```yaml
344
+ en:
345
+ views:
346
+ pagination:
347
+ first: "&laquo; First"
348
+ last: "Last &raquo;"
349
+ previous: "&lsaquo; Prev"
350
+ next: "Next &rsaquo;"
351
+ truncate: "&hellip;"
352
+ helpers:
353
+ page_entries_info:
354
+ one_page:
355
+ display_entries:
356
+ zero: "No %{entry_name} found"
357
+ one: "Displaying <b>1</b> %{entry_name}"
358
+ other: "Displaying <b>all %{count}</b> %{entry_name}"
359
+ more_pages:
360
+ display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
361
+ ```
362
+
363
+ If you use non-English localization see [i18n rules](https://github.com/svenfuchs/i18n/blob/master/test/test_data/locales/plurals.rb) for changing
364
+ `one_page:display_entries` block.
365
+
366
+
367
+ ## Customizing the Pagination Helper
368
+
369
+ Kaminari includes a handy template generator.
370
+
371
+ ### To Edit Your Paginator
372
+
373
+ Run the generator first,
374
+
375
+ ```sh
376
+ % rails g kaminari:views default
377
+ ```
378
+
379
+ then edit the partials in your app's `app/views/kaminari/` directory.
380
+
381
+ ### For Haml/Slim Users
382
+
383
+ You can use the [html2haml gem](https://github.com/haml/html2haml) or the [html2slim gem](https://github.com/slim-template/html2slim) to convert erb templates.
384
+ The kaminari gem will automatically pick up haml/slim templates if you place them in `app/views/kaminari/`.
385
+
386
+ ### Multiple Templates
387
+
388
+ In case you need different templates for your paginator (for example public and admin), you can pass `--views-prefix directory` like this:
389
+
390
+ ```sh
391
+ % rails g kaminari:views default --views-prefix admin
392
+ ```
393
+
394
+ that will generate partials in `app/views/admin/kaminari/` directory.
395
+
396
+ ### Themes
397
+
398
+ The generator has the ability to fetch several sample template themes from the external repository (https://github.com/amatsuda/kaminari_themes) in addition to the bundled "default" one, which will help you creating a nice looking paginator.
399
+
400
+ ```sh
401
+ % rails g kaminari:views THEME
402
+ ```
403
+
404
+ To see the full list of available themes, take a look at the themes repository, or just hit the generator without specifying `THEME` argument.
405
+
406
+ ```sh
407
+ % rails g kaminari:views
408
+ ```
409
+
410
+ ### Multiple Themes
411
+
412
+ To utilize multiple themes from within a single application, create a directory within the app/views/kaminari/ and move your custom template files into that directory.
413
+
414
+ ```sh
415
+ % rails g kaminari:views default (skip if you have existing kaminari views)
416
+ % cd app/views/kaminari
417
+ % mkdir my_custom_theme
418
+ % cp _*.html.* my_custom_theme/
419
+ ```
420
+
421
+ Next, reference that directory when calling the `paginate` method:
422
+
423
+ ```erb
424
+ <%= paginate @users, theme: 'my_custom_theme' %>
425
+ ```
426
+
427
+ Customize away!
428
+
429
+ Note: if the theme isn't present or none is specified, kaminari will default back to the views included within the gem.
430
+
431
+
432
+ ## Paginating Without Issuing SELECT COUNT Query
433
+
434
+ Generally the paginator needs to know the total number of records to display the links, but sometimes we don't need the total number of records and just need the "previous page" and "next page" links.
435
+ For such use case, Kaminari provides `without_count` mode that creates a paginatable collection without counting the number of all records.
436
+ This may be helpful when you're dealing with a very large dataset because counting on a big table tends to become slow on RDBMS.
437
+
438
+ Just add `.without_count` to your paginated object:
439
+
440
+ ```ruby
441
+ User.page(3).without_count
442
+ ```
443
+
444
+ In your view file, you can only use simple helpers like the following instead of the full-featured `paginate` helper:
445
+
446
+ ```erb
447
+ <%= link_to_prev_page @users, 'Previous Page' %>
448
+ <%= link_to_next_page @users, 'Next Page' %>
449
+ ```
450
+
451
+
452
+ ## Paginating a Generic Array object
453
+
454
+ Kaminari provides an Array wrapper class that adapts a generic Array object to the `paginate` view helper. However, the `paginate` helper doesn't automatically handle your Array object (this is intentional and by design).
455
+ `Kaminari::paginate_array` method converts your Array object into a paginatable Array that accepts `page` method.
456
+
457
+ ```ruby
458
+ @paginatable_array = Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)
459
+ ```
460
+
461
+ You can specify the `total_count` value through options Hash. This would be helpful when handling an Array-ish object that has a different `count` value from actual `count` such as RSolr search result or when you need to generate a custom pagination. For example:
462
+
463
+ ```ruby
464
+ @paginatable_array = Kaminari.paginate_array([], total_count: 145).page(params[:page]).per(10)
465
+ ```
466
+
467
+ or, in the case of using an external API to source the page of data:
468
+ ```ruby
469
+ page_size = 10
470
+ one_page = get_page_of_data params[:page], page_size
471
+ @paginatable_array = Kaminari.paginate_array(one_page.data, total_count: one_page.total_count).page(params[:page]).per(page_size)
472
+ ```
473
+
474
+
475
+ ## Creating Friendly URLs and Caching
476
+
477
+ Because of the `page` parameter and Rails routing, you can easily generate SEO and user-friendly URLs. For any resource you'd like to paginate, just add the following to your `routes.rb`:
478
+
479
+ ```ruby
480
+ resources :my_resources do
481
+ get 'page/:page', action: :index, on: :collection
482
+ end
483
+ ```
484
+
485
+ If you are using Rails 4 or later, you can simplify route definitions by using `concern`:
486
+
487
+ ```ruby
488
+ concern :paginatable do
489
+ get '(page/:page)', action: :index, on: :collection, as: ''
490
+ end
491
+
492
+ resources :my_resources, concerns: :paginatable
493
+ ```
494
+
495
+ This will create URLs like `/my_resources/page/33` instead of `/my_resources?page=33`. This is now a friendly URL, but it also has other added benefits...
496
+
497
+ Because the `page` parameter is now a URL segment, we can leverage on Rails page [caching](http://guides.rubyonrails.org/caching_with_rails.html#page-caching)!
498
+
499
+ NOTE: In this example, I've pointed the route to my `:index` action. You may have defined a custom pagination action in your controller - you should point `action: :your_custom_action` instead.
500
+
501
+
502
+ ## Other Framework/Library Support
503
+
504
+ ### The kaminari gem
505
+
506
+ Technically, the kaminari gem consists of 3 individual components:
507
+
508
+ kaminari-core: the core pagination logic
509
+ kaminari-activerecord: Active Record adapter
510
+ kaminari-actionview: Action View adapter
511
+
512
+ So, bundling `gem 'kaminari'` is equivalent to the following 2 lines (kaminari-core is referenced from the adapters):
513
+
514
+ ```ruby
515
+ gem 'kaminari-activerecord'
516
+ gem 'kaminari-actionview'
517
+ ```
518
+
519
+ ### For Other ORM Users
520
+
521
+ If you want to use other supported ORMs instead of ActiveRecord, for example Mongoid, bundle its adapter instead of kaminari-activerecord.
522
+
523
+ ```ruby
524
+ gem 'kaminari-mongoid'
525
+ gem 'kaminari-actionview'
526
+ ```
527
+
528
+ Kaminari currently provides adapters for the following ORMs:
529
+
530
+ * Active Record: https://github.com/kaminari/kaminari/tree/master/kaminari-activerecord (included in this repo)
531
+ * Mongoid: https://github.com/kaminari/kaminari-mongoid
532
+ * MongoMapper: https://github.com/kaminari/kaminari-mongo_mapper
533
+ * DataMapper: https://github.com/kaminari/kaminari-data_mapper (would not work on kaminari 1.0.x)
534
+
535
+ ### For Other Web Framework Users
536
+
537
+ If you want to use other web frameworks instead of Rails + Action View, for example Sinatra, bundle its adapter instead of kaminari-actionview.
538
+
539
+ ```ruby
540
+ gem 'kaminari-activerecord'
541
+ gem 'kaminari-sinatra'
542
+ ```
543
+
544
+ Kaminari currently provides adapters for the following web frameworks:
545
+
546
+ * Action View: https://github.com/kaminari/kaminari/tree/master/kaminari-actionview (included in this repo)
547
+ * Sinatra: https://github.com/kaminari/kaminari-sinatra
548
+ * Grape: https://github.com/kaminari/kaminari-grape
549
+
550
+
551
+ ## For More Information
552
+
553
+ Check out Kaminari recipes on the GitHub Wiki for more advanced tips and techniques. https://github.com/kaminari/kaminari/wiki/Kaminari-recipes
554
+
555
+
556
+ ## Questions, Feedback
557
+
558
+ Feel free to message me on Github (amatsuda) or Twitter ([@a_matsuda](https://twitter.com/a_matsuda)) ☇☇☇ :)
559
+
560
+
561
+ ## Contributing to Kaminari
562
+
563
+ Fork, fix, then send a pull request.
564
+
565
+ To run the test suite locally against all supported frameworks:
566
+
567
+ ```sh
568
+ % bundle install
569
+ % rake test:all
570
+ ```
571
+
572
+ To target the test suite against one framework:
573
+
574
+ ```sh
575
+ % rake test:active_record_50
576
+ ```
577
+
578
+ You can find a list of supported test tasks by running `rake -T`. You may also find it useful to run a specific test for a specific framework. To do so, you'll have to first make sure you have bundled everything for that configuration, then you can run the specific test:
579
+
580
+ ```sh
581
+ % BUNDLE_GEMFILE='gemfiles/active_record_50.gemfile' bundle install
582
+ % BUNDLE_GEMFILE='gemfiles/active_record_50.gemfile' TEST=kaminari-core/test/requests/navigation_test.rb bundle exec rake test
583
+ ```
584
+
585
+
586
+ ## Copyright
587
+
588
+ Copyright (c) 2011- Akira Matsuda. See MIT-LICENSE for further details.
data/Rakefile CHANGED
@@ -1,42 +1,35 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'bundler'
4
- Bundler::GemHelper.install_tasks
3
+ require "bundler/gem_tasks"
5
4
 
6
- require 'rspec/core'
7
- require 'rspec/core/rake_task'
5
+ require 'rake/testtask'
6
+ require 'kaminari'
8
7
 
9
- RSpec::Core::RakeTask.new(:spec) do |spec|
10
- spec.pattern = FileList['spec/**/*_spec.rb']
8
+ Rake::TestTask.new do |t|
9
+ t.libs << 'test'
10
+ t.pattern = "{test,#{File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'test')}}/**/*_test.rb"
11
+ t.warning = true
12
+ t.verbose = true
11
13
  end
12
14
 
13
- task :default => "spec:all"
15
+ task default: "test:all"
14
16
 
15
- namespace :spec do
17
+ namespace :test do
16
18
  mappers = %w(
17
19
  active_record_edge
20
+ active_record_60
21
+ active_record_52
22
+ active_record_51
23
+ active_record_50
18
24
  active_record_42
19
25
  active_record_41
20
- active_record_40
21
- active_record_32
22
- active_record_31
23
- active_record_30
24
- data_mapper_12
25
- mongoid_50
26
- mongoid_40
27
- mongoid_31
28
- mongoid_30
29
- mongoid_24
30
- mongo_mapper
31
- sinatra_13
32
- sinatra_14
33
26
  )
34
27
 
35
28
  mappers.each do |gemfile|
36
29
  desc "Run Tests against #{gemfile}"
37
30
  task gemfile do
38
31
  sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
39
- sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake -t spec"
32
+ sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake -t test"
40
33
  end
41
34
  end
42
35
 
@@ -44,11 +37,19 @@ namespace :spec do
44
37
  task :all do
45
38
  mappers.each do |gemfile|
46
39
  sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
47
- sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake spec"
40
+ sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake test"
48
41
  end
49
42
  end
50
43
  end
51
44
 
45
+ task :install_tasks_for_sub_gems do
46
+ Bundler::GemHelper.install_tasks dir: File.join(__dir__, 'kaminari-core'), name: 'kaminari-core'
47
+ Bundler::GemHelper.install_tasks dir: File.join(__dir__, 'kaminari-actionview'), name: 'kaminari-actionview'
48
+ Bundler::GemHelper.install_tasks dir: File.join(__dir__, 'kaminari-activerecord'), name: 'kaminari-activerecord'
49
+ end
50
+
51
+ Rake::Task[:build].enhance [:install_tasks_for_sub_gems]
52
+
52
53
  begin
53
54
  require 'rdoc/task'
54
55
 
@@ -57,7 +58,6 @@ begin
57
58
 
58
59
  rdoc.rdoc_dir = 'rdoc'
59
60
  rdoc.title = "kaminari #{Kaminari::VERSION}"
60
- rdoc.rdoc_files.include('README*')
61
61
  rdoc.rdoc_files.include('lib/**/*.rb')
62
62
  end
63
63
  rescue LoadError