Commit 81ec702
committed
Change interface for Rails pagination
This fixes #7 but is also related to a more dire issue. `after_filter`
was misunderstood to provide access to the response before the body had
been written. In actuality, the response has already been fully
constructed by the time `after_filter` is called, which means that the
collection has already been queried and written as json (or XML). In
order to maintain a nice interface without using after_filter, the
method signature of `paginate` has changed to act like `render`:
```rails
class API::v1::PostsController < ApplicationController
def index
# Uncomment this line to change the default per_page from 10 to 25:
# params[:per_page] ||= 25
posts = Post.all
paginate json: posts
end
end
```
At the end, `paginate` simply passes its arguments on to `render` so all
of `render's` options are supported. The collection is assumed to live
either in the argument hash's `:json` key or an `:xml` key.
Signed-off-by: David Celis <me@davidcel.is>1 parent aa7b000 commit 81ec702
File tree
3 files changed
+30
-21
lines changed- lib/rails
- spec/support
3 files changed
+30
-21
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | 31 | | |
38 | 32 | | |
39 | | - | |
| 33 | + | |
40 | 34 | | |
41 | | - | |
| 35 | + | |
42 | 36 | | |
43 | 37 | | |
44 | 38 | | |
45 | 39 | | |
46 | | - | |
47 | | - | |
| 40 | + | |
48 | 41 | | |
49 | | - | |
50 | | - | |
| 42 | + | |
| 43 | + | |
51 | 44 | | |
52 | | - | |
| 45 | + | |
53 | 46 | | |
54 | 47 | | |
55 | 48 | | |
56 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
57 | 59 | | |
58 | 60 | | |
59 | | - | |
| 61 | + | |
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
| 5 | + | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
23 | 28 | | |
24 | 29 | | |
25 | 30 | | |
| 31 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
62 | 63 | | |
63 | 64 | | |
0 commit comments