active_model_serializers 0.9.0.alpha1 → 0.9.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 +132 -15
- data/lib/action_controller/serialization.rb +5 -0
- data/lib/action_controller/serialization_test_case.rb +79 -0
- data/lib/active_model/array_serializer.rb +15 -8
- data/lib/active_model/default_serializer.rb +12 -2
- data/lib/active_model/serializable.rb +21 -6
- data/lib/active_model/serializer.rb +84 -9
- data/lib/active_model/serializer/associations.rb +38 -11
- data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +6 -2
- data/test/fixtures/poro.rb +11 -0
- data/test/integration/action_controller/serialization_test.rb +53 -0
- data/test/integration/action_controller/serialization_test_case_test.rb +61 -0
- data/test/integration/active_record/active_record_test.rb +1 -1
- data/test/integration/generators/scaffold_controller_generator_test.rb +0 -3
- data/test/test_helper.rb +4 -1
- data/test/unit/active_model/array_serializer/except_test.rb +18 -0
- data/test/unit/active_model/array_serializer/key_format_test.rb +18 -0
- data/test/unit/active_model/array_serializer/meta_test.rb +1 -1
- data/test/unit/active_model/array_serializer/only_test.rb +18 -0
- data/test/unit/active_model/array_serializer/root_test.rb +2 -2
- data/test/unit/active_model/array_serializer/scope_test.rb +1 -1
- data/test/unit/active_model/array_serializer/serialization_test.rb +120 -4
- data/test/unit/active_model/default_serializer_test.rb +1 -1
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +1 -1
- data/test/unit/active_model/serializer/associations_test.rb +1 -1
- data/test/unit/active_model/serializer/attributes_test.rb +1 -1
- data/test/unit/active_model/serializer/config_test.rb +7 -5
- data/test/unit/active_model/serializer/filter_test.rb +22 -2
- data/test/unit/active_model/serializer/has_many_test.rb +61 -4
- data/test/unit/active_model/serializer/has_one_test.rb +59 -3
- data/test/unit/active_model/serializer/key_format_test.rb +25 -0
- data/test/unit/active_model/serializer/meta_test.rb +1 -1
- data/test/unit/active_model/serializer/options_test.rb +15 -0
- data/test/unit/active_model/serializer/root_test.rb +2 -2
- data/test/unit/active_model/serializer/scope_test.rb +3 -3
- metadata +36 -27
- data/test/coverage_setup.rb +0 -15
- data/test/tmp/app/serializers/account_serializer.rb +0 -3
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'active_model/default_serializer'
|
2
|
-
require 'active_model/serializer'
|
3
2
|
|
4
3
|
module ActiveModel
|
5
4
|
class Serializer
|
@@ -15,27 +14,40 @@ module ActiveModel
|
|
15
14
|
@options = options
|
16
15
|
self.embed = options.fetch(:embed) { CONFIG.embed }
|
17
16
|
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } }
|
17
|
+
@key_format = options.fetch(:key_format) { CONFIG.key_format }
|
18
18
|
@embed_key = options[:embed_key] || :id
|
19
19
|
@key = options[:key]
|
20
20
|
@embedded_key = options[:root] || name
|
21
|
+
@embed_in_root_key = options.fetch(:embed_in_root_key) { CONFIG.embed_in_root_key }
|
22
|
+
@embed_namespace = options.fetch(:embed_namespace) { CONFIG.embed_namespace }
|
21
23
|
|
22
24
|
serializer = @options[:serializer]
|
23
|
-
@
|
25
|
+
@serializer_from_options = serializer.is_a?(String) ? serializer.constantize : serializer
|
24
26
|
end
|
25
27
|
|
26
28
|
attr_reader :name, :embed_ids, :embed_objects
|
27
|
-
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :root_key, :
|
29
|
+
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :root_key, :serializer_from_options, :options, :key_format, :embed_in_root_key, :embed_namespace
|
28
30
|
alias embed_ids? embed_ids
|
29
31
|
alias embed_objects? embed_objects
|
30
32
|
alias embed_in_root? embed_in_root
|
33
|
+
alias embed_in_root_key? embed_in_root_key
|
34
|
+
alias embed_namespace? embed_namespace
|
31
35
|
|
32
36
|
def embed=(embed)
|
33
37
|
@embed_ids = embed == :id || embed == :ids
|
34
38
|
@embed_objects = embed == :object || embed == :objects
|
35
39
|
end
|
36
40
|
|
41
|
+
def serializer_from_object(object)
|
42
|
+
Serializer.serializer_for(object)
|
43
|
+
end
|
44
|
+
|
45
|
+
def default_serializer
|
46
|
+
DefaultSerializer
|
47
|
+
end
|
48
|
+
|
37
49
|
def build_serializer(object, options = {})
|
38
|
-
|
50
|
+
serializer_class(object).new(object, options.merge(self.options))
|
39
51
|
end
|
40
52
|
|
41
53
|
class HasOne < Association
|
@@ -45,8 +57,11 @@ module ActiveModel
|
|
45
57
|
@key ||= "#{name}_id"
|
46
58
|
end
|
47
59
|
|
60
|
+
def serializer_class(object)
|
61
|
+
serializer_from_options || serializer_from_object(object) || default_serializer
|
62
|
+
end
|
63
|
+
|
48
64
|
def build_serializer(object, options = {})
|
49
|
-
@serializer_class ||= Serializer.serializer_for(object) || DefaultSerializer
|
50
65
|
options[:_wrap_in_array] = embed_in_root?
|
51
66
|
super
|
52
67
|
end
|
@@ -59,15 +74,27 @@ module ActiveModel
|
|
59
74
|
@key ||= "#{name.to_s.singularize}_ids"
|
60
75
|
end
|
61
76
|
|
62
|
-
def
|
63
|
-
if
|
64
|
-
|
65
|
-
@serializer_class = ArraySerializer
|
77
|
+
def serializer_class(object)
|
78
|
+
if use_array_serializer?
|
79
|
+
ArraySerializer
|
66
80
|
else
|
67
|
-
|
81
|
+
serializer_from_options
|
68
82
|
end
|
83
|
+
end
|
69
84
|
|
70
|
-
|
85
|
+
def options
|
86
|
+
if use_array_serializer?
|
87
|
+
{ each_serializer: serializer_from_options }.merge! super
|
88
|
+
else
|
89
|
+
super
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def use_array_serializer?
|
96
|
+
!serializer_from_options ||
|
97
|
+
serializer_from_options && !(serializer_from_options <= ArraySerializer)
|
71
98
|
end
|
72
99
|
end
|
73
100
|
end
|
@@ -26,7 +26,7 @@ module Rails
|
|
26
26
|
if options[:parent]
|
27
27
|
options[:parent]
|
28
28
|
elsif (ns = Rails::Generators.namespace) && ns.const_defined?(:ApplicationSerializer) ||
|
29
|
-
|
29
|
+
(Object.const_get(:ApplicationSerializer) rescue nil)
|
30
30
|
'ApplicationSerializer'
|
31
31
|
else
|
32
32
|
'ActiveModel::Serializer'
|
@@ -7,9 +7,13 @@ require 'active_model/serializer/railtie' if defined?(Rails)
|
|
7
7
|
begin
|
8
8
|
require 'action_controller'
|
9
9
|
require 'action_controller/serialization'
|
10
|
+
require 'action_controller/serialization_test_case'
|
10
11
|
|
11
|
-
ActiveSupport.on_load(:
|
12
|
-
|
12
|
+
ActiveSupport.on_load(:after_initialize) do
|
13
|
+
if ::ActionController::Serialization.enabled
|
14
|
+
ActionController::Base.send(:include, ::ActionController::Serialization)
|
15
|
+
ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
|
16
|
+
end
|
13
17
|
end
|
14
18
|
rescue LoadError
|
15
19
|
# rails not installed, continuing
|
data/test/fixtures/poro.rb
CHANGED
@@ -35,6 +35,9 @@ end
|
|
35
35
|
class Comment < Model
|
36
36
|
end
|
37
37
|
|
38
|
+
class WebLog < Model
|
39
|
+
end
|
40
|
+
|
38
41
|
###
|
39
42
|
## Serializers
|
40
43
|
###
|
@@ -62,3 +65,11 @@ end
|
|
62
65
|
class CommentSerializer < ActiveModel::Serializer
|
63
66
|
attributes :content
|
64
67
|
end
|
68
|
+
|
69
|
+
class WebLogSerializer < ActiveModel::Serializer
|
70
|
+
attributes :name, :display_name
|
71
|
+
end
|
72
|
+
|
73
|
+
class WebLogLowerCamelSerializer < WebLogSerializer
|
74
|
+
format_keys :lower_camel
|
75
|
+
end
|
@@ -194,6 +194,59 @@ module ActionController
|
|
194
194
|
end
|
195
195
|
end
|
196
196
|
|
197
|
+
class LowerCamelArraySerializerTest < ActionController::TestCase
|
198
|
+
class WebLogController < ActionController::Base
|
199
|
+
def render_array
|
200
|
+
render json: [WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}), WebLog.new({name: 'Name 2', display_name: 'Display Name 2'})], each_serializer: WebLogLowerCamelSerializer
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
tests WebLogController
|
205
|
+
|
206
|
+
def test_render_array
|
207
|
+
get :render_array
|
208
|
+
assert_equal 'application/json', @response.content_type
|
209
|
+
assert_equal '{"webLog":[{"name":"Name 1","displayName":"Display Name 1"},{"name":"Name 2","displayName":"Display Name 2"}]}', @response.body
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
class LowerCamelWoRootSerializerTest < ActionController::TestCase
|
214
|
+
class WebLogController < ActionController::Base
|
215
|
+
def render_without_root
|
216
|
+
render json: WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}),
|
217
|
+
root: false,
|
218
|
+
serializer: WebLogLowerCamelSerializer
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
tests WebLogController
|
223
|
+
|
224
|
+
def test_render_without_root
|
225
|
+
get :render_without_root
|
226
|
+
assert_equal 'application/json', @response.content_type
|
227
|
+
assert_equal '{"name":"Name 1","displayName":"Display Name 1"}', @response.body
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
class LowerCamelArrayWoRootSerializerTest < ActionController::TestCase
|
232
|
+
class WebLogController < ActionController::Base
|
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,
|
237
|
+
each_serializer: WebLogLowerCamelSerializer
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
tests WebLogController
|
242
|
+
|
243
|
+
def test_render_array_without_root
|
244
|
+
get :render_array_without_root
|
245
|
+
assert_equal 'application/json', @response.content_type
|
246
|
+
assert_equal '[{"name":"Name 1","displayName":"Display Name 1"},{"name":"Name 2","displayName":"Display Name 2"}]', @response.body
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
197
250
|
class ArrayEmbedingSerializerTest < ActionController::TestCase
|
198
251
|
def setup
|
199
252
|
super
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module SerializationsAssertions
|
5
|
+
class RenderSerializerTest < ActionController::TestCase
|
6
|
+
class MyController < ActionController::Base
|
7
|
+
def render_using_serializer
|
8
|
+
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
end
|
10
|
+
|
11
|
+
def render_text
|
12
|
+
render text: 'ok'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
tests MyController
|
17
|
+
|
18
|
+
def test_supports_specifying_serializers_with_a_serializer_class
|
19
|
+
get :render_using_serializer
|
20
|
+
assert_serializer ProfileSerializer
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_supports_specifying_serializers_with_a_regexp
|
24
|
+
get :render_using_serializer
|
25
|
+
assert_serializer %r{\AProfile.+\Z}
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_supports_specifying_serializers_with_a_string
|
29
|
+
get :render_using_serializer
|
30
|
+
assert_serializer 'ProfileSerializer'
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_supports_specifying_serializers_with_a_symbol
|
34
|
+
get :render_using_serializer
|
35
|
+
assert_serializer :profile_serializer
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_supports_specifying_serializers_with_a_nil
|
39
|
+
get :render_text
|
40
|
+
assert_serializer nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_raises_descriptive_error_message_when_serializer_was_not_rendered
|
44
|
+
get :render_using_serializer
|
45
|
+
e = assert_raise ActiveSupport::TestCase::Assertion do
|
46
|
+
assert_serializer 'PostSerializer'
|
47
|
+
end
|
48
|
+
assert_match 'expecting <"PostSerializer"> but rendering with <["ProfileSerializer"]>', e.message
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def test_raises_argument_error_when_asserting_with_invalid_object
|
53
|
+
get :render_using_serializer
|
54
|
+
e = assert_raise ArgumentError do
|
55
|
+
assert_serializer Hash
|
56
|
+
end
|
57
|
+
assert_match 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil', e.message
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -16,7 +16,6 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|
16
16
|
run_generator
|
17
17
|
|
18
18
|
assert_file 'app/controllers/accounts_controller.rb' do |content|
|
19
|
-
|
20
19
|
assert_instance_method :index, content do |m|
|
21
20
|
assert_match /@accounts = Account\.all/, m
|
22
21
|
assert_match /format.html/, m
|
@@ -60,8 +59,6 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
|
|
60
59
|
|
61
60
|
assert_match(/def account_params/, content)
|
62
61
|
assert_match(/params\.require\(:account\)\.permit\(:name, :description, :business_id\)/, content)
|
63
|
-
|
64
62
|
end
|
65
|
-
|
66
63
|
end
|
67
64
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'bundler/setup'
|
2
|
-
require 'coverage_setup'
|
3
2
|
require 'minitest/autorun'
|
4
3
|
require 'active_model_serializers'
|
5
4
|
require 'fixtures/poro'
|
6
5
|
|
6
|
+
# Ensure backward compatibility with Minitest 4
|
7
|
+
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
|
8
|
+
|
7
9
|
module TestHelper
|
8
10
|
Routes = ActionDispatch::Routing::RouteSet.new
|
9
11
|
Routes.draw do
|
@@ -12,6 +14,7 @@ module TestHelper
|
|
12
14
|
end
|
13
15
|
|
14
16
|
ActionController::Base.send :include, Routes.url_helpers
|
17
|
+
ActionController::Base.send :include, ActionController::Serialization
|
15
18
|
end
|
16
19
|
|
17
20
|
ActionController::TestCase.class_eval do
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class ExceptTest < Minitest::Test
|
6
|
+
def test_array_serializer_pass_except_to_items_serializers
|
7
|
+
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
8
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
9
|
+
serializer = ArraySerializer.new(array, except: [:description])
|
10
|
+
|
11
|
+
expected = [{ name: 'Name 1' },
|
12
|
+
{ name: 'Name 2' }]
|
13
|
+
|
14
|
+
assert_equal expected, serializer.serializable_array
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class KeyFormatTest < Minitest::Test
|
6
|
+
def test_array_serializer_pass_options_to_items_serializers
|
7
|
+
array = [WebLog.new({ name: 'Name 1', display_name: 'Display Name 1'}),
|
8
|
+
WebLog.new({ name: 'Name 2', display_name: 'Display Name 2'})]
|
9
|
+
serializer = ArraySerializer.new(array, key_format: :lower_camel)
|
10
|
+
|
11
|
+
expected = [{ name: 'Name 1', displayName: 'Display Name 1' },
|
12
|
+
{ name: 'Name 2', displayName: 'Display Name 2' }]
|
13
|
+
|
14
|
+
assert_equal expected, serializer.serializable_array
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -3,7 +3,7 @@ require 'active_model/serializer'
|
|
3
3
|
|
4
4
|
module ActiveModel
|
5
5
|
class ArraySerializer
|
6
|
-
class MetaTest <
|
6
|
+
class MetaTest < Minitest::Test
|
7
7
|
def setup
|
8
8
|
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
9
|
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class OnlyTest < Minitest::Test
|
6
|
+
def test_array_serializer_pass_only_to_items_serializers
|
7
|
+
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
8
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
9
|
+
serializer = ArraySerializer.new(array, only: [:name])
|
10
|
+
|
11
|
+
expected = [{ name: 'Name 1' },
|
12
|
+
{ name: 'Name 2' }]
|
13
|
+
|
14
|
+
assert_equal expected, serializer.serializable_array
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class ArraySerializer
|
5
|
-
class RootAsOptionTest <
|
5
|
+
class RootAsOptionTest < Minitest::Test
|
6
6
|
def setup
|
7
7
|
@old_root = ArraySerializer._root
|
8
8
|
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@@ -50,7 +50,7 @@ module ActiveModel
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
class RootInSerializerTest <
|
53
|
+
class RootInSerializerTest < Minitest::Test
|
54
54
|
def setup
|
55
55
|
@old_root = ArraySerializer._root
|
56
56
|
ArraySerializer._root = :in_serializer
|
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class ArraySerializer
|
5
|
-
class ScopeTest <
|
5
|
+
class ScopeTest < Minitest::Test
|
6
6
|
def test_array_serializer_pass_options_to_items_serializers
|
7
7
|
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
8
8
|
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
@@ -2,14 +2,14 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class ArraySerializer
|
5
|
-
class BasicObjectsSerializationTest <
|
5
|
+
class BasicObjectsSerializationTest < Minitest::Test
|
6
6
|
def setup
|
7
7
|
array = [1, 2, 3]
|
8
|
-
@serializer =
|
8
|
+
@serializer = Serializer.serializer_for(array).new(array)
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_serializer_for_array_returns_appropriate_type
|
12
|
-
assert_kind_of ArraySerializer, @serializer
|
12
|
+
assert_kind_of ActiveModel::ArraySerializer, @serializer
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_array_serializer_serializes_simple_objects
|
@@ -18,7 +18,24 @@ module ActiveModel
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
class
|
21
|
+
class CustomArraySerializerSupport < Minitest::Test
|
22
|
+
def setup
|
23
|
+
Object.const_set(:ArraySerializer, Class.new)
|
24
|
+
|
25
|
+
array = [1, 2, 3]
|
26
|
+
@serializer_class = Serializer.serializer_for(array)
|
27
|
+
end
|
28
|
+
|
29
|
+
def teardown
|
30
|
+
Object.send(:remove_const, :ArraySerializer)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_serializer_for_array_returns_appropriate_type
|
34
|
+
assert_equal ::ArraySerializer, @serializer_class
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class ModelSerializationTest < Minitest::Test
|
22
39
|
def test_array_serializer_serializes_models
|
23
40
|
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
24
41
|
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
@@ -78,6 +95,105 @@ module ActiveModel
|
|
78
95
|
ensure
|
79
96
|
PostSerializer._associations[:comments] = @old_association
|
80
97
|
end
|
98
|
+
|
99
|
+
def test_embed_object_for_has_one_association_with_nil_value
|
100
|
+
@association = UserSerializer._associations[:profile]
|
101
|
+
@old_association = @association.dup
|
102
|
+
|
103
|
+
@association.embed = :objects
|
104
|
+
|
105
|
+
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
106
|
+
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
107
|
+
|
108
|
+
class << @user1
|
109
|
+
def profile
|
110
|
+
nil
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class << @user2
|
115
|
+
def profile
|
116
|
+
@profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
@serializer = ArraySerializer.new([@user1, @user2]) #, root: :posts)
|
121
|
+
assert_equal([
|
122
|
+
{ name: "User 1", email: "email1@server.com", profile: nil },
|
123
|
+
{ name: "User 2", email: "email2@server.com", profile: { name: 'Name 1', description: 'Desc 1' } }
|
124
|
+
], @serializer.as_json)
|
125
|
+
ensure
|
126
|
+
UserSerializer._associations[:profile] = @old_association
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_embed_object_in_root_for_has_one_association_with_nil_value
|
130
|
+
@association = UserSerializer._associations[:profile]
|
131
|
+
@old_association = @association.dup
|
132
|
+
|
133
|
+
@association.embed = :ids
|
134
|
+
@association.embed_in_root = true
|
135
|
+
|
136
|
+
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
137
|
+
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
138
|
+
|
139
|
+
class << @user1
|
140
|
+
def profile
|
141
|
+
nil
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
class << @user2
|
146
|
+
def profile
|
147
|
+
@profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
@serializer = ArraySerializer.new([@user1, @user2], root: :users)
|
152
|
+
assert_equal({
|
153
|
+
users: [
|
154
|
+
{ name: "User 1", email: "email1@server.com", 'profile_id' => nil },
|
155
|
+
{ name: "User 2", email: "email2@server.com", 'profile_id' => @user2.profile.object_id }
|
156
|
+
],
|
157
|
+
'profiles' => [
|
158
|
+
{ name: 'Name 1', description: 'Desc 1' }
|
159
|
+
]
|
160
|
+
}, @serializer.as_json)
|
161
|
+
ensure
|
162
|
+
UserSerializer._associations[:profile] = @old_association
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_embed_object_in_root_for_has_one_association_with_all_nil_values
|
166
|
+
@association = UserSerializer._associations[:profile]
|
167
|
+
@old_association = @association.dup
|
168
|
+
|
169
|
+
@association.embed = :ids
|
170
|
+
@association.embed_in_root = true
|
171
|
+
|
172
|
+
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
173
|
+
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
174
|
+
|
175
|
+
class << @user1
|
176
|
+
def profile
|
177
|
+
nil
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
class << @user2
|
182
|
+
def profile
|
183
|
+
nil
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
@serializer = ArraySerializer.new([@user1, @user2], root: :users)
|
188
|
+
assert_equal({
|
189
|
+
users: [
|
190
|
+
{ name: "User 1", email: "email1@server.com", 'profile_id' => nil },
|
191
|
+
{ name: "User 2", email: "email2@server.com", 'profile_id' => nil }
|
192
|
+
]
|
193
|
+
}, @serializer.as_json)
|
194
|
+
ensure
|
195
|
+
UserSerializer._associations[:profile] = @old_association
|
196
|
+
end
|
81
197
|
end
|
82
198
|
end
|
83
199
|
end
|