Commit 64a7f52
committed
Support
Follow-up to [rails#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
```
[rails#420]: rails#420.serialize …, coder: … for Collections1 parent d488fe1 commit 64a7f52
File tree
9 files changed
+345
-16
lines changed- lib/active_resource
- test
- cases
- base
- fixtures
9 files changed
+345
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1163 | 1163 | | |
1164 | 1164 | | |
1165 | 1165 | | |
| 1166 | + | |
1166 | 1167 | | |
1167 | 1168 | | |
1168 | 1169 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
| 40 | + | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| |||
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 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
54 | 88 | | |
55 | 89 | | |
| 90 | + | |
56 | 91 | | |
57 | 92 | | |
58 | 93 | | |
59 | 94 | | |
60 | 95 | | |
61 | 96 | | |
62 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
63 | 100 | | |
64 | 101 | | |
65 | 102 | | |
| |||
69 | 106 | | |
70 | 107 | | |
71 | 108 | | |
72 | | - | |
73 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
74 | 117 | | |
75 | 118 | | |
76 | 119 | | |
| 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 | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
65 | 78 | | |
66 | 79 | | |
67 | 80 | | |
68 | 81 | | |
69 | 82 | | |
| 83 | + | |
70 | 84 | | |
71 | 85 | | |
72 | 86 | | |
| |||
75 | 89 | | |
76 | 90 | | |
77 | 91 | | |
| 92 | + | |
78 | 93 | | |
79 | 94 | | |
80 | 95 | | |
| |||
| 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