kaminari 1.1.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.

@@ -0,0 +1,16 @@
1
+ Kaminari is a volunteer effort. We encourage you to pitch in.
2
+
3
+ __*We only accept bug reports and pull requests in GitHub*__. Join the team!
4
+
5
+ * If you have a support question about how to use kaminari, please [ask on StackOverflow](http://stackoverflow.com/search?tab=newest&q=kaminari).
6
+ * Bug reports should include the following:
7
+ - Minimal example of code to reproduce the bug
8
+ - Stacktrace
9
+ - What you expected to see, and what you actually saw.
10
+
11
+ * Feature requests should be accompanied by a patch, that includes tests.
12
+ * We won't accept any feature requests that come without a patch.
13
+
14
+ Thanks! :heart: :heart: :heart:
15
+
16
+ Kaminari Team
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ # Specify your gem's dependencies in kaminari.gemspec
5
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Akira Matsuda
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,558 @@
1
+ # Kaminari [![Build Status](https://travis-ci.org/kaminari/kaminari.svg)](http://travis-ci.org/kaminari/kaminari) [![Code Climate](https://codeclimate.com/github/kaminari/kaminari/badges/gpa.svg)](https://codeclimate.com/github/kaminari/kaminari) [![Inch CI](http://inch-ci.org/github/kaminari/kaminari.svg)](http://inch-ci.org/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.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5
34
+
35
+ * Rails 4.1, 4.2, 5.0, 5.1, 5.2
36
+
37
+ * Sinatra 1.4
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
+ You can get page numbers or page conditions by using below methods.
78
+ ```ruby
79
+ User.count #=> 1000
80
+ User.page(1).limit_value #=> 20
81
+ User.page(1).total_pages #=> 50
82
+ User.page(1).current_page #=> 1
83
+ User.page(1).next_page #=> 2
84
+ User.page(2).prev_page #=> 1
85
+ User.page(1).first_page? #=> true
86
+ User.page(50).last_page? #=> true
87
+ User.page(100).out_of_range? #=> true
88
+ ```
89
+
90
+ ### The `per` Scope
91
+
92
+ To show a lot more users per each page (change the `per_page` value)
93
+
94
+ ```ruby
95
+ User.page(7).per(50)
96
+ ```
97
+
98
+ Note that the `per` scope is not directly defined on the models but is just a method defined on the page scope.
99
+ This is absolutely reasonable because you will never actually use `per_page` without specifying the `page` number.
100
+
101
+ Keep in mind that `per` internally utilizes `limit` and so it will override any `limit` that was set previously.
102
+ And if you want to get the size for all request records you can use `total_count` method:
103
+
104
+ ```ruby
105
+ User.count #=> 1000
106
+ a = User.limit(5); a.count #=> 5
107
+ a.page(1).per(20).size #=> 20
108
+ a.page(1).per(20).total_count #=> 1000
109
+ ```
110
+
111
+ ### The `padding` Scope
112
+
113
+ Occasionally you need to pad a number of records that is not a multiple of the page size.
114
+
115
+ ```ruby
116
+ User.page(7).per(50).padding(3)
117
+ ```
118
+
119
+ Note that the `padding` scope also is not directly defined on the models.
120
+
121
+
122
+ ## Configuring Kaminari
123
+
124
+ ### General Configuration Options
125
+
126
+ You can configure the following default values by overriding these values using `Kaminari.configure` method.
127
+
128
+ default_per_page # 25 by default
129
+ max_per_page # nil by default
130
+ max_pages # nil by default
131
+ window # 4 by default
132
+ outer_window # 0 by default
133
+ left # 0 by default
134
+ right # 0 by default
135
+ page_method_name # :page by default
136
+ param_name # :page by default
137
+ params_on_first_page # false by default
138
+
139
+ There's a handy generator that generates the default configuration file into config/initializers directory.
140
+ Run the following generator command, then edit the generated file.
141
+
142
+ ```sh
143
+ % rails g kaminari:config
144
+ ```
145
+
146
+ ### Changing `page_method_name`
147
+
148
+ 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.
149
+
150
+
151
+ ### Configuring Default per_page Value for Each Model by `paginates_per`
152
+
153
+ You can specify default `per_page` value per each model using the following declarative DSL.
154
+
155
+ ```ruby
156
+ class User < ActiveRecord::Base
157
+ paginates_per 50
158
+ end
159
+ ```
160
+
161
+ ### Configuring Max per_page Value for Each Model by `max_paginates_per`
162
+
163
+ You can specify max `per_page` value per each model using the following declarative DSL.
164
+ If the variable that specified via `per` scope is more than this variable, `max_paginates_per` is used instead of it.
165
+ Default value is nil, which means you are not imposing any max `per_page` value.
166
+
167
+ ```ruby
168
+ class User < ActiveRecord::Base
169
+ max_paginates_per 100
170
+ end
171
+ ```
172
+
173
+
174
+ ## Controllers
175
+
176
+ ### The Page Parameter Is in `params[:page]`
177
+
178
+ Typically, your controller code will look like this:
179
+
180
+ ```ruby
181
+ @users = User.order(:name).page params[:page]
182
+ ```
183
+
184
+
185
+ ## Views
186
+
187
+ ### The Same Old Helper Method
188
+
189
+ Just call the `paginate` helper:
190
+
191
+ ```erb
192
+ <%= paginate @users %>
193
+ ```
194
+
195
+ This will render several `?page=N` pagination links surrounded by an HTML5 `<nav>` tag.
196
+
197
+
198
+ ## Helpers
199
+
200
+ ### The `paginate` Helper Method
201
+
202
+ ```erb
203
+ <%= paginate @users %>
204
+ ```
205
+
206
+ This would output several pagination links such as `« First ‹ Prev ... 2 3 4 5 6 7 8 9 10 ... Next › Last »`
207
+
208
+ ### Specifying the "inner window" Size (4 by default)
209
+
210
+ ```erb
211
+ <%= paginate @users, window: 2 %>
212
+ ```
213
+
214
+ This would output something like `... 5 6 7 8 9 ...` when 7 is the current
215
+ page.
216
+
217
+ ### Specifying the "outer window" Size (0 by default)
218
+
219
+ ```erb
220
+ <%= paginate @users, outer_window: 3 %>
221
+ ```
222
+
223
+ This would output something like `1 2 3 ...(snip)... 18 19 20` while having 20 pages in total.
224
+
225
+ ### Outer Window Can Be Separately Specified by left, right (0 by default)
226
+
227
+ ```erb
228
+ <%= paginate @users, left: 1, right: 3 %>
229
+ ```
230
+
231
+ This would output something like `1 ...(snip)... 18 19 20` while having 20 pages in total.
232
+
233
+ ### Changing the Parameter Name (`:param_name`) for the Links
234
+
235
+ ```erb
236
+ <%= paginate @users, param_name: :pagina %>
237
+ ```
238
+
239
+ This would modify the query parameter name on each links.
240
+
241
+ ### Extra Parameters (`:params`) for the Links
242
+
243
+ ```erb
244
+ <%= paginate @users, params: {controller: 'foo', action: 'bar'} %>
245
+ ```
246
+
247
+ This would modify each link's `url_option`. :`controller` and :`action` might be the keys in common.
248
+
249
+ ### Ajax Links (crazy simple, but works perfectly!)
250
+
251
+ ```erb
252
+ <%= paginate @users, remote: true %>
253
+ ```
254
+
255
+ This would add `data-remote="true"` to all the links inside.
256
+
257
+ ### Specifying an Alternative Views Directory (default is kaminari/)
258
+
259
+ ```erb
260
+ <%= paginate @users, views_prefix: 'templates' %>
261
+ ```
262
+
263
+ This would search for partials in `app/views/templates/kaminari`.
264
+ 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).
265
+
266
+ ### The `link_to_next_page` and `link_to_previous_page` (aliased to `link_to_prev_page`) Helper Methods
267
+
268
+ ```erb
269
+ <%= link_to_next_page @items, 'Next Page' %>
270
+ ```
271
+
272
+ This simply renders a link to the next page. This would be helpful for creating a Twitter-like pagination feature.
273
+
274
+ ### The `page_entries_info` Helper Method
275
+
276
+ ```erb
277
+ <%= page_entries_info @posts %>
278
+ ```
279
+
280
+ This renders a helpful message with numbers of displayed vs. total entries.
281
+
282
+ By default, the message will use the humanized class name of objects in collection: for instance, "project types" for ProjectType models.
283
+ The namespace will be cut out and only the last name will be used. Override this with the `:entry_name` parameter:
284
+
285
+ ```erb
286
+ <%= page_entries_info @posts, entry_name: 'item' %>
287
+ #=> Displaying items 6 - 10 of 26 in total
288
+ ```
289
+
290
+ ### The `rel_next_prev_link_tags` Helper Method
291
+
292
+ ```erb
293
+ <%= rel_next_prev_link_tags @users %>
294
+ ```
295
+
296
+ This renders the rel next and prev link tags for the head.
297
+
298
+ ### The `path_to_next_page` Helper Method
299
+
300
+ ```erb
301
+ <%= path_to_next_page @users %>
302
+ ```
303
+
304
+ This returns the server relative path to the next page.
305
+
306
+ ### The `path_to_prev_page` Helper Method
307
+
308
+ ```erb
309
+ <%= path_to_prev_page @users %>
310
+ ```
311
+
312
+ This returns the server relative path to the previous page.
313
+
314
+
315
+ ## I18n and Labels
316
+
317
+ The default labels for 'first', 'last', 'previous', '...' and 'next' are stored in the I18n yaml inside the engine, and rendered through I18n API.
318
+ 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.
319
+
320
+ ```yaml
321
+ en:
322
+ views:
323
+ pagination:
324
+ first: "&laquo; First"
325
+ last: "Last &raquo;"
326
+ previous: "&lsaquo; Prev"
327
+ next: "Next &rsaquo;"
328
+ truncate: "&hellip;"
329
+ helpers:
330
+ page_entries_info:
331
+ one_page:
332
+ display_entries:
333
+ zero: "No %{entry_name} found"
334
+ one: "Displaying <b>1</b> %{entry_name}"
335
+ other: "Displaying <b>all %{count}</b> %{entry_name}"
336
+ more_pages:
337
+ display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
338
+ ```
339
+
340
+ If you use non-English localization see [i18n rules](https://github.com/svenfuchs/i18n/blob/master/test/test_data/locales/plurals.rb) for changing
341
+ `one_page:display_entries` block.
342
+
343
+
344
+ ## Customizing the Pagination Helper
345
+
346
+ Kaminari includes a handy template generator.
347
+
348
+ ### To Edit Your Paginator
349
+
350
+ Run the generator first,
351
+
352
+ ```sh
353
+ % rails g kaminari:views default
354
+ ```
355
+
356
+ then edit the partials in your app's `app/views/kaminari/` directory.
357
+
358
+ ### For Haml/Slim Users
359
+
360
+ 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.
361
+ The kaminari gem will automatically pick up haml/slim templates if you place them in `app/views/kaminari/`.
362
+
363
+ ### Multiple Templates
364
+
365
+ In case you need different templates for your paginator (for example public and admin), you can pass `--views-prefix directory` like this:
366
+
367
+ ```sh
368
+ % rails g kaminari:views default --views-prefix admin
369
+ ```
370
+
371
+ that will generate partials in `app/views/admin/kaminari/` directory.
372
+
373
+ ### Themes
374
+
375
+ 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.
376
+
377
+ ```sh
378
+ % rails g kaminari:views THEME
379
+ ```
380
+
381
+ To see the full list of available themes, take a look at the themes repository, or just hit the generator without specifying `THEME` argument.
382
+
383
+ ```sh
384
+ % rails g kaminari:views
385
+ ```
386
+
387
+ ### Multiple Themes
388
+
389
+ 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.
390
+
391
+ ```sh
392
+ % rails g kaminari:views default (skip if you have existing kaminari views)
393
+ % cd app/views/kaminari
394
+ % mkdir my_custom_theme
395
+ % cp _*.html.* my_custom_theme/
396
+ ```
397
+
398
+ Next, reference that directory when calling the `paginate` method:
399
+
400
+ ```erb
401
+ <%= paginate @users, theme: 'my_custom_theme' %>
402
+ ```
403
+
404
+ Customize away!
405
+
406
+ Note: if the theme isn't present or none is specified, kaminari will default back to the views included within the gem.
407
+
408
+
409
+ ## Paginating Without Issuing SELECT COUNT Query
410
+
411
+ 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.
412
+ For such use case, Kaminari provides `without_count` mode that creates a paginatable collection without counting the number of all records.
413
+ 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.
414
+
415
+ Just add `.without_count` to your paginated object:
416
+
417
+ ```ruby
418
+ User.page(3).without_count
419
+ ```
420
+
421
+ In your view file, you can only use simple helpers like the following instead of the full-featured `paginate` helper:
422
+
423
+ ```erb
424
+ <%= link_to_prev_page @users, 'Previous Page' %>
425
+ <%= link_to_next_page @users, 'Next Page' %>
426
+ ```
427
+
428
+
429
+ ## Paginating a Generic Array object
430
+
431
+ 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).
432
+ `Kaminari::paginate_array` method converts your Array object into a paginatable Array that accepts `page` method.
433
+
434
+ ```ruby
435
+ @paginatable_array = Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)
436
+ ```
437
+
438
+ 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:
439
+
440
+ ```ruby
441
+ @paginatable_array = Kaminari.paginate_array([], total_count: 145).page(params[:page]).per(10)
442
+ ```
443
+
444
+
445
+ ## Creating Friendly URLs and Caching
446
+
447
+ 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`:
448
+
449
+ ```ruby
450
+ resources :my_resources do
451
+ get 'page/:page', action: :index, on: :collection
452
+ end
453
+ ```
454
+
455
+ If you are using Rails 4 or later, you can simplify route definitions by using `concern`:
456
+
457
+ ```ruby
458
+ concern :paginatable do
459
+ get '(page/:page)', action: :index, on: :collection, as: ''
460
+ end
461
+
462
+ resources :my_resources, concerns: :paginatable
463
+ ```
464
+
465
+ 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...
466
+
467
+ 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)!
468
+
469
+ 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.
470
+
471
+
472
+ ## Other Framework/Library Support
473
+
474
+ ### The kaminari gem
475
+
476
+ Technically, the kaminari gem consists of 3 individual components:
477
+
478
+ kaminari-core: the core pagination logic
479
+ kaminari-activerecord: Active Record adapter
480
+ kaminari-actionview: Action View adapter
481
+
482
+ So, bundling `gem 'kaminari'` is equivalent to the following 2 lines (kaminari-core is referenced from the adapters):
483
+
484
+ ```ruby
485
+ gem 'kaminari-activerecord'
486
+ gem 'kaminari-actionview'
487
+ ```
488
+
489
+ ### For Other ORM Users
490
+
491
+ If you want to use other supported ORMs instead of ActiveRecord, for example Mongoid, bundle its adapter instead of kaminari-activerecord.
492
+
493
+ ```ruby
494
+ gem 'kaminari-mongoid'
495
+ gem 'kaminari-actionview'
496
+ ```
497
+
498
+ Kaminari currently provides adapters for the following ORMs:
499
+
500
+ * Active Record: https://github.com/kaminari/kaminari/tree/master/kaminari-activerecord (included in this repo)
501
+ * Mongoid: https://github.com/kaminari/kaminari-mongoid
502
+ * MongoMapper: https://github.com/kaminari/kaminari-mongo_mapper
503
+ * DataMapper: https://github.com/kaminari/kaminari-data_mapper (would not work on kaminari 1.0.x)
504
+
505
+ ### For Other Web Framework Users
506
+
507
+ If you want to use other web frameworks instead of Rails + Action View, for example Sinatra, bundle its adapter instead of kaminari-actionview.
508
+
509
+ ```ruby
510
+ gem 'kaminari-activerecord'
511
+ gem 'kaminari-sinatra'
512
+ ```
513
+
514
+ Kaminari currently provides adapters for the following web frameworks:
515
+
516
+ * Action View: https://github.com/kaminari/kaminari/tree/master/kaminari-actionview (included in this repo)
517
+ * Sinatra: https://github.com/kaminari/kaminari-sinatra
518
+ * Grape: https://github.com/kaminari/kaminari-grape
519
+
520
+
521
+ ## For More Information
522
+
523
+ Check out Kaminari recipes on the GitHub Wiki for more advanced tips and techniques. https://github.com/kaminari/kaminari/wiki/Kaminari-recipes
524
+
525
+
526
+ ## Questions, Feedback
527
+
528
+ Feel free to message me on Github (amatsuda) or Twitter ([@a_matsuda](https://twitter.com/a_matsuda)) ☇☇☇ :)
529
+
530
+
531
+ ## Contributing to Kaminari
532
+
533
+ Fork, fix, then send a pull request.
534
+
535
+ To run the test suite locally against all supported frameworks:
536
+
537
+ ```sh
538
+ % bundle install
539
+ % rake test:all
540
+ ```
541
+
542
+ To target the test suite against one framework:
543
+
544
+ ```sh
545
+ % rake test:active_record_50
546
+ ```
547
+
548
+ 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:
549
+
550
+ ```sh
551
+ % BUNDLE_GEMFILE='gemfiles/active_record_50.gemfile' bundle install
552
+ % BUNDLE_GEMFILE='gemfiles/active_record_50.gemfile' TEST=kaminari-core/test/requests/navigation_test.rb bundle exec rake test
553
+ ```
554
+
555
+
556
+ ## Copyright
557
+
558
+ Copyright (c) 2011- Akira Matsuda. See MIT-LICENSE for further details.