Commit 5f52cdd
Allow decorating objects after pagination (#91)
* Rubocop fix: Use filter_map instead of map { ... }.compact
Currently, builds are failing because of Rubocop complaining about these
three lines.
* Fix specs with Ransack 4.0
Ransack 4 requires explicitly allowlisting ransackable attributes and
associations for each model. This causes the specs to fail with the most
recent Ransack version. This fixes those spec failures.
* Allow decorating objects after pagination
This simplification allows decorating objects after they are paginated,
without losing the correct total object count.
I'm using an instance variable on the including controller here, because
the decorating the paginated collection will have us lose the instance
variable we set on it.
Here's the case where this happens: We have a complex ActiveRecord
collection that we run through Ransack and Kaminari, but before
rendering we want to convert each object in it using a
`SimpleDelegator`.
Here's a simplified version of the controller action we're looking at:
```
class UserDecorator < SimpleDelegator
def fantastic_for_rendering
"Whoah"
end
end
def index
allowed_fields = [
:first_name, :last_name, :created_at,
:notes_created_at, :notes_quantity
]
options = { sort_with_expressions: true }
jsonapi_filter(User.all, allowed_fields, options) do |filtered|
result = filtered.result
jsonapi_paginate(result) do |paginated|
paginated = paginated.map { |user| UserDecorator.new() }
render jsonapi: paginated
end
end
end
```
---------
Co-authored-by: Stas <stas@users.noreply.github.com>1 parent 52b4128 commit 5f52cdd
3 files changed
Lines changed: 27 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
19 | | - | |
20 | 22 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
| |||
64 | 63 | | |
65 | 64 | | |
66 | 65 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
| 66 | + | |
74 | 67 | | |
75 | 68 | | |
76 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| 122 | + | |
122 | 123 | | |
123 | 124 | | |
124 | 125 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
60 | | - | |
| 61 | + | |
| 62 | + | |
61 | 63 | | |
62 | 64 | | |
63 | 65 | | |
| |||
80 | 82 | | |
81 | 83 | | |
82 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
83 | 104 | | |
84 | 105 | | |
85 | 106 | | |
| |||
0 commit comments