active_model_serializers 0.9.13 → 0.10.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +21 -0
  3. data/.travis.yml +27 -0
  4. data/CHANGELOG.md +8 -236
  5. data/CONTRIBUTING.md +23 -12
  6. data/Gemfile +17 -0
  7. data/{MIT-LICENSE → LICENSE.txt} +3 -2
  8. data/README.md +151 -781
  9. data/Rakefile +12 -0
  10. data/active_model_serializers.gemspec +26 -0
  11. data/lib/action_controller/serialization.rb +30 -84
  12. data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
  13. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
  14. data/lib/active_model/serializer/adapter/json.rb +52 -0
  15. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
  16. data/lib/active_model/serializer/adapter/json_api.rb +152 -0
  17. data/lib/active_model/serializer/adapter/null.rb +11 -0
  18. data/lib/active_model/serializer/adapter.rb +87 -0
  19. data/lib/active_model/serializer/array_serializer.rb +32 -0
  20. data/lib/active_model/serializer/configuration.rb +13 -0
  21. data/lib/active_model/serializer/fieldset.rb +40 -0
  22. data/lib/active_model/serializer/version.rb +1 -3
  23. data/lib/active_model/serializer.rb +193 -276
  24. data/lib/active_model_serializers.rb +5 -19
  25. data/lib/generators/serializer/USAGE +6 -0
  26. data/lib/{active_model/serializer/generators → generators}/serializer/serializer_generator.rb +8 -10
  27. data/lib/{active_model/serializer/generators → generators}/serializer/templates/serializer.rb +2 -2
  28. data/test/action_controller/adapter_selector_test.rb +51 -0
  29. data/test/action_controller/explicit_serializer_test.rb +110 -0
  30. data/test/action_controller/json_api_linked_test.rb +173 -0
  31. data/test/action_controller/serialization_scope_name_test.rb +63 -0
  32. data/test/action_controller/serialization_test.rb +365 -0
  33. data/test/adapter/fragment_cache_test.rb +27 -0
  34. data/test/adapter/json/belongs_to_test.rb +41 -0
  35. data/test/adapter/json/collection_test.rb +59 -0
  36. data/test/adapter/json/has_many_test.rb +36 -0
  37. data/test/adapter/json_api/belongs_to_test.rb +147 -0
  38. data/test/adapter/json_api/collection_test.rb +89 -0
  39. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  40. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  41. data/test/adapter/json_api/has_many_test.rb +106 -0
  42. data/test/adapter/json_api/has_one_test.rb +59 -0
  43. data/test/adapter/json_api/linked_test.rb +257 -0
  44. data/test/adapter/json_test.rb +34 -0
  45. data/test/adapter/null_test.rb +25 -0
  46. data/test/adapter_test.rb +43 -0
  47. data/test/array_serializer_test.rb +29 -0
  48. data/test/fixtures/poro.rb +123 -172
  49. data/test/serializers/adapter_for_test.rb +50 -0
  50. data/test/serializers/associations_test.rb +106 -0
  51. data/test/serializers/attribute_test.rb +23 -0
  52. data/test/serializers/attributes_test.rb +28 -0
  53. data/test/serializers/cache_test.rb +128 -0
  54. data/test/serializers/configuration_test.rb +15 -0
  55. data/test/serializers/fieldset_test.rb +26 -0
  56. data/test/serializers/generators_test.rb +59 -0
  57. data/test/serializers/meta_test.rb +78 -0
  58. data/test/serializers/options_test.rb +21 -0
  59. data/test/serializers/serializer_for_test.rb +56 -0
  60. data/test/serializers/urls_test.rb +26 -0
  61. data/test/test_helper.rb +20 -10
  62. metadata +121 -148
  63. data/DESIGN.textile +0 -586
  64. data/lib/action_controller/serialization_test_case.rb +0 -82
  65. data/lib/active_model/array_serializer.rb +0 -70
  66. data/lib/active_model/default_serializer.rb +0 -30
  67. data/lib/active_model/serializable/utils.rb +0 -18
  68. data/lib/active_model/serializable.rb +0 -61
  69. data/lib/active_model/serializer/association/has_many.rb +0 -41
  70. data/lib/active_model/serializer/association/has_one.rb +0 -27
  71. data/lib/active_model/serializer/association.rb +0 -58
  72. data/lib/active_model/serializer/config.rb +0 -33
  73. data/lib/active_model/serializer/generators/resource_override.rb +0 -15
  74. data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
  75. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -16
  76. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
  77. data/lib/active_model/serializer/railtie.rb +0 -24
  78. data/lib/active_model/serializer_support.rb +0 -7
  79. data/test/benchmark/app.rb +0 -60
  80. data/test/benchmark/benchmarking_support.rb +0 -67
  81. data/test/benchmark/bm_active_record.rb +0 -41
  82. data/test/benchmark/setup.rb +0 -75
  83. data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
  84. data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
  85. data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
  86. data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
  87. data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
  88. data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
  89. data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
  90. data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
  91. data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
  92. data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
  93. data/test/fixtures/active_record.rb +0 -96
  94. data/test/fixtures/template.html.erb +0 -1
  95. data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
  96. data/test/integration/action_controller/serialization_test.rb +0 -287
  97. data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
  98. data/test/integration/active_record/active_record_test.rb +0 -94
  99. data/test/integration/generators/resource_generator_test.rb +0 -26
  100. data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
  101. data/test/integration/generators/serializer_generator_test.rb +0 -41
  102. data/test/test_app.rb +0 -18
  103. data/test/tmp/app/serializers/account_serializer.rb +0 -3
  104. data/test/unit/active_model/array_serializer/except_test.rb +0 -18
  105. data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
  106. data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
  107. data/test/unit/active_model/array_serializer/only_test.rb +0 -18
  108. data/test/unit/active_model/array_serializer/options_test.rb +0 -16
  109. data/test/unit/active_model/array_serializer/root_test.rb +0 -102
  110. data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
  111. data/test/unit/active_model/array_serializer/serialization_test.rb +0 -239
  112. data/test/unit/active_model/default_serializer_test.rb +0 -13
  113. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
  114. data/test/unit/active_model/serializer/associations_test.rb +0 -49
  115. data/test/unit/active_model/serializer/attributes_test.rb +0 -57
  116. data/test/unit/active_model/serializer/config_test.rb +0 -91
  117. data/test/unit/active_model/serializer/filter_test.rb +0 -69
  118. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
  119. data/test/unit/active_model/serializer/has_many_test.rb +0 -265
  120. data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
  121. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
  122. data/test/unit/active_model/serializer/has_one_test.rb +0 -253
  123. data/test/unit/active_model/serializer/key_format_test.rb +0 -25
  124. data/test/unit/active_model/serializer/meta_test.rb +0 -39
  125. data/test/unit/active_model/serializer/options_test.rb +0 -42
  126. data/test/unit/active_model/serializer/root_test.rb +0 -117
  127. data/test/unit/active_model/serializer/scope_test.rb +0 -49
  128. data/test/unit/active_model/serializer/url_helpers_test.rb +0 -36
  129. data/test/unit/active_model/serilizable_test.rb +0 -50
@@ -1,94 +0,0 @@
1
- require 'test_helper'
2
- require 'fixtures/active_record'
3
-
4
- module ActiveModel
5
- class Serializer
6
- class ActiveRecordTest < Minitest::Test
7
- def setup
8
- @post = ARPost.first
9
- end
10
-
11
- def test_serialization_embedding_objects
12
- post_serializer = ARPostSerializer.new(@post)
13
-
14
- assert_equal({
15
- 'ar_post' => {
16
- title: 'New post', body: 'A body!!!',
17
- ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'happy' }, { name: 'whiny' }] },
18
- { body: 'i liked it', ar_tags: [{:name=>"happy"}, {:name=>"short"}] }],
19
- ar_tags: [{ name: 'short' }, { name: 'whiny' }],
20
- ar_section: { 'name' => 'ruby' }
21
- }
22
- }, post_serializer.as_json)
23
- end
24
-
25
- def test_serialization_embedding_ids
26
- post_serializer = ARPostSerializer.new(@post)
27
-
28
- embed(ARPostSerializer, embed: :ids) do
29
- assert_equal({
30
- 'ar_post' => {
31
- title: 'New post', body: 'A body!!!',
32
- 'ar_comment_ids' => [1, 2],
33
- 'ar_tag_ids' => [1, 2],
34
- 'ar_section_id' => 1
35
- }
36
- }, post_serializer.as_json)
37
- end
38
- end
39
-
40
- def test_serialization_embedding_ids_including_in_root
41
- post_serializer = ARPostSerializer.new(@post)
42
-
43
- embed(ARPostSerializer, embed: :ids, embed_in_root: true) do
44
- embed(ARCommentSerializer, embed: :ids, embed_in_root: true) do
45
- assert_equal({
46
- 'ar_post' => {
47
- title: 'New post', body: 'A body!!!',
48
- 'ar_comment_ids' => [1, 2],
49
- 'ar_tag_ids' => [1, 2],
50
- 'ar_section_id' => 1
51
- },
52
- 'ar_comments' => [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
53
- { body: 'i liked it', 'ar_tag_ids' => [3, 1] }],
54
- 'ar_tags' => [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }],
55
- 'ar_sections' => [{ 'name' => 'ruby' }]
56
- }, post_serializer.as_json)
57
- end
58
- end
59
- end
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
-
77
- private
78
-
79
- def embed(serializer_class, options = {})
80
- old_assocs = Hash[serializer_class._associations.to_a.map { |(name, association)| [name, association.dup] }]
81
-
82
- serializer_class._associations.each_value do |association|
83
- association.embed = options[:embed]
84
- association.embed_in_root = options[:embed_in_root]
85
- association.embed_in_root_key = options[:embed_in_root_key]
86
- end
87
-
88
- yield
89
- ensure
90
- serializer_class._associations = old_assocs
91
- end
92
- end
93
- end
94
- end
@@ -1,26 +0,0 @@
1
- require 'test_helper'
2
- require 'rails'
3
- require 'active_model/serializer/railtie'
4
- require 'test_app'
5
-
6
- class ResourceGeneratorTest < Rails::Generators::TestCase
7
- destination File.expand_path('../../../tmp', __FILE__)
8
- setup :prepare_destination, :copy_routes
9
-
10
- tests Rails::Generators::ResourceGenerator
11
- arguments %w(account)
12
-
13
- def test_serializer_file_is_generated
14
- run_generator
15
-
16
- assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/
17
- end
18
-
19
- private
20
-
21
- def copy_routes
22
- config_dir = File.join(destination_root, 'config')
23
- FileUtils.mkdir_p(config_dir)
24
- File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw { }')
25
- end
26
- end
@@ -1,64 +0,0 @@
1
- require 'test_helper'
2
- require 'rails'
3
- require 'active_model/serializer/railtie'
4
- require 'test_app'
5
-
6
- class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
7
- destination File.expand_path('../../../tmp', __FILE__)
8
- setup :prepare_destination
9
-
10
- tests Rails::Generators::ScaffoldControllerGenerator
11
- arguments %w(account name:string description:text business:references)
12
-
13
- def test_generated_controller
14
- return true if Rails::VERSION::MAJOR < 4
15
-
16
- run_generator
17
-
18
- assert_file 'app/controllers/accounts_controller.rb' do |content|
19
- assert_instance_method :index, content do |m|
20
- assert_match(/@accounts = Account\.all/, m)
21
- assert_match(/format.html/, m)
22
- assert_match(/format.json \{ render json: @accounts \}/, m)
23
- end
24
-
25
- assert_instance_method :show, content do |m|
26
- assert_match(/format.html/, m)
27
- assert_match(/format.json \{ render json: @account \}/, m)
28
- end
29
-
30
- assert_instance_method :new, content do |m|
31
- assert_match(/@account = Account\.new/, m)
32
- end
33
-
34
- assert_instance_method :edit, content do |m|
35
- assert_predicate m, :blank?
36
- end
37
-
38
- assert_instance_method :create, content do |m|
39
- assert_match(/@account = Account\.new\(account_params\)/, m)
40
- assert_match(/@account\.save/, m)
41
- assert_match(/format\.html \{ redirect_to @account, notice: 'Account was successfully created\.' \}/, m)
42
- assert_match(/format\.json \{ render json: @account, status: :created \}/, m)
43
- assert_match(/format\.html \{ render action: 'new' \}/, m)
44
- assert_match(/format\.json \{ render json: @account\.errors, status: :unprocessable_entity \}/, m)
45
- end
46
-
47
- assert_instance_method :update, content do |m|
48
- assert_match(/format\.html \{ redirect_to @account, notice: 'Account was successfully updated\.' \}/, m)
49
- assert_match(/format\.json \{ head :no_content \}/, m)
50
- assert_match(/format\.html \{ render action: 'edit' \}/, m)
51
- assert_match(/format\.json \{ render json: @account.errors, status: :unprocessable_entity \}/, m)
52
- end
53
-
54
- assert_instance_method :destroy, content do |m|
55
- assert_match(/@account\.destroy/, m)
56
- assert_match(/format\.html { redirect_to accounts_url \}/, m)
57
- assert_match(/format\.json \{ head :no_content \}/, m)
58
- end
59
-
60
- assert_match(/def account_params/, content)
61
- assert_match(/params\.require\(:account\)\.permit\(:name, :description, :business_id\)/, content)
62
- end
63
- end
64
- end
@@ -1,41 +0,0 @@
1
- require 'test_helper'
2
- require 'rails'
3
- require 'active_model/serializer/railtie'
4
- require 'test_app'
5
-
6
- class SerializerGeneratorTest < Rails::Generators::TestCase
7
- destination File.expand_path('../../../tmp', __FILE__)
8
- setup :prepare_destination
9
-
10
- tests Rails::Generators::SerializerGenerator
11
- arguments %w(account name:string description:text business:references)
12
-
13
- def test_generates_a_serializer_with_attributes_and_associations
14
- run_generator
15
- assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/ do |serializer|
16
- assert_match(/attributes :id, :name, :description/, serializer)
17
- assert_match(/has_one :business/, serializer)
18
- end
19
- end
20
-
21
- def test_generates_a_namespaced_serializer
22
- run_generator ['admin/account']
23
- assert_file 'app/serializers/admin/account_serializer.rb', /class Admin::AccountSerializer < ActiveModel::Serializer/
24
- end
25
-
26
- def test_uses_application_serializer_if_one_exists
27
- Object.const_set(:ApplicationSerializer, Class.new)
28
- run_generator
29
- assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
30
- ensure
31
- Object.send :remove_const, :ApplicationSerializer
32
- end
33
-
34
- def test_uses_given_parent
35
- Object.const_set(:ApplicationSerializer, Class.new)
36
- run_generator ['Account', '--parent=MySerializer']
37
- assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < MySerializer/
38
- ensure
39
- Object.send :remove_const, :ApplicationSerializer
40
- end
41
- end
data/test/test_app.rb DELETED
@@ -1,18 +0,0 @@
1
- class TestApp < Rails::Application
2
- if config.respond_to?(:load_defaults)
3
- config.load_defaults("#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}")
4
- end
5
-
6
- if Rails.version.to_s.first >= '4'
7
- config.eager_load = false
8
- config.secret_key_base = 'abc123'
9
- end
10
- config.after_initialize do
11
- Rails.application.routes.default_url_options = { host: 'http://example.com' }
12
- end
13
-
14
- # Set up a logger to avoid creating a log directory on every run.
15
- config.logger = Logger.new(nil)
16
- end
17
-
18
- TestApp.initialize!
@@ -1,3 +0,0 @@
1
- class AccountSerializer < MySerializer
2
- attributes :id
3
- end
@@ -1,18 +0,0 @@
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
@@ -1,18 +0,0 @@
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
@@ -1,53 +0,0 @@
1
- require 'test_helper'
2
- require 'active_model/serializer'
3
-
4
- module ActiveModel
5
- class ArraySerializer
6
- class MetaTest < Minitest::Test
7
- def setup
8
- @profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
- @profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
10
- @serializer = ArraySerializer.new([@profile1, @profile2], root: 'profiles')
11
- end
12
-
13
- def test_meta
14
- @serializer.meta = { total: 10 }
15
-
16
- assert_equal({
17
- 'profiles' => [
18
- {
19
- name: 'Name 1',
20
- description: 'Description 1'
21
- }, {
22
- name: 'Name 2',
23
- description: 'Description 2'
24
- }
25
- ],
26
- meta: {
27
- total: 10
28
- }
29
- }, @serializer.as_json)
30
- end
31
-
32
- def test_meta_using_meta_key
33
- @serializer.meta_key = :my_meta
34
- @serializer.meta = { total: 10 }
35
-
36
- assert_equal({
37
- 'profiles' => [
38
- {
39
- name: 'Name 1',
40
- description: 'Description 1'
41
- }, {
42
- name: 'Name 2',
43
- description: 'Description 2'
44
- }
45
- ],
46
- my_meta: {
47
- total: 10
48
- }
49
- }, @serializer.as_json)
50
- end
51
- end
52
- end
53
- end
@@ -1,18 +0,0 @@
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
@@ -1,16 +0,0 @@
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
@@ -1,102 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class ArraySerializer
5
- class RootAsOptionTest < Minitest::Test
6
- def setup
7
- @old_root = ArraySerializer._root
8
- @profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
- @profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
10
- @serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
11
- end
12
-
13
- def teardown
14
- ArraySerializer._root = @old_root
15
- end
16
-
17
- def test_root_is_not_displayed_using_serializable_array
18
- assert_equal([
19
- { name: 'Name 1', description: 'Description 1' },
20
- { name: 'Name 2', description: 'Description 2' }
21
- ], @serializer.serializable_array)
22
- end
23
-
24
- def test_root_using_as_json
25
- assert_equal({
26
- initialize: [
27
- { name: 'Name 1', description: 'Description 1' },
28
- { name: 'Name 2', description: 'Description 2' }
29
- ]
30
- }, @serializer.as_json)
31
- end
32
-
33
- def test_root_as_argument_takes_precedence
34
- assert_equal({
35
- argument: [
36
- { name: 'Name 1', description: 'Description 1' },
37
- { name: 'Name 2', description: 'Description 2' }
38
- ]
39
- }, @serializer.as_json(root: :argument))
40
- end
41
-
42
- def test_using_false_root_in_initialize_takes_precedence
43
- ArraySerializer._root = 'root'
44
- @serializer = ArraySerializer.new([@profile1, @profile2], root: false)
45
-
46
- assert_equal([
47
- { name: 'Name 1', description: 'Description 1' },
48
- { name: 'Name 2', description: 'Description 2' }
49
- ], @serializer.as_json)
50
- end
51
- end
52
-
53
- class RootInSerializerTest < Minitest::Test
54
- def setup
55
- @old_root = ArraySerializer._root
56
- ArraySerializer._root = :in_serializer
57
- @profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
58
- @profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
59
- @serializer = ArraySerializer.new([@profile1, @profile2])
60
- @rooted_serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
61
- end
62
-
63
- def teardown
64
- ArraySerializer._root = @old_root
65
- end
66
-
67
- def test_root_is_not_displayed_using_serializable_hash
68
- assert_equal([
69
- { name: 'Name 1', description: 'Description 1' },
70
- { name: 'Name 2', description: 'Description 2' }
71
- ], @serializer.serializable_array)
72
- end
73
-
74
- def test_root_using_as_json
75
- assert_equal({
76
- in_serializer: [
77
- { name: 'Name 1', description: 'Description 1' },
78
- { name: 'Name 2', description: 'Description 2' }
79
- ]
80
- }, @serializer.as_json)
81
- end
82
-
83
- def test_root_in_initializer_takes_precedence
84
- assert_equal({
85
- initialize: [
86
- { name: 'Name 1', description: 'Description 1' },
87
- { name: 'Name 2', description: 'Description 2' }
88
- ]
89
- }, @rooted_serializer.as_json)
90
- end
91
-
92
- def test_root_as_argument_takes_precedence
93
- assert_equal({
94
- argument: [
95
- { name: 'Name 1', description: 'Description 1' },
96
- { name: 'Name 2', description: 'Description 2' }
97
- ]
98
- }, @rooted_serializer.as_json(root: :argument))
99
- end
100
- end
101
- end
102
- end
@@ -1,24 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class ArraySerializer
5
- class ScopeTest < Minitest::Test
6
- def test_array_serializer_pass_options_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, scope: current_user)
10
-
11
- expected = [{ name: 'Name 1', description: 'Description 1 - user' },
12
- { name: 'Name 2', description: 'Description 2 - user' }]
13
-
14
- assert_equal expected, serializer.serializable_array
15
- end
16
-
17
- private
18
-
19
- def current_user
20
- 'user'
21
- end
22
- end
23
- end
24
- end