adequate_serializer 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1fec099288058f8a38ccdd3fdccfe9ce2c2d67c1
4
- data.tar.gz: d4177cde980e80c2b7adfe7b8c7a0c5f52b538cb
3
+ metadata.gz: 75d51f7e119da79d4844eaf7588c35f5822f1d3a
4
+ data.tar.gz: 5f287e45995db1e14a3cba9224c30696037005a5
5
5
  SHA512:
6
- metadata.gz: ef07a512dc0006b231d6d8f512b2c1423ea7d0eb9ff1a948aa3ba25bf5cc276e6dfc91018e47e9ef07f572541b64a36b6eb3db87c5f36118c4cdc92fac8a8c22
7
- data.tar.gz: 79eca37e71344bccda080dac51425395d612a576c9e5cdc11943c3be836cbcf2684b697a7dca8c8c7239a21c6fb3ec7a4c36b49e8fa4241a79fdcaff87193829
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
- If you want to include associations, just specify them in the controller.
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
- In this case the PostSerializer will be used to nest the user's posts under the
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
- You can also include multiple associations by using an array of association
110
- keys.
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(*attrs)
17
- attrs.each do |attr|
18
- @_attributes << attr
18
+ def self.attributes(*attributes)
19
+ attributes.each do |attribute|
20
+ @_attributes << attribute
19
21
 
20
- define_method_for(attr)
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
- return {} if includes.nil?
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 serialize_multiple_associations(association_keys)
78
- association_keys.each_with_object({}) do |key, hash|
79
- hash[key] = serialize_association(key)
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
- serialize(associated_objects, root: false)
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)
@@ -1,3 +1,3 @@
1
1
  module AdequateSerializer
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
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.1.2
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-24 00:00:00.000000000 Z
11
+ date: 2015-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport