active_model_serializers 0.9.1 → 0.9.2
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/CHANGELOG.md +0 -6
- data/README.md +17 -109
- data/lib/action_controller/serialization.rb +8 -27
- data/lib/action_controller/serialization_test_case.rb +4 -4
- data/lib/active_model/array_serializer.rb +5 -12
- data/lib/active_model/serializable.rb +2 -24
- data/lib/active_model/serializer.rb +36 -70
- data/lib/active_model/serializer/{association.rb → associations.rb} +52 -8
- data/lib/active_model/serializer/railtie.rb +0 -8
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +1 -1
- data/test/fixtures/poro.rb +1 -110
- data/test/integration/action_controller/serialization_test.rb +0 -16
- data/test/integration/action_controller/serialization_test_case_test.rb +0 -10
- data/test/integration/active_record/active_record_test.rb +2 -2
- data/test/test_app.rb +0 -3
- data/test/unit/active_model/array_serializer/serialization_test.rb +1 -1
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -15
- data/test/unit/active_model/serializer/attributes_test.rb +0 -16
- data/test/unit/active_model/serializer/config_test.rb +0 -3
- data/test/unit/active_model/serializer/has_many_test.rb +16 -51
- data/test/unit/active_model/serializer/has_one_test.rb +0 -32
- data/test/unit/active_model/serializer/options_test.rb +0 -19
- metadata +3 -18
- data/lib/active_model/serializable/utils.rb +0 -16
- data/lib/active_model/serializer/association/has_many.rb +0 -39
- data/lib/active_model/serializer/association/has_one.rb +0 -25
- data/test/fixtures/template.html.erb +0 -1
- data/test/integration/action_controller/namespaced_serialization_test.rb +0 -96
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
- data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
- data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
- data/test/unit/active_model/serializer/url_helpers_test.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9a74f3f7f7116d91ae865c6f1ebff4a803181fc
|
4
|
+
data.tar.gz: 660cbc5ba7cd25125bdd8e50875b03c726b3c75a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef6c3b8ae2e948ffc77ffbf16823e6a13cb9d94410423be2a2469cbb75fd936b6b116d51bac5b4ce509603ec2021b3d494c9b675ce4a002ad596c8b55d2251c7
|
7
|
+
data.tar.gz: a54bd156271f4434d949dbf225775d9e9883a3362acda92227630d9045e639d321f188a3de23a5fad6390855f42929256d76808927e8a30051354ad7840f94b4
|
data/CHANGELOG.md
CHANGED
@@ -20,12 +20,6 @@
|
|
20
20
|
|
21
21
|
* Require rails >= 3.2.
|
22
22
|
|
23
|
-
* Serializers for associations are being looked up in a parent serializer's namespace first. Same with controllers' namespaces.
|
24
|
-
|
25
|
-
* Added a "prefix" option in case you want to use a different version of serializer.
|
26
|
-
|
27
|
-
* Serializers default namespace can be set in `default_serializer_options` and inherited by associations.
|
28
|
-
|
29
23
|
# VERSION 0.8.1
|
30
24
|
|
31
25
|
* Fix bug whereby a serializer using 'options' would blow up.
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
[](https://travis-ci.org/rails-api/active_model_serializers)
|
2
|
-
[](https://codeclimate.com/github/rails-api/active_model_serializers)
|
1
|
+
[](https://travis-ci.org/rails-api/active_model_serializers)
|
2
|
+
[](https://codeclimate.com/github/rails-api/active_model_serializers)
|
3
3
|
|
4
4
|
# ActiveModel::Serializers
|
5
5
|
|
6
6
|
## Purpose
|
7
7
|
|
8
|
-
`ActiveModel::Serializers` encapsulates the JSON serialization of objects.
|
9
|
-
Objects that respond to read\_attribute\_for\_serialization
|
8
|
+
`ActiveModel::Serializers` encapsulates the JSON serialization of objects.
|
9
|
+
Objects that respond to read\_attribute\_for\_serialization
|
10
10
|
(including `ActiveModel` and `ActiveRecord` objects) are supported.
|
11
11
|
|
12
12
|
Serializers know about both a model and the `current_user`, so you can
|
@@ -94,17 +94,6 @@ serializer when you render the object:
|
|
94
94
|
render json: @post, serializer: FancyPostSerializer
|
95
95
|
```
|
96
96
|
|
97
|
-
### Use serialization outside of ActionController::Base
|
98
|
-
|
99
|
-
When controller does not inherit from ActionController::Base,
|
100
|
-
include Serialization module manually:
|
101
|
-
|
102
|
-
```ruby
|
103
|
-
class ApplicationController < ActionController::API
|
104
|
-
include ActionController::Serialization
|
105
|
-
end
|
106
|
-
```
|
107
|
-
|
108
97
|
## Arrays
|
109
98
|
|
110
99
|
In your controllers, when you use `render :json` for an array of objects, AMS will
|
@@ -240,25 +229,13 @@ ActiveModel::Serializer.setup do |config|
|
|
240
229
|
config.key_format = :lower_camel
|
241
230
|
end
|
242
231
|
|
243
|
-
class BlogLowerCamelSerializer < ActiveModel::Serializer
|
232
|
+
class BlogLowerCamelSerializer < ActiveModel::Serializer
|
244
233
|
format_keys :lower_camel
|
245
234
|
end
|
246
235
|
|
247
236
|
BlogSerializer.new(object, key_format: :lower_camel)
|
248
237
|
```
|
249
238
|
|
250
|
-
## Changing the default association key type
|
251
|
-
|
252
|
-
You can specify that serializers use unsuffixed names as association keys by default.
|
253
|
-
|
254
|
-
`````ruby
|
255
|
-
ActiveModel::Serializer.setup do |config|
|
256
|
-
config.default_key_type = :name
|
257
|
-
end
|
258
|
-
````
|
259
|
-
|
260
|
-
This will build association keys like `comments` or `author` instead of `comment_ids` or `author_id`.
|
261
|
-
|
262
239
|
## Getting the old version
|
263
240
|
|
264
241
|
If you find that your project is already relying on the old rails to_json
|
@@ -304,7 +281,7 @@ Since this shadows any attribute named `object`, you can include them through `o
|
|
304
281
|
|
305
282
|
```ruby
|
306
283
|
class VersionSerializer < ActiveModel::Serializer
|
307
|
-
|
284
|
+
attribute :version_object, key: :object
|
308
285
|
|
309
286
|
def version_object
|
310
287
|
object.object
|
@@ -377,7 +354,7 @@ The above usage of `:meta` will produce the following:
|
|
377
354
|
If you would like to change the meta key name you can use the `:meta_key` option:
|
378
355
|
|
379
356
|
```ruby
|
380
|
-
render json: @posts, serializer: CustomArraySerializer,
|
357
|
+
render json: @posts, serializer: CustomArraySerializer, meta: {total: 10}, meta_key: 'meta_object'
|
381
358
|
```
|
382
359
|
|
383
360
|
The above usage of `:meta_key` will produce the following:
|
@@ -490,6 +467,9 @@ You may also use the `:serializer` option to specify a custom serializer class a
|
|
490
467
|
|
491
468
|
Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.
|
492
469
|
|
470
|
+
NOTE: polymorphic was removed because was only supported for has\_one
|
471
|
+
associations and is in the TODO list of the project.
|
472
|
+
|
493
473
|
## Embedding Associations
|
494
474
|
|
495
475
|
By default, associations will be embedded inside the serialized object. So if
|
@@ -536,15 +516,15 @@ Now, any associations will be supplied as an Array of IDs:
|
|
536
516
|
}
|
537
517
|
```
|
538
518
|
|
539
|
-
You may also choose to embed the IDs by the association's name underneath
|
540
|
-
`
|
519
|
+
You may also choose to embed the IDs by the association's name underneath an
|
520
|
+
`embed_key` for the resource. For example, say we want to change `comment_ids`
|
541
521
|
to `comments` underneath a `links` key:
|
542
522
|
|
543
523
|
```ruby
|
544
524
|
class PostSerializer < ActiveModel::Serializer
|
545
525
|
attributes :id, :title, :body
|
546
526
|
|
547
|
-
has_many :comments, embed:
|
527
|
+
has_many :comments, embed: ids, embed_namespace: :links
|
548
528
|
end
|
549
529
|
```
|
550
530
|
|
@@ -635,7 +615,7 @@ the root document (say, `linked`), you can specify an `embed_in_root_key`:
|
|
635
615
|
|
636
616
|
```ruby
|
637
617
|
class PostSerializer < ActiveModel::Serializer
|
638
|
-
embed
|
618
|
+
embed: ids, include: true, embed_in_root_key: :linked
|
639
619
|
|
640
620
|
attributes: :id, :title, :body
|
641
621
|
has_many :comments, :tags
|
@@ -666,8 +646,8 @@ The above would yield the following JSON document:
|
|
666
646
|
}
|
667
647
|
```
|
668
648
|
|
669
|
-
When side-loading data, your serializer cannot have the `{ root: false }` option,
|
670
|
-
as this would lead to invalid JSON. If you do not have a root key, the `include`
|
649
|
+
When side-loading data, your serializer cannot have the `{ root: false }` option,
|
650
|
+
as this would lead to invalid JSON. If you do not have a root key, the `include`
|
671
651
|
instruction will be ignored
|
672
652
|
|
673
653
|
You can also specify a different root for the embedded objects than the key
|
@@ -706,7 +686,7 @@ class PostSerializer < ActiveModel::Serializer
|
|
706
686
|
embed :ids, include: true
|
707
687
|
|
708
688
|
attributes :id, :title, :body
|
709
|
-
has_many :comments,
|
689
|
+
has_many :comments, embed_key: :external_id
|
710
690
|
end
|
711
691
|
```
|
712
692
|
|
@@ -734,78 +714,6 @@ data looking for information, is extremely useful.
|
|
734
714
|
If you are mostly working with the data in simple scenarios and manually making
|
735
715
|
Ajax requests, you probably just want to use the default embedded behavior.
|
736
716
|
|
737
|
-
|
738
|
-
## Embedding Polymorphic Associations
|
739
|
-
|
740
|
-
Because we need both the id and the type to be able to identify a polymorphic associated model, these are serialized in a slightly different format than common ones.
|
741
|
-
|
742
|
-
When embedding entire objects:
|
743
|
-
|
744
|
-
```ruby
|
745
|
-
class PostSerializer < ActiveModel::Serializer
|
746
|
-
attributes :id, :title
|
747
|
-
has_many :attachments, polymorphic: true
|
748
|
-
end
|
749
|
-
```
|
750
|
-
|
751
|
-
```json
|
752
|
-
{
|
753
|
-
"post": {
|
754
|
-
"id": 1,
|
755
|
-
"title": "New post",
|
756
|
-
"attachments": [
|
757
|
-
{
|
758
|
-
"type": "image"
|
759
|
-
"image": {
|
760
|
-
"id": 3
|
761
|
-
"name": "logo"
|
762
|
-
"url": "http://images.com/logo.jpg"
|
763
|
-
}
|
764
|
-
},
|
765
|
-
{
|
766
|
-
"type": "video"
|
767
|
-
"video": {
|
768
|
-
"id": 12
|
769
|
-
"uid": "XCSSMDFWW"
|
770
|
-
"source": "youtube"
|
771
|
-
}
|
772
|
-
}
|
773
|
-
]
|
774
|
-
}
|
775
|
-
}
|
776
|
-
```
|
777
|
-
|
778
|
-
When embedding ids:
|
779
|
-
|
780
|
-
```ruby
|
781
|
-
class PostSerializer < ActiveModel::Serializer
|
782
|
-
embed :ids
|
783
|
-
|
784
|
-
attributes :id, :title
|
785
|
-
has_many :attachments, polymorphic: true
|
786
|
-
end
|
787
|
-
```
|
788
|
-
|
789
|
-
```json
|
790
|
-
{
|
791
|
-
"post": {
|
792
|
-
"id": 1,
|
793
|
-
"title": "New post",
|
794
|
-
"attachment_ids": [
|
795
|
-
{
|
796
|
-
"type": "image"
|
797
|
-
"id": 12
|
798
|
-
},
|
799
|
-
{
|
800
|
-
"type": "video"
|
801
|
-
"id": 3
|
802
|
-
}
|
803
|
-
]
|
804
|
-
}
|
805
|
-
}
|
806
|
-
```
|
807
|
-
|
808
|
-
|
809
717
|
## Customizing Scope
|
810
718
|
|
811
719
|
In a serializer, `current_user` is the current authorization scope which the controller
|
@@ -45,32 +45,18 @@ module ActionController
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
serializer = build_json_serializer(resource, options)
|
48
|
+
def _render_option_json(resource, options)
|
49
|
+
serializer = build_json_serializer(resource, options)
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
51
|
+
if serializer
|
52
|
+
super(serializer, options)
|
53
|
+
else
|
54
|
+
super
|
57
55
|
end
|
58
56
|
end
|
59
57
|
|
60
58
|
private
|
61
59
|
|
62
|
-
def namespace_for_serializer
|
63
|
-
@namespace_for_serializer ||= self.class.parent unless self.class.parent == Object
|
64
|
-
end
|
65
|
-
|
66
|
-
def default_serializer(resource)
|
67
|
-
options = {}.tap do |o|
|
68
|
-
o[:namespace] = namespace_for_serializer if namespace_for_serializer
|
69
|
-
end
|
70
|
-
|
71
|
-
ActiveModel::Serializer.serializer_for(resource, options)
|
72
|
-
end
|
73
|
-
|
74
60
|
def default_serializer_options
|
75
61
|
{}
|
76
62
|
end
|
@@ -82,15 +68,10 @@ module ActionController
|
|
82
68
|
|
83
69
|
def build_json_serializer(resource, options = {})
|
84
70
|
options = default_serializer_options.merge(options)
|
85
|
-
@namespace_for_serializer = options.fetch(:namespace, nil)
|
86
71
|
|
87
|
-
if serializer = options.fetch(:serializer,
|
72
|
+
if serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
|
88
73
|
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
89
|
-
|
90
|
-
if resource.respond_to?(:each)
|
91
|
-
options[:resource_name] = controller_name
|
92
|
-
options[:namespace] = namespace_for_serializer if namespace_for_serializer
|
93
|
-
end
|
74
|
+
options[:resource_name] = controller_name if resource.respond_to?(:to_ary)
|
94
75
|
|
95
76
|
serializer.new(resource, options)
|
96
77
|
end
|
@@ -3,11 +3,11 @@ module ActionController
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
setup :
|
7
|
-
teardown :
|
6
|
+
setup :setup_subscriptions
|
7
|
+
teardown :teardown_subscriptions
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def setup_subscriptions
|
11
11
|
@serializers = Hash.new(0)
|
12
12
|
|
13
13
|
ActiveSupport::Notifications.subscribe("!serialize.active_model_serializers") do |name, start, finish, id, payload|
|
@@ -16,7 +16,7 @@ module ActionController
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def teardown_subscriptions
|
20
20
|
ActiveSupport::Notifications.unsubscribe("!serialize.active_model_serializers")
|
21
21
|
end
|
22
22
|
|
@@ -15,14 +15,12 @@ module ActiveModel
|
|
15
15
|
@object = object
|
16
16
|
@scope = options[:scope]
|
17
17
|
@root = options.fetch(:root, self.class._root)
|
18
|
-
@polymorphic = options.fetch(:polymorphic, false)
|
19
18
|
@meta_key = options[:meta_key] || :meta
|
20
19
|
@meta = options[@meta_key]
|
21
20
|
@each_serializer = options[:each_serializer]
|
22
21
|
@resource_name = options[:resource_name]
|
23
22
|
@only = options[:only] ? Array(options[:only]) : nil
|
24
23
|
@except = options[:except] ? Array(options[:except]) : nil
|
25
|
-
@namespace = options[:namespace]
|
26
24
|
@key_format = options[:key_format] || options[:each_serializer].try(:key_format)
|
27
25
|
end
|
28
26
|
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format
|
@@ -34,13 +32,13 @@ module ActiveModel
|
|
34
32
|
end
|
35
33
|
|
36
34
|
def serializer_for(item)
|
37
|
-
serializer_class = @each_serializer || Serializer.serializer_for(item
|
38
|
-
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except
|
35
|
+
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
|
36
|
+
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except)
|
39
37
|
end
|
40
38
|
|
41
|
-
def serializable_object
|
39
|
+
def serializable_object
|
42
40
|
@object.map do |item|
|
43
|
-
serializer_for(item).
|
41
|
+
serializer_for(item).serializable_object
|
44
42
|
end
|
45
43
|
end
|
46
44
|
alias_method :serializable_array, :serializable_object
|
@@ -51,11 +49,7 @@ module ActiveModel
|
|
51
49
|
next if !objects || objects.flatten.empty?
|
52
50
|
|
53
51
|
if hash.has_key?(type)
|
54
|
-
|
55
|
-
hash[type].deep_merge!(objects){ |key, old, new| (Array(old) + Array(new)).uniq }
|
56
|
-
else
|
57
|
-
hash[type].concat(objects).uniq!
|
58
|
-
end
|
52
|
+
hash[type].concat(objects).uniq!
|
59
53
|
else
|
60
54
|
hash[type] = objects
|
61
55
|
end
|
@@ -64,7 +58,6 @@ module ActiveModel
|
|
64
58
|
end
|
65
59
|
|
66
60
|
private
|
67
|
-
|
68
61
|
def instrumentation_keys
|
69
62
|
[:object, :scope, :root, :meta_key, :meta, :each_serializer, :resource_name, :key_format]
|
70
63
|
end
|
@@ -1,29 +1,17 @@
|
|
1
|
-
require 'active_model/serializable/utils'
|
2
|
-
|
3
1
|
module ActiveModel
|
4
2
|
module Serializable
|
5
|
-
def self.included(base)
|
6
|
-
base.extend Utils
|
7
|
-
end
|
8
|
-
|
9
3
|
def as_json(options={})
|
10
4
|
instrument('!serialize') do
|
11
5
|
if root = options.fetch(:root, json_key)
|
12
|
-
hash = { root => serializable_object
|
6
|
+
hash = { root => serializable_object }
|
13
7
|
hash.merge!(serializable_data)
|
14
8
|
hash
|
15
9
|
else
|
16
|
-
serializable_object
|
10
|
+
serializable_object
|
17
11
|
end
|
18
12
|
end
|
19
13
|
end
|
20
14
|
|
21
|
-
def serializable_object_with_notification(options={})
|
22
|
-
instrument('!serialize') do
|
23
|
-
serializable_object(options)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
15
|
def serializable_data
|
28
16
|
embedded_in_root_associations.tap do |hash|
|
29
17
|
if respond_to?(:meta) && meta
|
@@ -32,21 +20,11 @@ module ActiveModel
|
|
32
20
|
end
|
33
21
|
end
|
34
22
|
|
35
|
-
def namespace
|
36
|
-
get_namespace && Utils._const_get(get_namespace)
|
37
|
-
end
|
38
|
-
|
39
23
|
def embedded_in_root_associations
|
40
24
|
{}
|
41
25
|
end
|
42
26
|
|
43
27
|
private
|
44
|
-
|
45
|
-
def get_namespace
|
46
|
-
modules = self.class.name.split('::')
|
47
|
-
modules[0..-2].join('::') if modules.size > 1
|
48
|
-
end
|
49
|
-
|
50
28
|
def instrument(action, &block)
|
51
29
|
payload = instrumentation_keys.inject({ serializer: self.class.name }) do |payload, key|
|
52
30
|
payload[:payload] = self.instance_variable_get(:"@#{key}")
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'active_model/array_serializer'
|
2
2
|
require 'active_model/serializable'
|
3
|
-
require 'active_model/serializer/
|
3
|
+
require 'active_model/serializer/associations'
|
4
4
|
require 'active_model/serializer/config'
|
5
5
|
|
6
6
|
require 'thread'
|
@@ -55,15 +55,33 @@ end
|
|
55
55
|
end
|
56
56
|
attr_reader :key_format
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
if
|
61
|
-
|
58
|
+
if RUBY_VERSION >= '2.0'
|
59
|
+
def serializer_for(resource)
|
60
|
+
if resource.respond_to?(:to_ary)
|
61
|
+
if Object.constants.include?(:ArraySerializer)
|
62
|
+
::ArraySerializer
|
63
|
+
else
|
64
|
+
ArraySerializer
|
65
|
+
end
|
62
66
|
else
|
63
|
-
|
67
|
+
begin
|
68
|
+
Object.const_get "#{resource.class.name}Serializer"
|
69
|
+
rescue NameError
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
else
|
75
|
+
def serializer_for(resource)
|
76
|
+
if resource.respond_to?(:to_ary)
|
77
|
+
if Object.constants.include?(:ArraySerializer)
|
78
|
+
::ArraySerializer
|
79
|
+
else
|
80
|
+
ArraySerializer
|
81
|
+
end
|
82
|
+
else
|
83
|
+
"#{resource.class.name}Serializer".safe_constantize
|
64
84
|
end
|
65
|
-
else
|
66
|
-
_const_get build_serializer_class(resource, options)
|
67
85
|
end
|
68
86
|
end
|
69
87
|
|
@@ -72,19 +90,14 @@ end
|
|
72
90
|
alias root= _root=
|
73
91
|
|
74
92
|
def root_name
|
75
|
-
if name
|
76
|
-
root_name = name.demodulize.underscore.sub(/_serializer$/, '')
|
77
|
-
CONFIG.plural_default_root ? root_name.pluralize : root_name
|
78
|
-
end
|
93
|
+
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
79
94
|
end
|
80
95
|
|
81
96
|
def attributes(*attrs)
|
82
|
-
|
83
|
-
striped_attr = strip_attribute attr
|
97
|
+
@_attributes.concat attrs
|
84
98
|
|
85
|
-
|
86
|
-
|
87
|
-
define_method striped_attr do
|
99
|
+
attrs.each do |attr|
|
100
|
+
define_method attr do
|
88
101
|
object.read_attribute_for_serialization attr
|
89
102
|
end unless method_defined?(attr)
|
90
103
|
end
|
@@ -100,22 +113,6 @@ end
|
|
100
113
|
|
101
114
|
private
|
102
115
|
|
103
|
-
def strip_attribute(attr)
|
104
|
-
symbolized = attr.is_a?(Symbol)
|
105
|
-
|
106
|
-
attr = attr.to_s.gsub(/\?\Z/, '')
|
107
|
-
attr = attr.to_sym if symbolized
|
108
|
-
attr
|
109
|
-
end
|
110
|
-
|
111
|
-
def build_serializer_class(resource, options)
|
112
|
-
"".tap do |klass_name|
|
113
|
-
klass_name << "#{options[:namespace]}::" if options[:namespace]
|
114
|
-
klass_name << options[:prefix].to_s.classify if options[:prefix]
|
115
|
-
klass_name << "#{resource.class.name}Serializer"
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
116
|
def associate(klass, *attrs)
|
120
117
|
options = attrs.extract_options!
|
121
118
|
|
@@ -133,7 +130,6 @@ end
|
|
133
130
|
@object = object
|
134
131
|
@scope = options[:scope]
|
135
132
|
@root = options.fetch(:root, self.class._root)
|
136
|
-
@polymorphic = options.fetch(:polymorphic, false)
|
137
133
|
@meta_key = options[:meta_key] || :meta
|
138
134
|
@meta = options[@meta_key]
|
139
135
|
@wrap_in_array = options[:_wrap_in_array]
|
@@ -141,9 +137,8 @@ end
|
|
141
137
|
@except = options[:except] ? Array(options[:except]) : nil
|
142
138
|
@key_format = options[:key_format]
|
143
139
|
@context = options[:context]
|
144
|
-
@namespace = options[:namespace]
|
145
140
|
end
|
146
|
-
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context
|
141
|
+
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context
|
147
142
|
|
148
143
|
def json_key
|
149
144
|
key = if root == true || root.nil?
|
@@ -199,15 +194,12 @@ end
|
|
199
194
|
included_associations = filter(associations.keys)
|
200
195
|
associations.each_with_object({}) do |(name, association), hash|
|
201
196
|
if included_associations.include? name
|
202
|
-
association_serializer = build_serializer(association)
|
203
|
-
# we must do this always because even if the current association is not
|
204
|
-
# embeded in root, it might have its own associations that are embeded in root
|
205
|
-
hash.merge!(association_serializer.embedded_in_root_associations) {|key, oldval, newval| [newval, oldval].flatten }
|
206
|
-
|
207
197
|
if association.embed_in_root?
|
208
198
|
if association.embed_in_root_key?
|
209
199
|
hash = hash[association.embed_in_root_key] ||= {}
|
210
200
|
end
|
201
|
+
association_serializer = build_serializer(association)
|
202
|
+
hash.merge!(association_serializer.embedded_in_root_associations) {|key, oldval, newval| [newval, oldval].flatten }
|
211
203
|
|
212
204
|
serialized_data = association_serializer.serializable_object
|
213
205
|
key = association.root_key
|
@@ -223,17 +215,7 @@ end
|
|
223
215
|
|
224
216
|
def build_serializer(association)
|
225
217
|
object = send(association.name)
|
226
|
-
association.build_serializer(object,
|
227
|
-
end
|
228
|
-
|
229
|
-
def association_options_for_serializer(association)
|
230
|
-
prefix = association.options[:prefix]
|
231
|
-
namespace = association.options[:namespace] || @namespace || self.namespace
|
232
|
-
|
233
|
-
{ scope: scope }.tap do |opts|
|
234
|
-
opts[:namespace] = namespace if namespace
|
235
|
-
opts[:prefix] = prefix if prefix
|
236
|
-
end
|
218
|
+
association.build_serializer(object, scope: scope)
|
237
219
|
end
|
238
220
|
|
239
221
|
def serialize(association)
|
@@ -243,9 +225,9 @@ end
|
|
243
225
|
def serialize_ids(association)
|
244
226
|
associated_data = send(association.name)
|
245
227
|
if associated_data.respond_to?(:to_ary)
|
246
|
-
associated_data.map { |elem|
|
228
|
+
associated_data.map { |elem| elem.read_attribute_for_serialization(association.embed_key) }
|
247
229
|
else
|
248
|
-
|
230
|
+
associated_data.read_attribute_for_serialization(association.embed_key) if associated_data
|
249
231
|
end
|
250
232
|
end
|
251
233
|
|
@@ -273,30 +255,14 @@ end
|
|
273
255
|
end]
|
274
256
|
end
|
275
257
|
|
276
|
-
attr_writer :serialization_options
|
277
|
-
def serialization_options
|
278
|
-
@serialization_options || {}
|
279
|
-
end
|
280
|
-
|
281
258
|
def serializable_object(options={})
|
282
|
-
self.serialization_options = options
|
283
259
|
return @wrap_in_array ? [] : nil if @object.nil?
|
284
260
|
hash = attributes
|
285
261
|
hash.merge! associations
|
286
262
|
hash = convert_keys(hash) if key_format.present?
|
287
|
-
hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic
|
288
263
|
@wrap_in_array ? [hash] : hash
|
289
264
|
end
|
290
265
|
alias_method :serializable_hash, :serializable_object
|
291
|
-
|
292
|
-
def serialize_id(elem, association)
|
293
|
-
id = elem.read_attribute_for_serialization(association.embed_key)
|
294
|
-
association.polymorphic? ? { id: id, type: type_name(elem) } : id
|
295
|
-
end
|
296
|
-
|
297
|
-
def type_name(elem)
|
298
|
-
elem.class.to_s.demodulize.underscore.to_sym
|
299
|
-
end
|
300
266
|
end
|
301
267
|
|
302
268
|
end
|