active_model_serializers 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +91 -0
- data/README.md +9 -8
- data/lib/active_model/array_serializer.rb +5 -4
- data/lib/active_model/serializer.rb +11 -7
- data/lib/active_model/serializer/association/has_one.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers/mime_types.rb +14 -0
- data/test/fixtures/active_record.rb +4 -0
- data/test/fixtures/poro.rb +8 -1
- data/test/integration/action_controller/namespaced_serialization_test.rb +10 -1
- data/test/integration/action_controller/serialization_test.rb +5 -5
- data/test/integration/active_record/active_record_test.rb +17 -0
- data/test/unit/active_model/array_serializer/options_test.rb +16 -0
- data/test/unit/active_model/array_serializer/serialization_test.rb +17 -0
- data/test/unit/active_model/serializer/has_many_test.rb +1 -1
- data/test/unit/active_model/serializer/has_one_test.rb +14 -0
- data/test/unit/active_model/serializer/options_test.rb +8 -0
- metadata +7 -5
- data/test/serializers/tmp/app/serializers/account_serializer.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f42ff2b93cdc399b58c219efd12add5e5757612a
|
4
|
+
data.tar.gz: 624cba1a3b71a9aa668f7612b1937a889b98d22e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4abbf3c69e8de7d29b24aab129229e5bfa6c94eb46c73212016d02592ea8fb3d3fbba6eb0effef1f8b0b3a03ca359f71933678fe98da58db21590f76f1c02dd3
|
7
|
+
data.tar.gz: b5730a2a45db222ee3b331e1ef3a1ba68efc86c17dc7d2c8172c59d4bb4273624031085d331b96078f861d764cb7b8a58d8405ffd9c081af5268b7b319688e94
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,94 @@
|
|
1
|
+
## 0.09.x
|
2
|
+
|
3
|
+
### [0-9-stable](https://github.com/rails-api/active_model_serializers/compare/v0.9.4...0-9-stable)
|
4
|
+
|
5
|
+
### [v0.9.4 (2015-01-05)](https://github.com/rails-api/active_model_serializers/compare/v0.9.3...v0.9.4)
|
6
|
+
|
7
|
+
- [#752](https://github.com/rails-api/active_model_serializers/pull/752) Tiny improvement of README 0-9-stable (@basiam)
|
8
|
+
- [#749](https://github.com/rails-api/active_model_serializers/pull/749) remove trailing whitespace (@shwoodard)
|
9
|
+
- [#717](https://github.com/rails-api/active_model_serializers/pull/717) fixed issue with rendering Hash which appears in rails 4.2.0.beta4 (@kurko, @greshny)
|
10
|
+
- [#790](https://github.com/rails-api/active_model_serializers/pull/790) pass context to ArraySerializer (@lanej)
|
11
|
+
- [#797](https://github.com/rails-api/active_model_serializers/pull/797) Fix and test for #490 (@afn)
|
12
|
+
- [#813](https://github.com/rails-api/active_model_serializers/pull/813) Allow to define custom serializer for given class (@jtomaszewski)
|
13
|
+
- [#841](https://github.com/rails-api/active_model_serializers/pull/841) Fix issue with embedding multiple associations under the same root key (@antstorm)
|
14
|
+
- [#748](https://github.com/rails-api/active_model_serializers/pull/748) Propagate serialization_options across associations (@raphaelpereira)
|
15
|
+
|
16
|
+
### [v0.9.3 (2015/01/21 20:29 +00:00)](https://github.com/rails-api/active_model_serializers/compare/v0.9.2...v0.9.3)
|
17
|
+
|
18
|
+
Features:
|
19
|
+
- [#774](https://github.com/rails-api/active_model_serializers/pull/774) Fix nested include attributes (@nhocki)
|
20
|
+
- [#771](https://github.com/rails-api/active_model_serializers/pull/771) Make linked resource type names consistent with root names (@sweatypitts)
|
21
|
+
- [#696](https://github.com/rails-api/active_model_serializers/pull/696) Explicitly set serializer for associations (@ggordon)
|
22
|
+
- [#700](https://github.com/rails-api/active_model_serializers/pull/700) sparse fieldsets (@arenoir)
|
23
|
+
- [#768](https://github.com/rails-api/active_model_serializers/pull/768) Adds support for `meta` and `meta_key` attribute (@kurko)
|
24
|
+
|
25
|
+
### [v0.9.2](https://github.com/rails-api/active_model_serializers/compare/v0.9.1...v0.9.2)
|
26
|
+
|
27
|
+
### [v0.9.1 (2014/12/04 11:54 +00:00)](https://github.com/rails-api/active_model_serializers/compare/v0.9.0...v0.9.1)
|
28
|
+
|
29
|
+
- [#707](https://github.com/rails-api/active_model_serializers/pull/707) A Friendly Note on Which AMS Version to Use (@jherdman)
|
30
|
+
- [#730](https://github.com/rails-api/active_model_serializers/pull/730) Fixes nested has_many links in JSONAPI (@kurko)
|
31
|
+
- [#718](https://github.com/rails-api/active_model_serializers/pull/718) Allow overriding the adapter with render option (@ggordon)
|
32
|
+
- [#720](https://github.com/rails-api/active_model_serializers/pull/720) Rename attribute with :key (0.8.x compatibility) (@ggordon)
|
33
|
+
- [#728](https://github.com/rails-api/active_model_serializers/pull/728) Use type as key for linked resources (@kurko)
|
34
|
+
- [#729](https://github.com/rails-api/active_model_serializers/pull/729) Use the new beta build env on Travis (@joshk)
|
35
|
+
- [#703](https://github.com/rails-api/active_model_serializers/pull/703) Support serializer and each_serializer options in renderer (@ggordon, @mieko)
|
36
|
+
- [#727](https://github.com/rails-api/active_model_serializers/pull/727) Includes links inside of linked resources (@kurko)
|
37
|
+
- [#726](https://github.com/rails-api/active_model_serializers/pull/726) Bugfix: include nested has_many associations (@kurko)
|
38
|
+
- [#722](https://github.com/rails-api/active_model_serializers/pull/722) Fix infinite recursion (@ggordon)
|
39
|
+
- [#1](https://github.com/rails-api/active_model_serializers/pull/1) Allow for the implicit use of ArraySerializer when :each_serializer is specified (@mieko)
|
40
|
+
- [#692](https://github.com/rails-api/active_model_serializers/pull/692) Include 'linked' member for json-api collections (@ggordon)
|
41
|
+
- [#714](https://github.com/rails-api/active_model_serializers/pull/714) Define as_json instead of to_json (@guilleiguaran)
|
42
|
+
- [#710](https://github.com/rails-api/active_model_serializers/pull/710) JSON-API: Don't include linked section if associations are empty (@guilleiguaran)
|
43
|
+
- [#711](https://github.com/rails-api/active_model_serializers/pull/711) Fixes rbx gems bundling on TravisCI (@kurko)
|
44
|
+
- [#709](https://github.com/rails-api/active_model_serializers/pull/709) Add type key when association name is different than object type (@guilleiguaran)
|
45
|
+
- [#708](https://github.com/rails-api/active_model_serializers/pull/708) Handle correctly null associations (@guilleiguaran)
|
46
|
+
- [#691](https://github.com/rails-api/active_model_serializers/pull/691) Fix embed option for associations (@jacob-s-son)
|
47
|
+
- [#689](https://github.com/rails-api/active_model_serializers/pull/689) Fix support for custom root in JSON-API adapter (@guilleiguaran)
|
48
|
+
- [#685](https://github.com/rails-api/active_model_serializers/pull/685) Serialize ids as strings in JSON-API adapter (@guilleiguaran)
|
49
|
+
- [#684](https://github.com/rails-api/active_model_serializers/pull/684) Refactor adapters to implement support for array serialization (@guilleiguaran)
|
50
|
+
- [#682](https://github.com/rails-api/active_model_serializers/pull/682) Include root by default in JSON-API serializers (@guilleiguaran)
|
51
|
+
- [#625](https://github.com/rails-api/active_model_serializers/pull/625) Add DSL for urls (@JordanFaust)
|
52
|
+
- [#677](https://github.com/rails-api/active_model_serializers/pull/677) Add support for embed: :ids option for in associations (@guilleiguaran)
|
53
|
+
- [#681](https://github.com/rails-api/active_model_serializers/pull/681) Check superclasses for Serializers (@quainjn)
|
54
|
+
- [#680](https://github.com/rails-api/active_model_serializers/pull/680) Add support for root keys (@NullVoxPopuli)
|
55
|
+
- [#675](https://github.com/rails-api/active_model_serializers/pull/675) Support Rails 4.2.0 (@tricknotes)
|
56
|
+
- [#667](https://github.com/rails-api/active_model_serializers/pull/667) Require only activemodel instead of full rails (@guilleiguaran)
|
57
|
+
- [#653](https://github.com/rails-api/active_model_serializers/pull/653) Add "_test" suffix to JsonApi::HasManyTest filename. (@alexgenco)
|
58
|
+
- [#631](https://github.com/rails-api/active_model_serializers/pull/631) Update build badge URL (@craiglittle)
|
59
|
+
|
60
|
+
### [v0.9.0](https://github.com/rails-api/active_model_serializers/compare/v0.9.0.alpha1...v0.9.0)
|
61
|
+
|
62
|
+
### [0.9.0.alpha1 - January 7, 2014](https://github.com/rails-api/active_model_serializers/compare/d72b66d4c...v0.9.0.alpha1)
|
63
|
+
|
64
|
+
### 0.9.0.pre
|
65
|
+
|
66
|
+
* The following methods were removed
|
67
|
+
- Model#active\_model\_serializer
|
68
|
+
- Serializer#include!
|
69
|
+
- Serializer#include?
|
70
|
+
- Serializer#attr\_disabled=
|
71
|
+
- Serializer#cache
|
72
|
+
- Serializer#perform\_caching
|
73
|
+
- Serializer#schema (needs more discussion)
|
74
|
+
- Serializer#attribute
|
75
|
+
- Serializer#include\_#{name}? (filter method added)
|
76
|
+
- Serializer#attributes (took a hash)
|
77
|
+
|
78
|
+
* The following things were added
|
79
|
+
- Serializer#filter method
|
80
|
+
- CONFIG object
|
81
|
+
|
82
|
+
* Remove support for ruby 1.8 versions.
|
83
|
+
|
84
|
+
* Require rails >= 3.2.
|
85
|
+
|
86
|
+
* Serializers for associations are being looked up in a parent serializer's namespace first. Same with controllers' namespaces.
|
87
|
+
|
88
|
+
* Added a "prefix" option in case you want to use a different version of serializer.
|
89
|
+
|
90
|
+
* Serializers default namespace can be set in `default_serializer_options` and inherited by associations.
|
91
|
+
|
1
92
|
# VERSION 0.9.0.pre
|
2
93
|
|
3
94
|
* The following methods were removed
|
data/README.md
CHANGED
@@ -339,6 +339,7 @@ And it's also safe to mutate keys argument by doing keys.delete(:author)
|
|
339
339
|
in case you want to avoid creating two extra arrays. Note that if you do an
|
340
340
|
in-place modification, you still need to return the modified array.
|
341
341
|
|
342
|
+
### Alias Attribute
|
342
343
|
If you would like the key in the outputted JSON to be different from its name
|
343
344
|
in ActiveRecord, you can declare the attribute with the different name
|
344
345
|
and redefine that method:
|
@@ -755,18 +756,18 @@ end
|
|
755
756
|
"title": "New post",
|
756
757
|
"attachments": [
|
757
758
|
{
|
758
|
-
"type": "image"
|
759
|
+
"type": "image",
|
759
760
|
"image": {
|
760
|
-
"id": 3
|
761
|
-
"name": "logo"
|
761
|
+
"id": 3,
|
762
|
+
"name": "logo",
|
762
763
|
"url": "http://images.com/logo.jpg"
|
763
764
|
}
|
764
765
|
},
|
765
766
|
{
|
766
|
-
"type": "video"
|
767
|
+
"type": "video",
|
767
768
|
"video": {
|
768
|
-
"id": 12
|
769
|
-
"uid": "XCSSMDFWW"
|
769
|
+
"id": 12,
|
770
|
+
"uid": "XCSSMDFWW",
|
770
771
|
"source": "youtube"
|
771
772
|
}
|
772
773
|
}
|
@@ -793,11 +794,11 @@ end
|
|
793
794
|
"title": "New post",
|
794
795
|
"attachment_ids": [
|
795
796
|
{
|
796
|
-
"type": "image"
|
797
|
+
"type": "image",
|
797
798
|
"id": 12
|
798
799
|
},
|
799
800
|
{
|
800
|
-
"type": "video"
|
801
|
+
"type": "video",
|
801
802
|
"id": 3
|
802
803
|
}
|
803
804
|
]
|
@@ -15,17 +15,18 @@ module ActiveModel
|
|
15
15
|
@object = object
|
16
16
|
@scope = options[:scope]
|
17
17
|
@root = options.fetch(:root, self.class._root)
|
18
|
-
@polymorphic
|
18
|
+
@polymorphic = options.fetch(:polymorphic, false)
|
19
19
|
@meta_key = options[:meta_key] || :meta
|
20
20
|
@meta = options[@meta_key]
|
21
21
|
@each_serializer = options[:each_serializer]
|
22
22
|
@resource_name = options[:resource_name]
|
23
23
|
@only = options[:only] ? Array(options[:only]) : nil
|
24
24
|
@except = options[:except] ? Array(options[:except]) : nil
|
25
|
+
@context = options[:context]
|
25
26
|
@namespace = options[:namespace]
|
26
27
|
@key_format = options[:key_format] || options[:each_serializer].try(:key_format)
|
27
28
|
end
|
28
|
-
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format
|
29
|
+
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context
|
29
30
|
|
30
31
|
def json_key
|
31
32
|
key = root.nil? ? @resource_name : root
|
@@ -35,7 +36,7 @@ module ActiveModel
|
|
35
36
|
|
36
37
|
def serializer_for(item)
|
37
38
|
serializer_class = @each_serializer || Serializer.serializer_for(item, namespace: @namespace) || DefaultSerializer
|
38
|
-
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace)
|
39
|
+
serializer_class.new(item, scope: scope, key_format: key_format, context: @context, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace)
|
39
40
|
end
|
40
41
|
|
41
42
|
def serializable_object(options={})
|
@@ -66,7 +67,7 @@ module ActiveModel
|
|
66
67
|
private
|
67
68
|
|
68
69
|
def instrumentation_keys
|
69
|
-
[:object, :scope, :root, :meta_key, :meta, :each_serializer, :resource_name, :key_format]
|
70
|
+
[:object, :scope, :root, :meta_key, :meta, :each_serializer, :resource_name, :key_format, :context]
|
70
71
|
end
|
71
72
|
end
|
72
73
|
end
|
@@ -56,7 +56,9 @@ end
|
|
56
56
|
attr_reader :key_format
|
57
57
|
|
58
58
|
def serializer_for(resource, options = {})
|
59
|
-
if resource.respond_to?(:
|
59
|
+
if resource.respond_to?(:serializer_class)
|
60
|
+
resource.serializer_class
|
61
|
+
elsif resource.respond_to?(:to_ary)
|
60
62
|
if Object.constants.include?(:ArraySerializer)
|
61
63
|
::ArraySerializer
|
62
64
|
else
|
@@ -161,7 +163,7 @@ end
|
|
161
163
|
end
|
162
164
|
end
|
163
165
|
|
164
|
-
def associations
|
166
|
+
def associations(options={})
|
165
167
|
associations = self.class._associations
|
166
168
|
included_associations = filter(associations.keys)
|
167
169
|
associations.each_with_object({}) do |(name, association), hash|
|
@@ -178,7 +180,7 @@ end
|
|
178
180
|
if association.embed_namespace?
|
179
181
|
hash = hash[association.embed_namespace] ||= {}
|
180
182
|
end
|
181
|
-
hash[association.embedded_key] = serialize association
|
183
|
+
hash[association.embedded_key] = serialize association, options
|
182
184
|
end
|
183
185
|
end
|
184
186
|
end
|
@@ -202,7 +204,9 @@ end
|
|
202
204
|
association_serializer = build_serializer(association)
|
203
205
|
# we must do this always because even if the current association is not
|
204
206
|
# embeded in root, it might have its own associations that are embeded in root
|
205
|
-
hash.merge!(association_serializer.embedded_in_root_associations)
|
207
|
+
hash.merge!(association_serializer.embedded_in_root_associations) do |key, oldval, newval|
|
208
|
+
oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq }
|
209
|
+
end
|
206
210
|
|
207
211
|
if association.embed_in_root?
|
208
212
|
if association.embed_in_root_key?
|
@@ -236,8 +240,8 @@ end
|
|
236
240
|
end
|
237
241
|
end
|
238
242
|
|
239
|
-
def serialize(association)
|
240
|
-
build_serializer(association).serializable_object
|
243
|
+
def serialize(association,options={})
|
244
|
+
build_serializer(association).serializable_object(options)
|
241
245
|
end
|
242
246
|
|
243
247
|
def serialize_ids(association)
|
@@ -282,7 +286,7 @@ end
|
|
282
286
|
self.serialization_options = options
|
283
287
|
return @wrap_in_array ? [] : nil if @object.nil?
|
284
288
|
hash = attributes
|
285
|
-
hash.merge! associations
|
289
|
+
hash.merge! associations(options)
|
286
290
|
hash = convert_keys(hash) if key_format.present?
|
287
291
|
hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic
|
288
292
|
@wrap_in_array ? [hash] : hash
|
@@ -12,7 +12,7 @@ module ActiveModel
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def serializer_class(object, options = {})
|
15
|
-
serializer_from_options || serializer_from_object(object, options) || default_serializer
|
15
|
+
(serializer_from_options unless object.nil?) || serializer_from_object(object, options) || default_serializer
|
16
16
|
end
|
17
17
|
|
18
18
|
def build_serializer(object, options = {})
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# module ActiveModelSerializers::JSONAPI
|
2
|
+
# HEADERS = {
|
3
|
+
# content_type: { "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE }
|
4
|
+
# }
|
5
|
+
# MEDIA_TYPE = 'application/vnd.api+json'
|
6
|
+
# end
|
7
|
+
#
|
8
|
+
# Mime::Type.register ActiveModelSerializers::JSONAPI::MEDIA_TYPE, :api_json
|
9
|
+
#
|
10
|
+
# ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup(ActiveModelSerializers::JSONAPI::MEDIA_TYPE)] = lambda do |body|
|
11
|
+
# data = JSON.parse(body)
|
12
|
+
# data = {:_json => data} unless data.is_a?(Hash)
|
13
|
+
# data.with_indifferent_access
|
14
|
+
# end
|
@@ -74,6 +74,10 @@ class ARSectionSerializer < ActiveModel::Serializer
|
|
74
74
|
attributes 'name'
|
75
75
|
end
|
76
76
|
|
77
|
+
class AREmbeddedSerializer < ActiveModel::Serializer
|
78
|
+
has_many :ar_tags, :ar_comments
|
79
|
+
end
|
80
|
+
|
77
81
|
ARPost.create(title: 'New post',
|
78
82
|
body: 'A body!!!',
|
79
83
|
ar_section: ARSection.create(name: 'ruby')).tap do |post|
|
data/test/fixtures/poro.rb
CHANGED
@@ -88,7 +88,7 @@ class UserSerializer < ActiveModel::Serializer
|
|
88
88
|
end
|
89
89
|
|
90
90
|
class UserInfoSerializer < ActiveModel::Serializer
|
91
|
-
has_one :user
|
91
|
+
has_one :user, serializer: UserSerializer
|
92
92
|
end
|
93
93
|
|
94
94
|
class ProfileSerializer < ActiveModel::Serializer
|
@@ -109,6 +109,13 @@ end
|
|
109
109
|
class PostSerializer < ActiveModel::Serializer
|
110
110
|
attributes :title, :body
|
111
111
|
|
112
|
+
def title
|
113
|
+
keyword = serialization_options[:highlight_keyword]
|
114
|
+
title = object.read_attribute_for_serialization(:title)
|
115
|
+
title = title.gsub(keyword,"'#{keyword}'") if keyword
|
116
|
+
title
|
117
|
+
end
|
118
|
+
|
112
119
|
has_many :comments
|
113
120
|
end
|
114
121
|
|
@@ -19,6 +19,10 @@ module ActionController
|
|
19
19
|
def render_comments
|
20
20
|
render json: [Comment.new(content: 'Comment 1')]
|
21
21
|
end
|
22
|
+
|
23
|
+
def render_hash
|
24
|
+
render json: {message: 'not found'}, status: 404
|
25
|
+
end
|
22
26
|
end
|
23
27
|
|
24
28
|
tests TestNamespace::MyController
|
@@ -42,6 +46,11 @@ module ActionController
|
|
42
46
|
get :render_comments
|
43
47
|
assert_serializer CommentSerializer
|
44
48
|
end
|
49
|
+
|
50
|
+
def test_render_hash_regression
|
51
|
+
get :render_hash
|
52
|
+
assert_equal JSON.parse(response.body), {'message' => 'not found'}
|
53
|
+
end
|
45
54
|
end
|
46
55
|
|
47
56
|
class OptionNamespacedSerializationTest < ActionController::TestCase
|
@@ -93,4 +102,4 @@ module ActionController
|
|
93
102
|
end
|
94
103
|
|
95
104
|
end
|
96
|
-
end
|
105
|
+
end
|
@@ -213,8 +213,8 @@ module ActionController
|
|
213
213
|
class LowerCamelWoRootSerializerTest < ActionController::TestCase
|
214
214
|
class WebLogController < ActionController::Base
|
215
215
|
def render_without_root
|
216
|
-
render json: WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}),
|
217
|
-
root: false,
|
216
|
+
render json: WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}),
|
217
|
+
root: false,
|
218
218
|
serializer: WebLogLowerCamelSerializer
|
219
219
|
end
|
220
220
|
end
|
@@ -231,9 +231,9 @@ module ActionController
|
|
231
231
|
class LowerCamelArrayWoRootSerializerTest < ActionController::TestCase
|
232
232
|
class WebLogController < ActionController::Base
|
233
233
|
def render_array_without_root
|
234
|
-
render json: [WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}),
|
235
|
-
WebLog.new({name: 'Name 2', display_name: 'Display Name 2'})],
|
236
|
-
root: false,
|
234
|
+
render json: [WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}),
|
235
|
+
WebLog.new({name: 'Name 2', display_name: 'Display Name 2'})],
|
236
|
+
root: false,
|
237
237
|
each_serializer: WebLogLowerCamelSerializer
|
238
238
|
end
|
239
239
|
end
|
@@ -58,6 +58,22 @@ module ActiveModel
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
+
def test_serialization_embedding_ids_in_common_root_key
|
62
|
+
post_serializer = AREmbeddedSerializer.new(@post)
|
63
|
+
|
64
|
+
embed(AREmbeddedSerializer, embed: :ids, embed_in_root: true, embed_in_root_key: :linked) do
|
65
|
+
embed(ARCommentSerializer, embed: :ids, embed_in_root: true, embed_in_root_key: :linked) do
|
66
|
+
assert_equal({
|
67
|
+
'ar_tags' => [{ name: 'short' },
|
68
|
+
{ name: 'whiny' },
|
69
|
+
{ name: 'happy' }],
|
70
|
+
'ar_comments' => [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
|
71
|
+
{ body: 'i liked it', 'ar_tag_ids' => [3, 1] }]
|
72
|
+
}, post_serializer.as_json[:linked])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
61
77
|
private
|
62
78
|
|
63
79
|
def embed(serializer_class, options = {})
|
@@ -66,6 +82,7 @@ module ActiveModel
|
|
66
82
|
serializer_class._associations.each_value do |association|
|
67
83
|
association.embed = options[:embed]
|
68
84
|
association.embed_in_root = options[:embed_in_root]
|
85
|
+
association.embed_in_root_key = options[:embed_in_root_key]
|
69
86
|
end
|
70
87
|
|
71
88
|
yield
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class OptionsTest < Minitest::Test
|
6
|
+
def test_custom_options_are_accessible_from_serializer
|
7
|
+
|
8
|
+
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
9
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
10
|
+
serializer = ArraySerializer.new(array, only: [:name], context: {foo: :bar})
|
11
|
+
|
12
|
+
assert_equal({foo: :bar}, serializer.context)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -35,6 +35,23 @@ module ActiveModel
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
+
class CustomSerializerClassTest < Minitest::Test
|
39
|
+
def setup
|
40
|
+
Object.const_set(:CustomSerializer, Class.new)
|
41
|
+
end
|
42
|
+
|
43
|
+
def teardown
|
44
|
+
Object.send(:remove_const, :CustomSerializer)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_serializer_for_array_returns_appropriate_type
|
48
|
+
object = {}
|
49
|
+
def object.serializer_class; CustomSerializer; end
|
50
|
+
|
51
|
+
assert_equal CustomSerializer, Serializer.serializer_for(object)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
38
55
|
class ModelSerializationTest < Minitest::Test
|
39
56
|
def test_array_serializer_serializes_models
|
40
57
|
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
@@ -176,7 +176,7 @@ module ActiveModel
|
|
176
176
|
|
177
177
|
def test_associations_embedding_objects_using_a_given_array_serializer
|
178
178
|
@association.serializer_from_options = Class.new(ArraySerializer) do
|
179
|
-
def serializable_object
|
179
|
+
def serializable_object(options={})
|
180
180
|
{ my_content: ['fake'] }
|
181
181
|
end
|
182
182
|
end
|
@@ -176,6 +176,20 @@ module ActiveModel
|
|
176
176
|
}, @user_serializer.as_json)
|
177
177
|
end
|
178
178
|
|
179
|
+
def test_associations_embedding_objects_with_nil_values
|
180
|
+
user_info = UserInfo.new
|
181
|
+
user_info.instance_eval do
|
182
|
+
def user
|
183
|
+
nil
|
184
|
+
end
|
185
|
+
end
|
186
|
+
user_info_serializer = UserInfoSerializer.new(user_info)
|
187
|
+
|
188
|
+
assert_equal({
|
189
|
+
'user_info' => { user: nil }
|
190
|
+
}, user_info_serializer.as_json)
|
191
|
+
end
|
192
|
+
|
179
193
|
def test_associations_embedding_ids_using_embed_namespace
|
180
194
|
@association.embed_namespace = :links
|
181
195
|
@association.embed = :ids
|
@@ -21,6 +21,9 @@ module ActiveModel
|
|
21
21
|
serialization_options[:force_the_description]
|
22
22
|
end
|
23
23
|
end
|
24
|
+
|
25
|
+
@category = Category.new({name: 'Category 1'})
|
26
|
+
@category_serializer = CategorySerializer.new(@category)
|
24
27
|
end
|
25
28
|
|
26
29
|
def test_filtered_attributes_serialization
|
@@ -29,6 +32,11 @@ module ActiveModel
|
|
29
32
|
'profile' => { name: 'Name 1', description: forced_description }
|
30
33
|
}, @profile_serializer.as_json(force_the_description: forced_description))
|
31
34
|
end
|
35
|
+
|
36
|
+
def test_filtered_attributes_serialization_across_association
|
37
|
+
assert_equal("'T1'",
|
38
|
+
@category_serializer.as_json(highlight_keyword: 'T1')['category'][:posts][0][:title])
|
39
|
+
end
|
32
40
|
end
|
33
41
|
end
|
34
42
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_model_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- José Valim
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2016-01-05 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- lib/active_model/serializer/version.rb
|
76
76
|
- lib/active_model/serializer_support.rb
|
77
77
|
- lib/active_model_serializers.rb
|
78
|
+
- lib/active_model_serializers/mime_types.rb
|
78
79
|
- test/fixtures/active_record.rb
|
79
80
|
- test/fixtures/poro.rb
|
80
81
|
- test/fixtures/template.html.erb
|
@@ -85,13 +86,13 @@ files:
|
|
85
86
|
- test/integration/generators/resource_generator_test.rb
|
86
87
|
- test/integration/generators/scaffold_controller_generator_test.rb
|
87
88
|
- test/integration/generators/serializer_generator_test.rb
|
88
|
-
- test/serializers/tmp/app/serializers/account_serializer.rb
|
89
89
|
- test/test_app.rb
|
90
90
|
- test/test_helper.rb
|
91
91
|
- test/unit/active_model/array_serializer/except_test.rb
|
92
92
|
- test/unit/active_model/array_serializer/key_format_test.rb
|
93
93
|
- test/unit/active_model/array_serializer/meta_test.rb
|
94
94
|
- test/unit/active_model/array_serializer/only_test.rb
|
95
|
+
- test/unit/active_model/array_serializer/options_test.rb
|
95
96
|
- test/unit/active_model/array_serializer/root_test.rb
|
96
97
|
- test/unit/active_model/array_serializer/scope_test.rb
|
97
98
|
- test/unit/active_model/array_serializer/serialization_test.rb
|
@@ -132,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
133
|
version: '0'
|
133
134
|
requirements: []
|
134
135
|
rubyforge_project:
|
135
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.4.8
|
136
137
|
signing_key:
|
137
138
|
specification_version: 4
|
138
139
|
summary: Bringing consistency and object orientation to model serialization. Works
|
@@ -148,13 +149,13 @@ test_files:
|
|
148
149
|
- test/integration/generators/resource_generator_test.rb
|
149
150
|
- test/integration/generators/scaffold_controller_generator_test.rb
|
150
151
|
- test/integration/generators/serializer_generator_test.rb
|
151
|
-
- test/serializers/tmp/app/serializers/account_serializer.rb
|
152
152
|
- test/test_app.rb
|
153
153
|
- test/test_helper.rb
|
154
154
|
- test/unit/active_model/array_serializer/except_test.rb
|
155
155
|
- test/unit/active_model/array_serializer/key_format_test.rb
|
156
156
|
- test/unit/active_model/array_serializer/meta_test.rb
|
157
157
|
- test/unit/active_model/array_serializer/only_test.rb
|
158
|
+
- test/unit/active_model/array_serializer/options_test.rb
|
158
159
|
- test/unit/active_model/array_serializer/root_test.rb
|
159
160
|
- test/unit/active_model/array_serializer/scope_test.rb
|
160
161
|
- test/unit/active_model/array_serializer/serialization_test.rb
|
@@ -175,3 +176,4 @@ test_files:
|
|
175
176
|
- test/unit/active_model/serializer/root_test.rb
|
176
177
|
- test/unit/active_model/serializer/scope_test.rb
|
177
178
|
- test/unit/active_model/serializer/url_helpers_test.rb
|
179
|
+
has_rdoc:
|