active_model_serializers 0.10.9 → 0.10.13

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.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -2
  3. data/README.md +18 -19
  4. data/lib/action_controller/serialization.rb +9 -1
  5. data/lib/active_model/serializer/collection_serializer.rb +11 -2
  6. data/lib/active_model/serializer/concerns/caching.rb +2 -1
  7. data/lib/active_model/serializer/fieldset.rb +1 -1
  8. data/lib/active_model/serializer/reflection.rb +4 -4
  9. data/lib/active_model/serializer/version.rb +1 -1
  10. data/lib/active_model/serializer.rb +13 -4
  11. data/lib/active_model_serializers/adapter/attributes.rb +21 -0
  12. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +5 -1
  13. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
  14. data/lib/active_model_serializers/model/caching.rb +26 -0
  15. metadata +20 -292
  16. data/.github/ISSUE_TEMPLATE.md +0 -29
  17. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  18. data/.gitignore +0 -35
  19. data/.rubocop.yml +0 -109
  20. data/.simplecov +0 -110
  21. data/.travis.yml +0 -63
  22. data/CODE_OF_CONDUCT.md +0 -74
  23. data/CONTRIBUTING.md +0 -105
  24. data/Gemfile +0 -74
  25. data/Rakefile +0 -76
  26. data/active_model_serializers.gemspec +0 -64
  27. data/appveyor.yml +0 -28
  28. data/bin/bench +0 -171
  29. data/bin/bench_regression +0 -316
  30. data/bin/rubocop +0 -38
  31. data/bin/serve_benchmark +0 -39
  32. data/docs/README.md +0 -41
  33. data/docs/STYLE.md +0 -58
  34. data/docs/general/adapters.md +0 -269
  35. data/docs/general/caching.md +0 -58
  36. data/docs/general/configuration_options.md +0 -185
  37. data/docs/general/deserialization.md +0 -100
  38. data/docs/general/fields.md +0 -31
  39. data/docs/general/getting_started.md +0 -133
  40. data/docs/general/instrumentation.md +0 -40
  41. data/docs/general/key_transforms.md +0 -40
  42. data/docs/general/logging.md +0 -21
  43. data/docs/general/rendering.md +0 -293
  44. data/docs/general/serializers.md +0 -495
  45. data/docs/how-open-source-maintained.jpg +0 -0
  46. data/docs/howto/add_pagination_links.md +0 -138
  47. data/docs/howto/add_relationship_links.md +0 -140
  48. data/docs/howto/add_root_key.md +0 -62
  49. data/docs/howto/grape_integration.md +0 -42
  50. data/docs/howto/outside_controller_use.md +0 -66
  51. data/docs/howto/passing_arbitrary_options.md +0 -27
  52. data/docs/howto/serialize_poro.md +0 -73
  53. data/docs/howto/test.md +0 -154
  54. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  55. data/docs/integrations/ember-and-json-api.md +0 -147
  56. data/docs/integrations/grape.md +0 -19
  57. data/docs/jsonapi/errors.md +0 -56
  58. data/docs/jsonapi/schema/schema.json +0 -366
  59. data/docs/jsonapi/schema.md +0 -151
  60. data/docs/rfcs/0000-namespace.md +0 -106
  61. data/docs/rfcs/template.md +0 -15
  62. data/test/action_controller/adapter_selector_test.rb +0 -64
  63. data/test/action_controller/explicit_serializer_test.rb +0 -137
  64. data/test/action_controller/json/include_test.rb +0 -248
  65. data/test/action_controller/json_api/deserialization_test.rb +0 -114
  66. data/test/action_controller/json_api/errors_test.rb +0 -42
  67. data/test/action_controller/json_api/fields_test.rb +0 -68
  68. data/test/action_controller/json_api/linked_test.rb +0 -204
  69. data/test/action_controller/json_api/pagination_test.rb +0 -126
  70. data/test/action_controller/json_api/transform_test.rb +0 -191
  71. data/test/action_controller/lookup_proc_test.rb +0 -51
  72. data/test/action_controller/namespace_lookup_test.rb +0 -234
  73. data/test/action_controller/serialization_scope_name_test.rb +0 -237
  74. data/test/action_controller/serialization_test.rb +0 -480
  75. data/test/active_model_serializers/adapter_for_test.rb +0 -210
  76. data/test/active_model_serializers/json_pointer_test.rb +0 -24
  77. data/test/active_model_serializers/logging_test.rb +0 -79
  78. data/test/active_model_serializers/model_test.rb +0 -144
  79. data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
  80. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
  81. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
  82. data/test/active_model_serializers/test/schema_test.rb +0 -133
  83. data/test/active_model_serializers/test/serializer_test.rb +0 -64
  84. data/test/active_record_test.rb +0 -11
  85. data/test/adapter/attributes_test.rb +0 -42
  86. data/test/adapter/deprecation_test.rb +0 -102
  87. data/test/adapter/json/belongs_to_test.rb +0 -47
  88. data/test/adapter/json/collection_test.rb +0 -106
  89. data/test/adapter/json/has_many_test.rb +0 -55
  90. data/test/adapter/json/transform_test.rb +0 -95
  91. data/test/adapter/json_api/belongs_to_test.rb +0 -157
  92. data/test/adapter/json_api/collection_test.rb +0 -98
  93. data/test/adapter/json_api/errors_test.rb +0 -78
  94. data/test/adapter/json_api/fields_test.rb +0 -98
  95. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
  96. data/test/adapter/json_api/has_many_test.rb +0 -175
  97. data/test/adapter/json_api/has_one_test.rb +0 -82
  98. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
  99. data/test/adapter/json_api/json_api_test.rb +0 -35
  100. data/test/adapter/json_api/linked_test.rb +0 -415
  101. data/test/adapter/json_api/links_test.rb +0 -112
  102. data/test/adapter/json_api/pagination_links_test.rb +0 -208
  103. data/test/adapter/json_api/parse_test.rb +0 -139
  104. data/test/adapter/json_api/relationship_test.rb +0 -399
  105. data/test/adapter/json_api/resource_meta_test.rb +0 -102
  106. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
  107. data/test/adapter/json_api/transform_test.rb +0 -514
  108. data/test/adapter/json_api/type_test.rb +0 -195
  109. data/test/adapter/json_test.rb +0 -48
  110. data/test/adapter/null_test.rb +0 -24
  111. data/test/adapter/polymorphic_test.rb +0 -220
  112. data/test/adapter_test.rb +0 -69
  113. data/test/array_serializer_test.rb +0 -24
  114. data/test/benchmark/app.rb +0 -67
  115. data/test/benchmark/benchmarking_support.rb +0 -69
  116. data/test/benchmark/bm_active_record.rb +0 -83
  117. data/test/benchmark/bm_adapter.rb +0 -40
  118. data/test/benchmark/bm_caching.rb +0 -121
  119. data/test/benchmark/bm_lookup_chain.rb +0 -85
  120. data/test/benchmark/bm_transform.rb +0 -47
  121. data/test/benchmark/config.ru +0 -3
  122. data/test/benchmark/controllers.rb +0 -85
  123. data/test/benchmark/fixtures.rb +0 -221
  124. data/test/cache_test.rb +0 -717
  125. data/test/collection_serializer_test.rb +0 -129
  126. data/test/fixtures/active_record.rb +0 -115
  127. data/test/fixtures/poro.rb +0 -227
  128. data/test/generators/scaffold_controller_generator_test.rb +0 -26
  129. data/test/generators/serializer_generator_test.rb +0 -77
  130. data/test/grape_test.rb +0 -198
  131. data/test/lint_test.rb +0 -51
  132. data/test/logger_test.rb +0 -22
  133. data/test/poro_test.rb +0 -11
  134. data/test/serializable_resource_test.rb +0 -81
  135. data/test/serializers/association_macros_test.rb +0 -39
  136. data/test/serializers/associations_test.rb +0 -520
  137. data/test/serializers/attribute_test.rb +0 -155
  138. data/test/serializers/attributes_test.rb +0 -54
  139. data/test/serializers/caching_configuration_test_isolated.rb +0 -172
  140. data/test/serializers/configuration_test.rb +0 -34
  141. data/test/serializers/fieldset_test.rb +0 -16
  142. data/test/serializers/meta_test.rb +0 -204
  143. data/test/serializers/options_test.rb +0 -34
  144. data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
  145. data/test/serializers/reflection_test.rb +0 -481
  146. data/test/serializers/root_test.rb +0 -23
  147. data/test/serializers/serialization_test.rb +0 -57
  148. data/test/serializers/serializer_for_test.rb +0 -138
  149. data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
  150. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  151. data/test/support/isolated_unit.rb +0 -86
  152. data/test/support/rails5_shims.rb +0 -55
  153. data/test/support/rails_app.rb +0 -40
  154. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  155. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  156. data/test/support/schemas/custom/show.json +0 -7
  157. data/test/support/schemas/hyper_schema.json +0 -93
  158. data/test/support/schemas/render_using_json_api.json +0 -43
  159. data/test/support/schemas/simple_json_pointers.json +0 -10
  160. data/test/support/serialization_testing.rb +0 -81
  161. data/test/test_helper.rb +0 -72
@@ -1,54 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModel
6
- class Serializer
7
- class AttributesTest < ActiveSupport::TestCase
8
- def setup
9
- @profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
10
- @profile_serializer = ProfileSerializer.new(@profile)
11
- @comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
12
- @serializer_klass = Class.new(CommentSerializer)
13
- @serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
14
- attributes :date, :likes
15
- end
16
- end
17
-
18
- def test_attributes_definition
19
- assert_equal([:name, :description],
20
- @profile_serializer.class._attributes)
21
- end
22
-
23
- def test_attributes_inheritance_definition
24
- assert_equal([:id, :body], @serializer_klass._attributes)
25
- end
26
-
27
- def test_attributes_inheritance
28
- serializer = @serializer_klass.new(@comment)
29
- assert_equal({ id: 1, body: 'ZOMG!!' },
30
- serializer.attributes)
31
- end
32
-
33
- def test_attribute_inheritance_with_new_attribute_definition
34
- assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes)
35
- assert_equal([:id, :body], CommentSerializer._attributes)
36
- end
37
-
38
- def test_attribute_inheritance_with_new_attribute
39
- serializer = @serializer_klass_with_new_attributes.new(@comment)
40
- assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil },
41
- serializer.attributes)
42
- end
43
-
44
- def test_multiple_calls_with_the_same_attribute
45
- serializer_class = Class.new(ActiveModel::Serializer) do
46
- attributes :id, :title
47
- attributes :id, :title, :title, :body
48
- end
49
-
50
- assert_equal([:id, :title, :body], serializer_class._attributes)
51
- end
52
- end
53
- end
54
- end
@@ -1,172 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Execute this test in isolation
4
- require 'support/isolated_unit'
5
-
6
- class CachingConfigurationTest < ActiveSupport::TestCase
7
- include ActiveSupport::Testing::Isolation
8
-
9
- setup do
10
- require 'rails'
11
- # AMS needs to be required before Rails.application is initialized for
12
- # Railtie's to fire in Rails.application.initialize!
13
- # (and make_basic_app initializes the app)
14
- require 'active_model_serializers'
15
- # Create serializers before Rails.application.initialize!
16
- # To ensure we're testing that the cache settings depend on
17
- # the Railtie firing, not on the ActionController being loaded.
18
- create_serializers
19
- end
20
-
21
- def create_serializers
22
- @cached_serializer = Class.new(ActiveModel::Serializer) do
23
- cache skip_digest: true
24
- attributes :id, :name, :title
25
- end
26
- @fragment_cached_serializer = Class.new(ActiveModel::Serializer) do
27
- cache only: :id
28
- attributes :id, :name, :title
29
- end
30
- @non_cached_serializer = Class.new(ActiveModel::Serializer) do
31
- attributes :id, :name, :title
32
- end
33
- end
34
-
35
- class PerformCachingTrue < CachingConfigurationTest
36
- setup do
37
- # Let's make that Rails app and initialize it!
38
- make_basic_app do |app|
39
- app.config.action_controller.perform_caching = true
40
- app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
41
- end
42
- controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
43
- end
44
-
45
- test 'it sets perform_caching to true on AMS.config and serializers' do
46
- assert Rails.configuration.action_controller.perform_caching
47
- assert ActiveModelSerializers.config.perform_caching
48
- assert ActiveModel::Serializer.perform_caching?
49
- assert @cached_serializer.perform_caching?
50
- assert @non_cached_serializer.perform_caching?
51
- assert @fragment_cached_serializer.perform_caching?
52
- end
53
-
54
- test 'it sets the AMS.config.cache_store to the controller cache_store' do
55
- assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore
56
- assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class
57
- end
58
-
59
- test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do
60
- assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class
61
- assert_equal controller_cache_store, @cached_serializer.cache_store.class
62
- assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class
63
- end
64
-
65
- test 'the cached serializer has cache_enabled?' do
66
- assert @cached_serializer.cache_enabled?
67
- end
68
-
69
- test 'the cached serializer does not have fragment_cache_enabled?' do
70
- refute @cached_serializer.fragment_cache_enabled?
71
- end
72
-
73
- test 'the non-cached serializer cache_store is nil' do
74
- assert_nil @non_cached_serializer._cache
75
- assert_nil @non_cached_serializer.cache_store
76
- assert_nil @non_cached_serializer._cache
77
- end
78
-
79
- test 'the non-cached serializer does not have cache_enabled?' do
80
- refute @non_cached_serializer.cache_enabled?
81
- end
82
-
83
- test 'the non-cached serializer does not have fragment_cache_enabled?' do
84
- refute @non_cached_serializer.fragment_cache_enabled?
85
- end
86
-
87
- test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do
88
- assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class
89
- assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class
90
- assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class
91
- end
92
-
93
- test 'the fragment cached serializer does not have cache_enabled?' do
94
- refute @fragment_cached_serializer.cache_enabled?
95
- end
96
-
97
- test 'the fragment cached serializer has fragment_cache_enabled?' do
98
- assert @fragment_cached_serializer.fragment_cache_enabled?
99
- end
100
- end
101
-
102
- class PerformCachingFalse < CachingConfigurationTest
103
- setup do
104
- # Let's make that Rails app and initialize it!
105
- make_basic_app do |app|
106
- app.config.action_controller.perform_caching = false
107
- app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
108
- end
109
- controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
110
- end
111
-
112
- test 'it sets perform_caching to false on AMS.config and serializers' do
113
- refute Rails.configuration.action_controller.perform_caching
114
- refute ActiveModelSerializers.config.perform_caching
115
- refute ActiveModel::Serializer.perform_caching?
116
- refute @cached_serializer.perform_caching?
117
- refute @non_cached_serializer.perform_caching?
118
- refute @fragment_cached_serializer.perform_caching?
119
- end
120
-
121
- test 'it sets the AMS.config.cache_store to the controller cache_store' do
122
- assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore
123
- assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class
124
- end
125
-
126
- test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do
127
- assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class
128
- assert_equal controller_cache_store, @cached_serializer.cache_store.class
129
- assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class
130
- end
131
-
132
- test 'the cached serializer does not have cache_enabled?' do
133
- refute @cached_serializer.cache_enabled?
134
- end
135
-
136
- test 'the cached serializer does not have fragment_cache_enabled?' do
137
- refute @cached_serializer.fragment_cache_enabled?
138
- end
139
-
140
- test 'the non-cached serializer cache_store is nil' do
141
- assert_nil @non_cached_serializer._cache
142
- assert_nil @non_cached_serializer.cache_store
143
- assert_nil @non_cached_serializer._cache
144
- end
145
-
146
- test 'the non-cached serializer does not have cache_enabled?' do
147
- refute @non_cached_serializer.cache_enabled?
148
- end
149
-
150
- test 'the non-cached serializer does not have fragment_cache_enabled?' do
151
- refute @non_cached_serializer.fragment_cache_enabled?
152
- end
153
-
154
- test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do
155
- assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class
156
- assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class
157
- assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class
158
- end
159
-
160
- test 'the fragment cached serializer does not have cache_enabled?' do
161
- refute @fragment_cached_serializer.cache_enabled?
162
- end
163
-
164
- test 'the fragment cached serializer does not have fragment_cache_enabled?' do
165
- refute @fragment_cached_serializer.fragment_cache_enabled?
166
- end
167
- end
168
-
169
- def controller_cache_store
170
- ActionController::Base.cache_store.class
171
- end
172
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModel
6
- class Serializer
7
- class ConfigurationTest < ActiveSupport::TestCase
8
- def test_collection_serializer
9
- assert_equal ActiveModel::Serializer::CollectionSerializer, ActiveModelSerializers.config.collection_serializer
10
- end
11
-
12
- def test_array_serializer
13
- assert_equal ActiveModel::Serializer::CollectionSerializer, ActiveModelSerializers.config.array_serializer
14
- end
15
-
16
- def test_setting_array_serializer_sets_collection_serializer
17
- config = ActiveModelSerializers.config
18
- old_config = config.dup
19
- begin
20
- assert_equal ActiveModel::Serializer::CollectionSerializer, config.collection_serializer
21
- config.array_serializer = :foo
22
- assert_equal config.array_serializer, :foo
23
- assert_equal config.collection_serializer, :foo
24
- ensure
25
- ActiveModelSerializers.config.replace(old_config)
26
- end
27
- end
28
-
29
- def test_default_adapter
30
- assert_equal :attributes, ActiveModelSerializers.config.adapter
31
- end
32
- end
33
- end
34
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModel
6
- class Serializer
7
- class FieldsetTest < ActiveSupport::TestCase
8
- def test_fieldset_with_hash
9
- fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
10
- expected = { post: [:id, :title], comment: [:body] }
11
-
12
- assert_equal(expected, fieldset.fields)
13
- end
14
- end
15
- end
16
- end
@@ -1,204 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModel
6
- class Serializer
7
- class MetaTest < ActiveSupport::TestCase
8
- def setup
9
- @blog = Blog.new(id: 1,
10
- name: 'AMS Hints',
11
- writer: Author.new(id: 2, name: 'Steve'),
12
- articles: [Post.new(id: 3, title: 'AMS')])
13
- end
14
-
15
- def test_meta_is_present_with_root
16
- actual = ActiveModelSerializers::SerializableResource.new(
17
- @blog,
18
- adapter: :json,
19
- serializer: AlternateBlogSerializer,
20
- meta: { total: 10 }
21
- ).as_json
22
- expected = {
23
- blog: {
24
- id: 1,
25
- title: 'AMS Hints'
26
- },
27
- 'meta' => {
28
- total: 10
29
- }
30
- }
31
- assert_equal(expected, actual)
32
- end
33
-
34
- def test_meta_is_not_included_when_blank
35
- actual = ActiveModelSerializers::SerializableResource.new(
36
- @blog,
37
- adapter: :json,
38
- serializer: AlternateBlogSerializer,
39
- meta: {}
40
- ).as_json
41
- expected = {
42
- blog: {
43
- id: 1,
44
- title: 'AMS Hints'
45
- }
46
- }
47
- assert_equal(expected, actual)
48
- end
49
-
50
- def test_meta_is_not_included_when_empty_string
51
- actual = ActiveModelSerializers::SerializableResource.new(
52
- @blog,
53
- adapter: :json,
54
- serializer: AlternateBlogSerializer,
55
- meta: ''
56
- ).as_json
57
- expected = {
58
- blog: {
59
- id: 1,
60
- title: 'AMS Hints'
61
- }
62
- }
63
- assert_equal(expected, actual)
64
- end
65
-
66
- def test_meta_is_not_included_when_root_is_missing
67
- actual = ActiveModelSerializers::SerializableResource.new(
68
- @blog,
69
- adapter: :attributes,
70
- serializer: AlternateBlogSerializer,
71
- meta: { total: 10 }
72
- ).as_json
73
- expected = {
74
- id: 1,
75
- title: 'AMS Hints'
76
- }
77
- assert_equal(expected, actual)
78
- end
79
-
80
- def test_meta_key_is_used
81
- actual = ActiveModelSerializers::SerializableResource.new(
82
- @blog,
83
- adapter: :json,
84
- serializer: AlternateBlogSerializer,
85
- meta: { total: 10 },
86
- meta_key: 'haha_meta'
87
- ).as_json
88
- expected = {
89
- blog: {
90
- id: 1,
91
- title: 'AMS Hints'
92
- },
93
- 'haha_meta' => {
94
- total: 10
95
- }
96
- }
97
- assert_equal(expected, actual)
98
- end
99
-
100
- def test_meta_key_is_not_used_with_json_api
101
- actual = ActiveModelSerializers::SerializableResource.new(
102
- @blog,
103
- adapter: :json_api,
104
- serializer: AlternateBlogSerializer,
105
- meta: { total: 10 },
106
- meta_key: 'haha_meta'
107
- ).as_json
108
- expected = {
109
- data: {
110
- id: '1',
111
- type: 'blogs',
112
- attributes: { title: 'AMS Hints' }
113
- },
114
- meta: { total: 10 }
115
- }
116
- assert_equal(expected, actual)
117
- end
118
-
119
- def test_meta_key_is_not_present_when_empty_hash_with_json_api
120
- actual = ActiveModelSerializers::SerializableResource.new(
121
- @blog,
122
- adapter: :json_api,
123
- serializer: AlternateBlogSerializer,
124
- meta: {}
125
- ).as_json
126
- expected = {
127
- data: {
128
- id: '1',
129
- type: 'blogs',
130
- attributes: { title: 'AMS Hints' }
131
- }
132
- }
133
- assert_equal(expected, actual)
134
- end
135
-
136
- def test_meta_key_is_not_present_when_empty_string_with_json_api
137
- actual = ActiveModelSerializers::SerializableResource.new(
138
- @blog,
139
- adapter: :json_api,
140
- serializer: AlternateBlogSerializer,
141
- meta: ''
142
- ).as_json
143
- expected = {
144
- data: {
145
- id: '1',
146
- type: 'blogs',
147
- attributes: { title: 'AMS Hints' }
148
- }
149
- }
150
- assert_equal(expected, actual)
151
- end
152
-
153
- def test_meta_is_not_present_on_arrays_without_root
154
- actual = ActiveModelSerializers::SerializableResource.new(
155
- [@blog],
156
- adapter: :attributes,
157
- meta: { total: 10 }
158
- ).as_json
159
- expected = [{
160
- id: 1,
161
- name: 'AMS Hints',
162
- writer: {
163
- id: 2,
164
- name: 'Steve'
165
- },
166
- articles: [{
167
- id: 3,
168
- title: 'AMS',
169
- body: nil
170
- }]
171
- }]
172
- assert_equal(expected, actual)
173
- end
174
-
175
- def test_meta_is_present_on_arrays_with_root
176
- actual = ActiveModelSerializers::SerializableResource.new(
177
- [@blog],
178
- adapter: :json,
179
- meta: { total: 10 },
180
- meta_key: 'haha_meta'
181
- ).as_json
182
- expected = {
183
- blogs: [{
184
- id: 1,
185
- name: 'AMS Hints',
186
- writer: {
187
- id: 2,
188
- name: 'Steve'
189
- },
190
- articles: [{
191
- id: 3,
192
- title: 'AMS',
193
- body: nil
194
- }]
195
- }],
196
- 'haha_meta' => {
197
- total: 10
198
- }
199
- }
200
- assert_equal(expected, actual)
201
- end
202
- end
203
- end
204
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModel
6
- class Serializer
7
- class OptionsTest < ActiveSupport::TestCase
8
- class ModelWithOptions < ActiveModelSerializers::Model
9
- attributes :name, :description
10
- end
11
- class ModelWithOptionsSerializer < ActiveModel::Serializer
12
- attributes :name, :description
13
-
14
- def arguments_passed_in?
15
- instance_options[:my_options] == :accessible
16
- end
17
- end
18
-
19
- setup do
20
- @model_with_options = ModelWithOptions.new(name: 'Name 1', description: 'Description 1')
21
- end
22
-
23
- def test_options_are_accessible
24
- model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options, my_options: :accessible)
25
- assert model_with_options_serializer.arguments_passed_in?
26
- end
27
-
28
- def test_no_option_is_passed_in
29
- model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options)
30
- refute model_with_options_serializer.arguments_passed_in?
31
- end
32
- end
33
- end
34
- end
@@ -1,81 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModel
6
- class Serializer
7
- class ReadAttributeForSerializationTest < ActiveSupport::TestCase
8
- # https://github.com/rails-api/active_model_serializers/issues/1653
9
- class Parent < ActiveModelSerializers::Model
10
- attributes :id
11
- end
12
- class Child < Parent
13
- attributes :name
14
- end
15
- class ParentSerializer < ActiveModel::Serializer
16
- attributes :$id
17
-
18
- define_method(:$id) do
19
- object.id
20
- end
21
- end
22
- class ChildSerializer < ParentSerializer
23
- attributes :name
24
- end
25
-
26
- def test_child_serializer_calls_dynamic_method_in_parent_serializer
27
- parent = ParentSerializer.new(Parent.new(id: 5))
28
- child = ChildSerializer.new(Child.new(id: 6, name: 'Child'))
29
- assert_equal 5, parent.read_attribute_for_serialization(:$id)
30
- assert_equal 6, child.read_attribute_for_serialization(:$id)
31
- end
32
-
33
- # https://github.com/rails-api/active_model_serializers/issues/1658
34
- class ErrorResponse < ActiveModelSerializers::Model
35
- attributes :error
36
- end
37
- class ApplicationSerializer < ActiveModel::Serializer
38
- attributes :status
39
-
40
- def status
41
- object.try(:errors).blank? && object.try(:error).blank?
42
- end
43
- end
44
- class ErrorResponseSerializer < ApplicationSerializer
45
- attributes :error
46
- end
47
- class ErrorResponseWithSuperSerializer < ApplicationSerializer
48
- attributes :error
49
-
50
- def success
51
- super
52
- end
53
- end
54
-
55
- def test_child_serializer_with_error_attribute
56
- error = ErrorResponse.new(error: 'i have an error')
57
- serializer = ErrorResponseSerializer.new(error)
58
- serializer_with_super = ErrorResponseWithSuperSerializer.new(error)
59
- assert_equal false, serializer.read_attribute_for_serialization(:status)
60
- assert_equal false, serializer_with_super.read_attribute_for_serialization(:status)
61
- end
62
-
63
- def test_child_serializer_with_errors
64
- error = ErrorResponse.new
65
- error.errors.add(:invalid, 'i am not valid')
66
- serializer = ErrorResponseSerializer.new(error)
67
- serializer_with_super = ErrorResponseWithSuperSerializer.new(error)
68
- assert_equal false, serializer.read_attribute_for_serialization(:status)
69
- assert_equal false, serializer_with_super.read_attribute_for_serialization(:status)
70
- end
71
-
72
- def test_child_serializer_no_error_attribute_or_errors
73
- error = ErrorResponse.new
74
- serializer = ErrorResponseSerializer.new(error)
75
- serializer_with_super = ErrorResponseWithSuperSerializer.new(error)
76
- assert_equal true, serializer.read_attribute_for_serialization(:status)
77
- assert_equal true, serializer_with_super.read_attribute_for_serialization(:status)
78
- end
79
- end
80
- end
81
- end