active_model_serializers 0.10.3 → 0.10.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (224) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +154 -2
  3. data/README.md +153 -15
  4. data/lib/action_controller/serialization.rb +11 -1
  5. data/lib/active_model/serializable_resource.rb +2 -0
  6. data/lib/active_model/serializer.rb +275 -81
  7. data/lib/active_model/serializer/adapter.rb +2 -0
  8. data/lib/active_model/serializer/adapter/attributes.rb +2 -0
  9. data/lib/active_model/serializer/adapter/base.rb +2 -0
  10. data/lib/active_model/serializer/adapter/json.rb +2 -0
  11. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  12. data/lib/active_model/serializer/adapter/null.rb +2 -0
  13. data/lib/active_model/serializer/array_serializer.rb +2 -0
  14. data/lib/active_model/serializer/association.rb +53 -14
  15. data/lib/active_model/serializer/attribute.rb +2 -0
  16. data/lib/active_model/serializer/belongs_to_reflection.rb +7 -1
  17. data/lib/active_model/serializer/collection_serializer.rb +8 -5
  18. data/lib/active_model/serializer/concerns/caching.rb +36 -23
  19. data/lib/active_model/serializer/error_serializer.rb +2 -0
  20. data/lib/active_model/serializer/errors_serializer.rb +2 -0
  21. data/lib/active_model/serializer/field.rb +2 -0
  22. data/lib/active_model/serializer/fieldset.rb +3 -1
  23. data/lib/active_model/serializer/has_many_reflection.rb +6 -1
  24. data/lib/active_model/serializer/has_one_reflection.rb +3 -1
  25. data/lib/active_model/serializer/lazy_association.rb +99 -0
  26. data/lib/active_model/serializer/link.rb +23 -0
  27. data/lib/active_model/serializer/lint.rb +2 -0
  28. data/lib/active_model/serializer/null.rb +2 -0
  29. data/lib/active_model/serializer/reflection.rb +122 -73
  30. data/lib/active_model/serializer/version.rb +3 -1
  31. data/lib/active_model_serializers.rb +29 -11
  32. data/lib/active_model_serializers/adapter.rb +3 -1
  33. data/lib/active_model_serializers/adapter/attributes.rb +23 -0
  34. data/lib/active_model_serializers/adapter/base.rb +4 -2
  35. data/lib/active_model_serializers/adapter/json.rb +2 -0
  36. data/lib/active_model_serializers/adapter/json_api.rb +44 -26
  37. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
  38. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  39. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  40. data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
  41. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  42. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +42 -21
  43. data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -9
  44. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +35 -18
  45. data/lib/active_model_serializers/adapter/null.rb +2 -0
  46. data/lib/active_model_serializers/callbacks.rb +2 -0
  47. data/lib/active_model_serializers/deprecate.rb +2 -0
  48. data/lib/active_model_serializers/deserialization.rb +2 -0
  49. data/lib/active_model_serializers/json_pointer.rb +2 -0
  50. data/lib/active_model_serializers/logging.rb +2 -0
  51. data/lib/active_model_serializers/lookup_chain.rb +2 -0
  52. data/lib/active_model_serializers/model.rb +111 -30
  53. data/lib/active_model_serializers/model/caching.rb +25 -0
  54. data/lib/active_model_serializers/railtie.rb +4 -0
  55. data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
  56. data/lib/active_model_serializers/serializable_resource.rb +4 -2
  57. data/lib/active_model_serializers/serialization_context.rb +2 -0
  58. data/lib/active_model_serializers/test.rb +2 -0
  59. data/lib/active_model_serializers/test/schema.rb +4 -2
  60. data/lib/active_model_serializers/test/serializer.rb +2 -0
  61. data/lib/generators/rails/resource_override.rb +3 -1
  62. data/lib/generators/rails/serializer_generator.rb +2 -0
  63. data/lib/grape/active_model_serializers.rb +2 -0
  64. data/lib/grape/formatters/active_model_serializers.rb +2 -0
  65. data/lib/grape/helpers/active_model_serializers.rb +2 -0
  66. data/lib/tasks/rubocop.rake +55 -0
  67. metadata +74 -291
  68. data/.github/ISSUE_TEMPLATE.md +0 -29
  69. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  70. data/.gitignore +0 -35
  71. data/.rubocop.yml +0 -102
  72. data/.simplecov +0 -110
  73. data/.travis.yml +0 -51
  74. data/CODE_OF_CONDUCT.md +0 -74
  75. data/CONTRIBUTING.md +0 -105
  76. data/Gemfile +0 -56
  77. data/Rakefile +0 -103
  78. data/active_model_serializers.gemspec +0 -62
  79. data/appveyor.yml +0 -24
  80. data/bin/bench +0 -171
  81. data/bin/bench_regression +0 -316
  82. data/bin/serve_benchmark +0 -39
  83. data/docs/ARCHITECTURE.md +0 -125
  84. data/docs/README.md +0 -42
  85. data/docs/STYLE.md +0 -58
  86. data/docs/general/adapters.md +0 -247
  87. data/docs/general/caching.md +0 -58
  88. data/docs/general/configuration_options.md +0 -169
  89. data/docs/general/deserialization.md +0 -100
  90. data/docs/general/fields.md +0 -31
  91. data/docs/general/getting_started.md +0 -133
  92. data/docs/general/instrumentation.md +0 -40
  93. data/docs/general/key_transforms.md +0 -40
  94. data/docs/general/logging.md +0 -14
  95. data/docs/general/rendering.md +0 -294
  96. data/docs/general/serializers.md +0 -461
  97. data/docs/how-open-source-maintained.jpg +0 -0
  98. data/docs/howto/add_pagination_links.md +0 -138
  99. data/docs/howto/add_relationship_links.md +0 -137
  100. data/docs/howto/add_root_key.md +0 -55
  101. data/docs/howto/grape_integration.md +0 -42
  102. data/docs/howto/outside_controller_use.md +0 -65
  103. data/docs/howto/passing_arbitrary_options.md +0 -27
  104. data/docs/howto/serialize_poro.md +0 -32
  105. data/docs/howto/test.md +0 -154
  106. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  107. data/docs/integrations/ember-and-json-api.md +0 -144
  108. data/docs/integrations/grape.md +0 -19
  109. data/docs/jsonapi/errors.md +0 -56
  110. data/docs/jsonapi/schema.md +0 -151
  111. data/docs/jsonapi/schema/schema.json +0 -366
  112. data/docs/rfcs/0000-namespace.md +0 -106
  113. data/docs/rfcs/template.md +0 -15
  114. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  115. data/lib/active_model/serializer/concerns/associations.rb +0 -102
  116. data/lib/active_model/serializer/concerns/attributes.rb +0 -82
  117. data/lib/active_model/serializer/concerns/configuration.rb +0 -59
  118. data/lib/active_model/serializer/concerns/links.rb +0 -35
  119. data/lib/active_model/serializer/concerns/meta.rb +0 -29
  120. data/lib/active_model/serializer/concerns/type.rb +0 -25
  121. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  122. data/lib/active_model_serializers/key_transform.rb +0 -74
  123. data/test/action_controller/adapter_selector_test.rb +0 -53
  124. data/test/action_controller/explicit_serializer_test.rb +0 -135
  125. data/test/action_controller/json/include_test.rb +0 -246
  126. data/test/action_controller/json_api/deserialization_test.rb +0 -112
  127. data/test/action_controller/json_api/errors_test.rb +0 -40
  128. data/test/action_controller/json_api/fields_test.rb +0 -57
  129. data/test/action_controller/json_api/linked_test.rb +0 -202
  130. data/test/action_controller/json_api/pagination_test.rb +0 -116
  131. data/test/action_controller/json_api/transform_test.rb +0 -181
  132. data/test/action_controller/lookup_proc_test.rb +0 -49
  133. data/test/action_controller/namespace_lookup_test.rb +0 -226
  134. data/test/action_controller/serialization_scope_name_test.rb +0 -229
  135. data/test/action_controller/serialization_test.rb +0 -472
  136. data/test/active_model_serializers/adapter_for_test.rb +0 -208
  137. data/test/active_model_serializers/json_pointer_test.rb +0 -22
  138. data/test/active_model_serializers/key_transform_test.rb +0 -297
  139. data/test/active_model_serializers/logging_test.rb +0 -77
  140. data/test/active_model_serializers/model_test.rb +0 -22
  141. data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
  142. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -143
  143. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
  144. data/test/active_model_serializers/test/schema_test.rb +0 -130
  145. data/test/active_model_serializers/test/serializer_test.rb +0 -62
  146. data/test/active_record_test.rb +0 -9
  147. data/test/adapter/attributes_test.rb +0 -43
  148. data/test/adapter/deprecation_test.rb +0 -100
  149. data/test/adapter/json/belongs_to_test.rb +0 -45
  150. data/test/adapter/json/collection_test.rb +0 -104
  151. data/test/adapter/json/has_many_test.rb +0 -45
  152. data/test/adapter/json/transform_test.rb +0 -93
  153. data/test/adapter/json_api/belongs_to_test.rb +0 -155
  154. data/test/adapter/json_api/collection_test.rb +0 -96
  155. data/test/adapter/json_api/errors_test.rb +0 -76
  156. data/test/adapter/json_api/fields_test.rb +0 -88
  157. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  158. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
  159. data/test/adapter/json_api/has_many_test.rb +0 -165
  160. data/test/adapter/json_api/has_one_test.rb +0 -80
  161. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -166
  162. data/test/adapter/json_api/json_api_test.rb +0 -33
  163. data/test/adapter/json_api/linked_test.rb +0 -413
  164. data/test/adapter/json_api/links_test.rb +0 -95
  165. data/test/adapter/json_api/pagination_links_test.rb +0 -193
  166. data/test/adapter/json_api/parse_test.rb +0 -137
  167. data/test/adapter/json_api/relationship_test.rb +0 -397
  168. data/test/adapter/json_api/resource_identifier_test.rb +0 -110
  169. data/test/adapter/json_api/resource_meta_test.rb +0 -100
  170. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
  171. data/test/adapter/json_api/transform_test.rb +0 -504
  172. data/test/adapter/json_api/type_test.rb +0 -61
  173. data/test/adapter/json_test.rb +0 -46
  174. data/test/adapter/null_test.rb +0 -22
  175. data/test/adapter/polymorphic_test.rb +0 -171
  176. data/test/adapter_test.rb +0 -67
  177. data/test/array_serializer_test.rb +0 -22
  178. data/test/benchmark/app.rb +0 -65
  179. data/test/benchmark/benchmarking_support.rb +0 -67
  180. data/test/benchmark/bm_active_record.rb +0 -81
  181. data/test/benchmark/bm_adapter.rb +0 -38
  182. data/test/benchmark/bm_caching.rb +0 -119
  183. data/test/benchmark/bm_lookup_chain.rb +0 -83
  184. data/test/benchmark/bm_transform.rb +0 -45
  185. data/test/benchmark/config.ru +0 -3
  186. data/test/benchmark/controllers.rb +0 -83
  187. data/test/benchmark/fixtures.rb +0 -219
  188. data/test/cache_test.rb +0 -579
  189. data/test/collection_serializer_test.rb +0 -110
  190. data/test/fixtures/active_record.rb +0 -78
  191. data/test/fixtures/poro.rb +0 -286
  192. data/test/generators/scaffold_controller_generator_test.rb +0 -24
  193. data/test/generators/serializer_generator_test.rb +0 -74
  194. data/test/grape_test.rb +0 -178
  195. data/test/lint_test.rb +0 -49
  196. data/test/logger_test.rb +0 -20
  197. data/test/poro_test.rb +0 -9
  198. data/test/serializable_resource_test.rb +0 -79
  199. data/test/serializers/association_macros_test.rb +0 -37
  200. data/test/serializers/associations_test.rb +0 -370
  201. data/test/serializers/attribute_test.rb +0 -151
  202. data/test/serializers/attributes_test.rb +0 -52
  203. data/test/serializers/caching_configuration_test_isolated.rb +0 -170
  204. data/test/serializers/configuration_test.rb +0 -32
  205. data/test/serializers/fieldset_test.rb +0 -14
  206. data/test/serializers/meta_test.rb +0 -202
  207. data/test/serializers/options_test.rb +0 -21
  208. data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
  209. data/test/serializers/root_test.rb +0 -21
  210. data/test/serializers/serialization_test.rb +0 -55
  211. data/test/serializers/serializer_for_test.rb +0 -136
  212. data/test/serializers/serializer_for_with_namespace_test.rb +0 -87
  213. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  214. data/test/support/isolated_unit.rb +0 -82
  215. data/test/support/rails5_shims.rb +0 -53
  216. data/test/support/rails_app.rb +0 -36
  217. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  218. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  219. data/test/support/schemas/custom/show.json +0 -7
  220. data/test/support/schemas/hyper_schema.json +0 -93
  221. data/test/support/schemas/render_using_json_api.json +0 -43
  222. data/test/support/schemas/simple_json_pointers.json +0 -10
  223. data/test/support/serialization_testing.rb +0 -71
  224. data/test/test_helper.rb +0 -58
@@ -1,106 +0,0 @@
1
- - Start Date: (2015-10-29)
2
- - RFC PR: https://github.com/rails-api/active_model_serializers/pull/1310
3
- - ActiveModelSerializers Issue: https://github.com/rails-api/active_model_serializers/issues/1298
4
-
5
- # Summary
6
-
7
- Provide a consistent API for the user of the AMS.
8
-
9
- # Motivation
10
-
11
- The actual public API is defined under `ActiveModelSerializers`,
12
- `ActiveModel::Serializer` and `ActiveModel`.
13
-
14
- At the `ActiveModel::Serializer` we have:
15
-
16
- - `ActiveModel::Serializer.config`
17
- - `ActiveModel::Serializer`
18
-
19
- At the `ActiveModelSerializers` we have:
20
-
21
- - `ActiveModelSerializers::Model`
22
- - `ActiveModelSerializers.logger`
23
-
24
- At `ActiveModel` we have:
25
-
26
- - `ActiveModel::SerializableResource`
27
-
28
- The idea here is to provide a single namespace `ActiveModelSerializers` to the user.
29
- Following the same idea we have on other gems like
30
- [Devise](https://github.com/plataformatec/devise/blob/e9c82472ffe7c43a448945f77e034a0e47dde0bb/lib/devise.rb),
31
- [Refile](https://github.com/refile/refile/blob/6b24c293d044862dafbf1bfa4606672a64903aa2/lib/refile.rb) and
32
- [Active Job](https://github.com/rails/rails/blob/30bacc26f8f258b39e12f63fe52389a968d9c1ea/activejob/lib/active_job.rb)
33
- for example.
34
-
35
- This way we are clarifing the boundaries of
36
- [ActiveModelSerializers and Rails](https://github.com/rails-api/active_model_serializers/blob/master/CHANGELOG.md#prehistory)
37
- and make clear that the `ActiveModel::Serializer` class is no longer the primary
38
- behavior of the ActiveModelSerializers.
39
-
40
- # Detailed design
41
-
42
- ## New classes and modules organization
43
-
44
- Since this will be a big change we can do this on baby steps, read small pull requests. A
45
- possible approach is:
46
-
47
- - All new code will be in `lib/active_model_serializers/` using
48
- the module namespace `ActiveModelSerializers`.
49
- - Move all content under `ActiveModel::Serializer` to be under
50
- `ActiveModelSerializers`, the adapter is on this steps;
51
- - Move all content under `ActiveModel` to be under `ActiveModelSerializers`,
52
- the `SerializableResource` is on this step;
53
- - Change all public API that doesn't make sense, keeping in mind only to keep
54
- this in the same namespace
55
- - Update the README;
56
- - Update the docs;
57
-
58
- The following table represents the current and the desired classes and modules
59
- at the first moment.
60
-
61
- | Current | Desired | Notes |
62
- |--------------------------------------------------------|--------------------------------------------------|--------------------|
63
- | `ActiveModelSerializers` and `ActiveModel::Serializer` | `ActiveModelSerializers` | The main namespace |
64
- | `ActiveModelSerializers.logger` | `ActiveModelSerializers.logger` ||
65
- | `ActiveModelSerializers::Model` | `ActiveModelSerializers::Model` ||
66
- | `ActiveModel::SerializableResource` | `ActiveModelSerializers::SerializableResource` ||
67
- | `ActiveModel::Serializer` | `ActiveModelSerializers::Serializer` | The name can be discussed in a future pull request. For example, we can rename this to `Resource` [following this idea](https://github.com/rails-api/active_model_serializers/pull/1301/files#r42963185) more info about naming in the next section|
68
- | `ActiveModel::Serializer.config` | `ActiveModelSerializers.config` ||
69
-
70
- ## Renaming of class and modules
71
-
72
- When moving some content to the new namespace we can find some names that does
73
- not make much sense like `ActiveModel::Serializer::Adapter::JsonApi`.
74
- Discussion of renaming existing classes / modules and JsonApi objects will
75
- happen in separate pull requests, and issues, and in the google doc
76
- https://docs.google.com/document/d/1rcrJr0sVcazY2Opd_6Kmv1iIwuHbI84s1P_NzFn-05c/edit?usp=sharing
77
-
78
- Some of names already have a definition.
79
-
80
- - Adapters get their own namespace under ActiveModelSerializers. E.g
81
- `ActiveModelSerializers::Adapter`
82
- - Serializers get their own namespace under ActiveModelSerializers. E.g
83
- `ActiveModelSerializers::Serializer`
84
-
85
- ## Keeping compatibility
86
-
87
- All moved classes or modules be aliased to their old name and location with
88
- deprecation warnings, such as
89
- [was done for CollectionSerializer](https://github.com/rails-api/active_model_serializers/pull/1251).
90
-
91
- # Drawbacks
92
-
93
- This will be a breaking change, so all users serializers will be broken after a
94
- major bump.
95
- All pull requests will need to rebase since the architeture will change a lot.
96
-
97
- # Alternatives
98
-
99
- We can keep the way it is, and keep in mind to not add another namespace as a
100
- public API.
101
-
102
- # Unresolved questions
103
-
104
- What is the better class name to be used to the class that will be inherited at
105
- the creation of a serializer. This can be discussed in other RFC or directly via
106
- pull request.
@@ -1,15 +0,0 @@
1
- - Start Date: (YYYY-MM-DD)
2
- - RFC PR: https://github.com/rails-api/active_model_serializers/pull/dddd
3
- - ActiveModelSerializers Issue: https://github.com/rails-api/active_model_serializers/issues/dddd
4
-
5
- # Summary
6
-
7
- # Motivation
8
-
9
- # Detailed design
10
-
11
- # Drawbacks
12
-
13
- # Alternatives
14
-
15
- # Unresolved questions
@@ -1,7 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- # @api private
4
- class CollectionReflection < Reflection
5
- end
6
- end
7
- end
@@ -1,102 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- # Defines an association in the object should be rendered.
4
- #
5
- # The serializer object should implement the association name
6
- # as a method which should return an array when invoked. If a method
7
- # with the association name does not exist, the association name is
8
- # dispatched to the serialized object.
9
- #
10
- module Associations
11
- extend ActiveSupport::Concern
12
-
13
- included do
14
- with_options instance_writer: false, instance_reader: true do |serializer|
15
- serializer.class_attribute :_reflections
16
- self._reflections ||= {}
17
- end
18
-
19
- extend ActiveSupport::Autoload
20
- autoload :Association
21
- autoload :Reflection
22
- autoload :SingularReflection
23
- autoload :CollectionReflection
24
- autoload :BelongsToReflection
25
- autoload :HasOneReflection
26
- autoload :HasManyReflection
27
- end
28
-
29
- module ClassMethods
30
- def inherited(base)
31
- super
32
- base._reflections = _reflections.dup
33
- end
34
-
35
- # @param [Symbol] name of the association
36
- # @param [Hash<Symbol => any>] options for the reflection
37
- # @return [void]
38
- #
39
- # @example
40
- # has_many :comments, serializer: CommentSummarySerializer
41
- #
42
- def has_many(name, options = {}, &block) # rubocop:disable Style/PredicateName
43
- associate(HasManyReflection.new(name, options, block))
44
- end
45
-
46
- # @param [Symbol] name of the association
47
- # @param [Hash<Symbol => any>] options for the reflection
48
- # @return [void]
49
- #
50
- # @example
51
- # belongs_to :author, serializer: AuthorSerializer
52
- #
53
- def belongs_to(name, options = {}, &block)
54
- associate(BelongsToReflection.new(name, options, block))
55
- end
56
-
57
- # @param [Symbol] name of the association
58
- # @param [Hash<Symbol => any>] options for the reflection
59
- # @return [void]
60
- #
61
- # @example
62
- # has_one :author, serializer: AuthorSerializer
63
- #
64
- def has_one(name, options = {}, &block) # rubocop:disable Style/PredicateName
65
- associate(HasOneReflection.new(name, options, block))
66
- end
67
-
68
- private
69
-
70
- # Add reflection and define {name} accessor.
71
- # @param [ActiveModel::Serializer::Reflection] reflection
72
- # @return [void]
73
- #
74
- # @api private
75
- #
76
- def associate(reflection)
77
- key = reflection.options[:key] || reflection.name
78
- self._reflections[key] = reflection
79
- end
80
- end
81
-
82
- # @param [JSONAPI::IncludeDirective] include_directive (defaults to the
83
- # +default_include_directive+ config value when not provided)
84
- # @return [Enumerator<Association>]
85
- #
86
- def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
87
- include_slice ||= include_directive
88
- return unless object
89
-
90
- Enumerator.new do |y|
91
- self.class._reflections.values.each do |reflection|
92
- next if reflection.excluded?(self)
93
- key = reflection.options.fetch(:key, reflection.name)
94
- next unless include_directive.key?(key)
95
-
96
- y.yield reflection.build_association(self, instance_options, include_slice)
97
- end
98
- end
99
- end
100
- end
101
- end
102
- end
@@ -1,82 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- module Attributes
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- with_options instance_writer: false, instance_reader: false do |serializer|
8
- serializer.class_attribute :_attributes_data # @api private
9
- self._attributes_data ||= {}
10
- end
11
-
12
- extend ActiveSupport::Autoload
13
- autoload :Attribute
14
-
15
- # Return the +attributes+ of +object+ as presented
16
- # by the serializer.
17
- def attributes(requested_attrs = nil, reload = false)
18
- @attributes = nil if reload
19
- @attributes ||= self.class._attributes_data.each_with_object({}) do |(key, attr), hash|
20
- next if attr.excluded?(self)
21
- next unless requested_attrs.nil? || requested_attrs.include?(key)
22
- hash[key] = attr.value(self)
23
- end
24
- end
25
- end
26
-
27
- module ClassMethods
28
- def inherited(base)
29
- super
30
- base._attributes_data = _attributes_data.dup
31
- end
32
-
33
- # @example
34
- # class AdminAuthorSerializer < ActiveModel::Serializer
35
- # attributes :id, :name, :recent_edits
36
- def attributes(*attrs)
37
- attrs = attrs.first if attrs.first.class == Array
38
-
39
- attrs.each do |attr|
40
- attribute(attr)
41
- end
42
- end
43
-
44
- # @example
45
- # class AdminAuthorSerializer < ActiveModel::Serializer
46
- # attributes :id, :recent_edits
47
- # attribute :name, key: :title
48
- #
49
- # attribute :full_name do
50
- # "#{object.first_name} #{object.last_name}"
51
- # end
52
- #
53
- # def recent_edits
54
- # object.edits.last(5)
55
- # end
56
- def attribute(attr, options = {}, &block)
57
- key = options.fetch(:key, attr)
58
- _attributes_data[key] = Attribute.new(attr, options, block)
59
- end
60
-
61
- # @api private
62
- # keys of attributes
63
- # @see Serializer::attribute
64
- def _attributes
65
- _attributes_data.keys
66
- end
67
-
68
- # @api private
69
- # maps attribute value to explict key name
70
- # @see Serializer::attribute
71
- # @see FragmentCache#fragment_serializer
72
- def _attributes_keys
73
- _attributes_data
74
- .each_with_object({}) do |(key, attr), hash|
75
- next if key == attr.name
76
- hash[attr.name] = { key: key }
77
- end
78
- end
79
- end
80
- end
81
- end
82
- end
@@ -1,59 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- module Configuration
4
- include ActiveSupport::Configurable
5
- extend ActiveSupport::Concern
6
-
7
- # Configuration options may also be set in
8
- # Serializers and Adapters
9
- included do |base|
10
- config = base.config
11
- config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
12
- config.serializer_lookup_enabled = true
13
-
14
- def config.array_serializer=(collection_serializer)
15
- self.collection_serializer = collection_serializer
16
- end
17
-
18
- def config.array_serializer
19
- collection_serializer
20
- end
21
-
22
- config.default_includes = '*'
23
- config.adapter = :attributes
24
- config.key_transform = nil
25
- config.jsonapi_pagination_links_enabled = true
26
- config.jsonapi_resource_type = :plural
27
- config.jsonapi_namespace_separator = '-'.freeze
28
- config.jsonapi_version = '1.0'
29
- config.jsonapi_toplevel_meta = {}
30
- # Make JSON API top-level jsonapi member opt-in
31
- # ref: http://jsonapi.org/format/#document-top-level
32
- config.jsonapi_include_toplevel_object = false
33
- config.include_data_default = true
34
-
35
- # For configuring how serializers are found.
36
- # This should be an array of procs.
37
- #
38
- # The priority of the output is that the first item
39
- # in the evaluated result array will take precedence
40
- # over other possible serializer paths.
41
- #
42
- # i.e.: First match wins.
43
- #
44
- # @example output
45
- # => [
46
- # "CustomNamespace::ResourceSerializer",
47
- # "ParentSerializer::ResourceSerializer",
48
- # "ResourceNamespace::ResourceSerializer" ,
49
- # "ResourceSerializer"]
50
- #
51
- # If CustomNamespace::ResourceSerializer exists, it will be used
52
- # for serialization
53
- config.serializer_lookup_chain = ActiveModelSerializers::LookupChain::DEFAULT.dup
54
-
55
- config.schema_path = 'test/support/schemas'
56
- end
57
- end
58
- end
59
- end
@@ -1,35 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- module Links
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- with_options instance_writer: false, instance_reader: true do |serializer|
8
- serializer.class_attribute :_links # @api private
9
- self._links ||= {}
10
- end
11
-
12
- extend ActiveSupport::Autoload
13
- end
14
-
15
- module ClassMethods
16
- def inherited(base)
17
- super
18
- base._links = _links.dup
19
- end
20
-
21
- # Define a link on a serializer.
22
- # @example
23
- # link(:self) { resource_url(object) }
24
- # @example
25
- # link(:self) { "http://example.com/resource/#{object.id}" }
26
- # @example
27
- # link :resource, "http://example.com/resource"
28
- #
29
- def link(name, value = nil, &block)
30
- _links[name] = block || value
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,29 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- module Meta
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- with_options instance_writer: false, instance_reader: true do |serializer|
8
- serializer.class_attribute :_meta # @api private
9
- end
10
-
11
- extend ActiveSupport::Autoload
12
- end
13
-
14
- module ClassMethods
15
- # Set the JSON API meta attribute of a serializer.
16
- # @example
17
- # class AdminAuthorSerializer < ActiveModel::Serializer
18
- # meta { stuff: 'value' }
19
- # @example
20
- # meta do
21
- # { comment_count: object.comments.count }
22
- # end
23
- def meta(value = nil, &block)
24
- self._meta = block || value
25
- end
26
- end
27
- end
28
- end
29
- end