Commit 9a8053c
committed
Support
Follow-up to [#420][]
Background
---
The introduction of `ActiveResource::Base.coder` and the
`ActiveResource::Coder` class added support for encoding and decoding
instances of `Base`.
However, collection-focused methods like `Base.where`, `Base.all`, and
`Base.find(:all)` return `ActiveResource::Collection` instances.
Problem
---
While some collection instances are equivalent to `Array` instances,
they are capable of being parsed into `Hash` values that include
additional metadata (for example, pagination URLs, total counts, etc.).
If applications were to dump results, there is a potential loss of that
metadata.
Proposal
---
First, this commit modifies the `ActiveResource::Coder` class to accept
a boolean `:collection` keyword to treat values as
`ActiveResource::Collection` instances.
Next, extend the `ActiveResource::Collection` class to retain the
originally parsed values as a new `#original_parsed` attribute. It also
defines the `#encode` method to rely on the resource class format for
encoding.
Additionally, extend the `ActiveResource::Serialization` module to also
define a `.collection_coder` class attribute to serve as a convenience
method for consumer to pass to `.serialize …, coder: …`:
```ruby
class Person < ActiveResource::Base
# …
end
class Team < ApplicationRecord
serialize :people, coder: Person.collection_coder
end
```
Like the instance-level coders, collection-level coders constructed with
`collection: true` also accept an encoder proc to transform the value
prior to dumping (for JSON/JSONB columns, for example):
```ruby
class Person < ActiveResource::Base
# …
end
class Team < ApplicationRecord
# pass a to_proc-ready method name
serialize :people, coder: ActiveResource::Coder.new(Person, :original_parsed, collection: true)
# pass a block
serialize :people, coder: ActiveResource::Coder.new(Person, collection: true) do |collection|
collection.original_parsed
end
end
```
[#420]: #420.serialize …, coder: … for Collections1 parent bd154d2 commit 9a8053c
File tree
8 files changed
+344
-22
lines changed- lib/active_resource
- test
- cases
- base
- fixtures
8 files changed
+344
-22
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1162 | 1162 | | |
1163 | 1163 | | |
1164 | 1164 | | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
1165 | 1173 | | |
1166 | 1174 | | |
1167 | 1175 | | |
| |||
1212 | 1220 | | |
1213 | 1221 | | |
1214 | 1222 | | |
1215 | | - | |
1216 | | - | |
1217 | | - | |
1218 | | - | |
1219 | | - | |
1220 | | - | |
1221 | | - | |
1222 | 1223 | | |
1223 | 1224 | | |
1224 | 1225 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
50 | 75 | | |
51 | | - | |
| 76 | + | |
52 | 77 | | |
53 | 78 | | |
54 | 79 | | |
55 | 80 | | |
56 | 81 | | |
57 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
58 | 88 | | |
59 | 89 | | |
| 90 | + | |
60 | 91 | | |
61 | 92 | | |
62 | 93 | | |
63 | 94 | | |
64 | 95 | | |
65 | 96 | | |
66 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
67 | 100 | | |
68 | 101 | | |
69 | 102 | | |
| |||
73 | 106 | | |
74 | 107 | | |
75 | 108 | | |
76 | | - | |
77 | | - | |
78 | 109 | | |
79 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
80 | 118 | | |
81 | 119 | | |
82 | 120 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
93 | 97 | | |
94 | 98 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
69 | 82 | | |
70 | 83 | | |
71 | 84 | | |
72 | 85 | | |
73 | 86 | | |
| 87 | + | |
74 | 88 | | |
75 | 89 | | |
76 | 90 | | |
| |||
79 | 93 | | |
80 | 94 | | |
81 | 95 | | |
| 96 | + | |
82 | 97 | | |
83 | 98 | | |
84 | 99 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
0 commit comments