adequate_serializer 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +21 -5
- data/lib/adequate_serializer/base.rb +33 -16
- data/lib/adequate_serializer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75d51f7e119da79d4844eaf7588c35f5822f1d3a
|
4
|
+
data.tar.gz: 5f287e45995db1e14a3cba9224c30696037005a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64b355e2ca1f0f92e0e7db1d321e8a98fbd15cd010c01616b05ccb5ef8d9da4341945710f95b6907b76c4eaf8805247458d15e5e41982f7a4fcef5803a8b031e
|
7
|
+
data.tar.gz: a9c19e14e8ba3672d61b6e1311bc3ffe7ec1290d9e675d916b22a9acda90c9a258c5451879cc4057792fc4d2e541c9219b68df6beedd13f7eea6757fa4a16eab
|
data/README.md
CHANGED
@@ -97,17 +97,32 @@ Within a serializer's methods, you can access the object being serialized as
|
|
97
97
|
|
98
98
|
### Associations
|
99
99
|
|
100
|
-
|
100
|
+
Associations that always have to be included can be specified in the
|
101
|
+
serializer:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class UserSerializer::Base
|
105
|
+
attributes :id, :created_at, :updated_at
|
106
|
+
associations :posts, :comments
|
107
|
+
end
|
108
|
+
```
|
109
|
+
|
110
|
+
If the association only needs to be included for certain endpoints, it can be
|
111
|
+
specified in the controller:
|
101
112
|
|
102
113
|
```ruby
|
103
114
|
serialize(@user, includes: :posts)
|
104
115
|
```
|
105
116
|
|
106
|
-
|
107
|
-
`posts` key in the user JSON object.
|
117
|
+
The PostSerializer will be used to nest the user's posts under the
|
118
|
+
`posts` key in the user JSON object in both cases.
|
119
|
+
|
120
|
+
You can also include multiple associations or even nest them by using the
|
121
|
+
`includes` syntax.
|
108
122
|
|
109
|
-
|
110
|
-
|
123
|
+
```ruby
|
124
|
+
serialize(@user, includes: [:posts, comments: :user])
|
125
|
+
```
|
111
126
|
|
112
127
|
#### Overriding association methods
|
113
128
|
|
@@ -116,6 +131,7 @@ If you want to override any association, you can use:
|
|
116
131
|
```ruby
|
117
132
|
class UserSerializer::Base
|
118
133
|
attributes :id, :created_at, :updated_at
|
134
|
+
associations :posts
|
119
135
|
|
120
136
|
def posts
|
121
137
|
object.posts.published
|
@@ -6,18 +6,26 @@ module AdequateSerializer
|
|
6
6
|
include Helper
|
7
7
|
|
8
8
|
class << self
|
9
|
+
attr_accessor :_associations
|
9
10
|
attr_accessor :_attributes
|
10
11
|
end
|
11
12
|
|
12
13
|
def self.inherited(base)
|
14
|
+
base._associations = (_associations || []).dup
|
13
15
|
base._attributes = (_attributes || []).dup
|
14
16
|
end
|
15
17
|
|
16
|
-
def self.attributes(*
|
17
|
-
|
18
|
-
@_attributes <<
|
18
|
+
def self.attributes(*attributes)
|
19
|
+
attributes.each do |attribute|
|
20
|
+
@_attributes << attribute
|
19
21
|
|
20
|
-
define_method_for(
|
22
|
+
define_method_for(attribute)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.associations(*associations)
|
27
|
+
associations.each do |association|
|
28
|
+
@_associations << association
|
21
29
|
end
|
22
30
|
end
|
23
31
|
|
@@ -48,13 +56,7 @@ module AdequateSerializer
|
|
48
56
|
end
|
49
57
|
|
50
58
|
def associations
|
51
|
-
|
52
|
-
|
53
|
-
if includes.respond_to?(:each)
|
54
|
-
serialize_multiple_associations(includes)
|
55
|
-
else
|
56
|
-
{ includes => serialize_association(includes) }
|
57
|
-
end
|
59
|
+
serialize_multiple_associations(normalized_associations)
|
58
60
|
end
|
59
61
|
|
60
62
|
private
|
@@ -74,15 +76,30 @@ module AdequateSerializer
|
|
74
76
|
name.to_sym
|
75
77
|
end
|
76
78
|
|
77
|
-
def
|
78
|
-
|
79
|
-
|
79
|
+
def normalized_associations
|
80
|
+
all_associations = Array(includes) | self.class._associations
|
81
|
+
|
82
|
+
all_associations.each_with_object({}) do |(key, value), hash|
|
83
|
+
if key.is_a?(Hash)
|
84
|
+
hash.merge!(key)
|
85
|
+
else
|
86
|
+
hash[key] ||= value
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def serialize_multiple_associations(associations)
|
92
|
+
associations.each_with_object({}) do |(key, includes), hash|
|
93
|
+
hash[key] = serialize_association(key, includes)
|
80
94
|
end
|
81
95
|
end
|
82
96
|
|
83
|
-
def serialize_association(association_key)
|
97
|
+
def serialize_association(association_key, includes)
|
84
98
|
associated_objects = associated_objects(association_key)
|
85
|
-
|
99
|
+
|
100
|
+
unless associated_objects.nil?
|
101
|
+
serialize(associated_objects, root: false, includes: includes)
|
102
|
+
end
|
86
103
|
end
|
87
104
|
|
88
105
|
def associated_objects(association_key)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adequate_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bastian Bartmann
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|