active_model_serializers 0.10.0 → 0.10.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +10 -5
  3. data/.travis.yml +41 -21
  4. data/CHANGELOG.md +200 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +25 -4
  7. data/README.md +166 -28
  8. data/Rakefile +5 -32
  9. data/active_model_serializers.gemspec +23 -25
  10. data/appveyor.yml +10 -6
  11. data/bin/rubocop +38 -0
  12. data/docs/README.md +2 -1
  13. data/docs/general/adapters.md +35 -11
  14. data/docs/general/caching.md +7 -1
  15. data/docs/general/configuration_options.md +86 -1
  16. data/docs/general/deserialization.md +1 -1
  17. data/docs/general/fields.md +31 -0
  18. data/docs/general/getting_started.md +1 -1
  19. data/docs/general/logging.md +7 -0
  20. data/docs/general/rendering.md +63 -25
  21. data/docs/general/serializers.md +137 -14
  22. data/docs/howto/add_pagination_links.md +16 -17
  23. data/docs/howto/add_relationship_links.md +140 -0
  24. data/docs/howto/add_root_key.md +11 -0
  25. data/docs/howto/grape_integration.md +42 -0
  26. data/docs/howto/outside_controller_use.md +12 -4
  27. data/docs/howto/passing_arbitrary_options.md +2 -2
  28. data/docs/howto/serialize_poro.md +46 -5
  29. data/docs/howto/test.md +2 -0
  30. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  31. data/docs/integrations/ember-and-json-api.md +67 -32
  32. data/docs/jsonapi/schema.md +1 -1
  33. data/lib/action_controller/serialization.rb +15 -3
  34. data/lib/active_model/serializable_resource.rb +2 -0
  35. data/lib/active_model/serializer/adapter/attributes.rb +2 -0
  36. data/lib/active_model/serializer/adapter/base.rb +4 -0
  37. data/lib/active_model/serializer/adapter/json.rb +2 -0
  38. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  39. data/lib/active_model/serializer/adapter/null.rb +2 -0
  40. data/lib/active_model/serializer/adapter.rb +2 -0
  41. data/lib/active_model/serializer/array_serializer.rb +10 -5
  42. data/lib/active_model/serializer/association.rb +64 -10
  43. data/lib/active_model/serializer/attribute.rb +2 -0
  44. data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
  45. data/lib/active_model/serializer/collection_serializer.rb +39 -13
  46. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +87 -116
  47. data/lib/active_model/serializer/error_serializer.rb +13 -7
  48. data/lib/active_model/serializer/errors_serializer.rb +27 -20
  49. data/lib/active_model/serializer/field.rb +2 -0
  50. data/lib/active_model/serializer/fieldset.rb +2 -0
  51. data/lib/active_model/serializer/has_many_reflection.rb +5 -3
  52. data/lib/active_model/serializer/has_one_reflection.rb +3 -4
  53. data/lib/active_model/serializer/lazy_association.rb +99 -0
  54. data/lib/active_model/serializer/link.rb +23 -0
  55. data/lib/active_model/serializer/lint.rb +136 -130
  56. data/lib/active_model/serializer/null.rb +2 -0
  57. data/lib/active_model/serializer/reflection.rb +132 -67
  58. data/lib/active_model/serializer/version.rb +3 -1
  59. data/lib/active_model/serializer.rb +308 -82
  60. data/lib/active_model_serializers/adapter/attributes.rb +5 -66
  61. data/lib/active_model_serializers/adapter/base.rb +41 -39
  62. data/lib/active_model_serializers/adapter/json.rb +2 -0
  63. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
  64. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  65. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  66. data/lib/active_model_serializers/adapter/json_api/link.rb +3 -1
  67. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  68. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +49 -21
  69. data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
  70. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
  71. data/lib/active_model_serializers/adapter/json_api.rb +84 -65
  72. data/lib/active_model_serializers/adapter/null.rb +2 -0
  73. data/lib/active_model_serializers/adapter.rb +9 -1
  74. data/lib/active_model_serializers/callbacks.rb +2 -0
  75. data/lib/active_model_serializers/deprecate.rb +3 -2
  76. data/lib/active_model_serializers/deserialization.rb +4 -0
  77. data/lib/active_model_serializers/json_pointer.rb +2 -0
  78. data/lib/active_model_serializers/logging.rb +2 -0
  79. data/lib/active_model_serializers/lookup_chain.rb +82 -0
  80. data/lib/active_model_serializers/model.rb +111 -28
  81. data/lib/active_model_serializers/railtie.rb +7 -1
  82. data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
  83. data/lib/active_model_serializers/serializable_resource.rb +10 -7
  84. data/lib/active_model_serializers/serialization_context.rb +12 -3
  85. data/lib/active_model_serializers/test/schema.rb +4 -2
  86. data/lib/active_model_serializers/test/serializer.rb +2 -0
  87. data/lib/active_model_serializers/test.rb +2 -0
  88. data/lib/active_model_serializers.rb +35 -10
  89. data/lib/generators/rails/resource_override.rb +3 -1
  90. data/lib/generators/rails/serializer_generator.rb +6 -4
  91. data/lib/grape/active_model_serializers.rb +9 -5
  92. data/lib/grape/formatters/active_model_serializers.rb +21 -2
  93. data/lib/grape/helpers/active_model_serializers.rb +3 -0
  94. data/lib/tasks/rubocop.rake +55 -0
  95. data/test/action_controller/adapter_selector_test.rb +16 -5
  96. data/test/action_controller/explicit_serializer_test.rb +7 -4
  97. data/test/action_controller/json/include_test.rb +108 -27
  98. data/test/action_controller/json_api/deserialization_test.rb +3 -1
  99. data/test/action_controller/json_api/errors_test.rb +10 -9
  100. data/test/action_controller/json_api/fields_test.rb +68 -0
  101. data/test/action_controller/json_api/linked_test.rb +31 -24
  102. data/test/action_controller/json_api/pagination_test.rb +33 -23
  103. data/test/action_controller/json_api/transform_test.rb +13 -3
  104. data/test/action_controller/lookup_proc_test.rb +51 -0
  105. data/test/action_controller/namespace_lookup_test.rb +234 -0
  106. data/test/action_controller/serialization_scope_name_test.rb +14 -6
  107. data/test/action_controller/serialization_test.rb +23 -12
  108. data/test/active_model_serializers/adapter_for_test.rb +2 -0
  109. data/test/active_model_serializers/json_pointer_test.rb +17 -13
  110. data/test/active_model_serializers/logging_test.rb +2 -0
  111. data/test/active_model_serializers/model_test.rb +139 -4
  112. data/test/active_model_serializers/railtie_test_isolated.rb +14 -7
  113. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
  114. data/test/active_model_serializers/serialization_context_test_isolated.rb +25 -10
  115. data/test/active_model_serializers/test/schema_test.rb +5 -2
  116. data/test/active_model_serializers/test/serializer_test.rb +2 -0
  117. data/test/active_record_test.rb +2 -0
  118. data/test/adapter/attributes_test.rb +42 -0
  119. data/test/adapter/deprecation_test.rb +2 -0
  120. data/test/adapter/json/belongs_to_test.rb +2 -0
  121. data/test/adapter/json/collection_test.rb +16 -0
  122. data/test/adapter/json/has_many_test.rb +12 -2
  123. data/test/adapter/json/transform_test.rb +17 -15
  124. data/test/adapter/json_api/belongs_to_test.rb +2 -0
  125. data/test/adapter/json_api/collection_test.rb +6 -3
  126. data/test/adapter/json_api/errors_test.rb +19 -19
  127. data/test/adapter/json_api/fields_test.rb +14 -3
  128. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -0
  129. data/test/adapter/json_api/has_many_test.rb +51 -20
  130. data/test/adapter/json_api/has_one_test.rb +2 -0
  131. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
  132. data/test/adapter/json_api/json_api_test.rb +7 -7
  133. data/test/adapter/json_api/linked_test.rb +35 -12
  134. data/test/adapter/json_api/links_test.rb +22 -3
  135. data/test/adapter/json_api/pagination_links_test.rb +55 -13
  136. data/test/adapter/json_api/parse_test.rb +3 -1
  137. data/test/adapter/json_api/relationship_test.rb +311 -73
  138. data/test/adapter/json_api/resource_meta_test.rb +5 -3
  139. data/test/adapter/json_api/toplevel_jsonapi_test.rb +2 -0
  140. data/test/adapter/json_api/transform_test.rb +265 -253
  141. data/test/adapter/json_api/type_test.rb +170 -36
  142. data/test/adapter/json_test.rb +10 -7
  143. data/test/adapter/null_test.rb +3 -2
  144. data/test/adapter/polymorphic_test.rb +54 -5
  145. data/test/adapter_test.rb +3 -1
  146. data/test/array_serializer_test.rb +2 -0
  147. data/test/benchmark/app.rb +3 -1
  148. data/test/benchmark/benchmarking_support.rb +3 -1
  149. data/test/benchmark/bm_active_record.rb +83 -0
  150. data/test/benchmark/bm_adapter.rb +40 -0
  151. data/test/benchmark/bm_caching.rb +18 -16
  152. data/test/benchmark/bm_lookup_chain.rb +85 -0
  153. data/test/benchmark/bm_transform.rb +23 -10
  154. data/test/benchmark/controllers.rb +18 -17
  155. data/test/benchmark/fixtures.rb +74 -72
  156. data/test/cache_test.rb +301 -69
  157. data/test/collection_serializer_test.rb +33 -14
  158. data/test/fixtures/active_record.rb +47 -10
  159. data/test/fixtures/poro.rb +128 -183
  160. data/test/generators/scaffold_controller_generator_test.rb +2 -0
  161. data/test/generators/serializer_generator_test.rb +25 -5
  162. data/test/grape_test.rb +172 -56
  163. data/test/lint_test.rb +3 -1
  164. data/test/logger_test.rb +15 -11
  165. data/test/poro_test.rb +2 -0
  166. data/test/serializable_resource_test.rb +20 -22
  167. data/test/serializers/association_macros_test.rb +5 -2
  168. data/test/serializers/associations_test.rb +274 -49
  169. data/test/serializers/attribute_test.rb +7 -3
  170. data/test/serializers/attributes_test.rb +3 -1
  171. data/test/serializers/caching_configuration_test_isolated.rb +8 -6
  172. data/test/serializers/configuration_test.rb +2 -0
  173. data/test/serializers/fieldset_test.rb +3 -1
  174. data/test/serializers/meta_test.rb +14 -6
  175. data/test/serializers/options_test.rb +19 -6
  176. data/test/serializers/read_attribute_for_serialization_test.rb +5 -3
  177. data/test/serializers/reflection_test.rb +481 -0
  178. data/test/serializers/root_test.rb +3 -1
  179. data/test/serializers/serialization_test.rb +4 -2
  180. data/test/serializers/serializer_for_test.rb +14 -10
  181. data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
  182. data/test/support/isolated_unit.rb +11 -4
  183. data/test/support/rails5_shims.rb +10 -2
  184. data/test/support/rails_app.rb +4 -9
  185. data/test/support/serialization_testing.rb +33 -5
  186. data/test/test_helper.rb +15 -0
  187. metadata +126 -46
  188. data/.rubocop_todo.yml +0 -167
  189. data/docs/ARCHITECTURE.md +0 -126
  190. data/lib/active_model/serializer/associations.rb +0 -100
  191. data/lib/active_model/serializer/attributes.rb +0 -82
  192. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  193. data/lib/active_model/serializer/configuration.rb +0 -35
  194. data/lib/active_model/serializer/include_tree.rb +0 -111
  195. data/lib/active_model/serializer/links.rb +0 -35
  196. data/lib/active_model/serializer/meta.rb +0 -29
  197. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  198. data/lib/active_model/serializer/type.rb +0 -25
  199. data/lib/active_model_serializers/key_transform.rb +0 -70
  200. data/test/active_model_serializers/key_transform_test.rb +0 -263
  201. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  202. data/test/adapter/json_api/relationships_test.rb +0 -199
  203. data/test/adapter/json_api/resource_identifier_test.rb +0 -85
  204. data/test/include_tree/from_include_args_test.rb +0 -26
  205. data/test/include_tree/from_string_test.rb +0 -94
  206. data/test/include_tree/include_args_to_hash_test.rb +0 -64
data/docs/ARCHITECTURE.md DELETED
@@ -1,126 +0,0 @@
1
- [Back to Guides](README.md)
2
-
3
- This document focuses on architecture the 0.10.x version of ActiveModelSerializers. If you are interested in the architecture of the 0.8 or 0.9 versions,
4
- please refer to the [0.8 README](https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md) or
5
- [0.9 README](https://github.com/rails-api/active_model_serializers/blob/0-9-stable/README.md).
6
-
7
- The original design is also available [here](https://github.com/rails-api/active_model_serializers/blob/d72b66d4c5355b0ff0a75a04895fcc4ea5b0c65e/README.textile).
8
-
9
- # ARCHITECTURE
10
-
11
- An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/serialization.rb)
12
- and exposes an `attributes` method, among a few others.
13
- It allows you to specify which attributes and associations should be represented in the serializatation of the resource.
14
- It requires an adapter to transform its attributes into a JSON document; it cannot be serialized itself.
15
- It may be useful to think of it as a
16
- [presenter](http://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters).
17
-
18
- The **`ActiveModel::ArraySerializer`** represent a collection of resources as serializers
19
- and, if there is no serializer, primitives.
20
-
21
- The **`ActiveModel::Adapter`** describes the structure of the JSON document generated from a
22
- serializer. For example, the `Attributes` example represents each serializer as its
23
- unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
24
- API](http://jsonapi.org/) document.
25
-
26
- The **`ActiveModelSerializers::SerializableResource`** acts to coordinate the serializer(s) and adapter
27
- to an object that responds to `to_json`, and `as_json`. It is used in the controller to
28
- encapsulate the serialization resource when rendered. However, it can also be used on its own
29
- to serialize a resource outside of a controller, as well.
30
-
31
- ## Primitive handling
32
-
33
- Definitions: A primitive is usually a String or Array. There is no serializer
34
- defined for them; they will be serialized when the resource is converted to JSON (`as_json` or
35
- `to_json`). (The below also applies for any object with no serializer.)
36
-
37
- ActiveModelSerializers doesn't handle primitives passed to `render json:` at all.
38
-
39
- However, when a primitive value is an attribute or in a collection,
40
- it is not modified.
41
-
42
- Internally, if no serializer can be found in the controller, the resource is not decorated by
43
- ActiveModelSerializers.
44
-
45
- If the collection serializer (ArraySerializer) cannot
46
- identify a serializer for a resource in its collection, it raises [`NoSerializerError`](https://github.com/rails-api/active_model_serializers/issues/1191#issuecomment-142327128)
47
- which is rescued in `ActiveModel::Serializer::Reflection#build_association` which sets
48
- the association value directly:
49
-
50
- ```ruby
51
- reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
52
- ```
53
-
54
- (which is called by the adapter as `serializer.associations(*)`.)
55
-
56
- ## How options are parsed
57
-
58
- High-level overview:
59
-
60
- - For a collection
61
- - `:serializer` specifies the collection serializer and
62
- - `:each_serializer` specifies the serializer for each resource in the collection.
63
- - For a single resource, the `:serializer` option is the resource serializer.
64
- - Options are partitioned in serializer options and adapter options. Keys for adapter options are specified by
65
- [`ADAPTER_OPTION_KEYS`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/serializable_resource.rb#L5).
66
- The remaining options are serializer options.
67
-
68
- Details:
69
-
70
- 1. **ActionController::Serialization**
71
- 1. `serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)`
72
- 1. `options` are partitioned into `adapter_opts` and everything else (`serializer_opts`).
73
- The `adapter_opts` keys are defined in `ActiveModelSerializers::SerializableResource::ADAPTER_OPTION_KEYS`.
74
- 1. **ActiveModelSerializers::SerializableResource**
75
- 1. `if serializable_resource.serializer?` (there is a serializer for the resource, and an adapter is used.)
76
- - Where `serializer?` is `use_adapter? && !!(serializer)`
77
- - Where `use_adapter?`: 'True when no explicit adapter given, or explicit value is truthy (non-nil);
78
- False when explicit adapter is falsy (nil or false)'
79
- - Where `serializer`:
80
- 1. from explicit `:serializer` option, else
81
- 2. implicitly from resource `ActiveModel::Serializer.serializer_for(resource)`
82
- 1. A side-effect of checking `serializer` is:
83
- - The `:serializer` option is removed from the serializer_opts hash
84
- - If the `:each_serializer` option is present, it is removed from the serializer_opts hash and set as the `:serializer` option
85
- 1. The serializer and adapter are created as
86
- 1. `serializer_instance = serializer.new(resource, serializer_opts)`
87
- 2. `adapter_instance = ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)`
88
- 1. **ActiveModel::Serializer::ArraySerializer#new**
89
- 1. If the `serializer_instance` was a `ArraySerializer` and the `:serializer` serializer_opts
90
- is present, then [that serializer is passed into each resource](https://github.com/rails-api/active_model_serializers/blob/a54d237e2828fe6bab1ea5dfe6360d4ecc8214cd/lib/active_model/serializer/array_serializer.rb#L14-L16).
91
- 1. **ActiveModel::Serializer#attributes** is used by the adapter to get the attributes for
92
- resource as defined by the serializer.
93
-
94
- ## What does a 'serializable resource' look like?
95
-
96
- - An `ActiveRecord::Base` object.
97
- - Any Ruby object that passes the
98
- [Lint](http://www.rubydoc.info/github/rails-api/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
99
- [code](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/lint.rb).
100
-
101
- ActiveModelSerializers provides a
102
- [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb),
103
- which is a simple serializable PORO (Plain-Old Ruby Object).
104
-
105
- ActiveModelSerializers::Model may be used either as a template, or in production code.
106
-
107
- ```ruby
108
- class MyModel < ActiveModelSerializers::Model
109
- attr_accessor :id, :name, :level
110
- end
111
- ```
112
-
113
- The default serializer for `MyModel` would be `MyModelSerializer` whether MyModel is an
114
- ActiveRecord::Base object or not.
115
-
116
- Outside of the controller the rules are **exactly** the same as for records. For example:
117
-
118
- ```ruby
119
- render json: MyModel.new(level: 'awesome'), adapter: :json
120
- ```
121
-
122
- would be serialized the same as
123
-
124
- ```ruby
125
- ActiveModelSerializers::SerializableResource.new(MyModel.new(level: 'awesome'), adapter: :json).as_json
126
- ```
@@ -1,100 +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
- DEFAULT_INCLUDE_TREE = ActiveModel::Serializer::IncludeTree.from_string('*')
14
-
15
- included do
16
- with_options instance_writer: false, instance_reader: true do |serializer|
17
- serializer.class_attribute :_reflections
18
- self._reflections ||= []
19
- end
20
-
21
- extend ActiveSupport::Autoload
22
- autoload :Association
23
- autoload :Reflection
24
- autoload :SingularReflection
25
- autoload :CollectionReflection
26
- autoload :BelongsToReflection
27
- autoload :HasOneReflection
28
- autoload :HasManyReflection
29
- end
30
-
31
- module ClassMethods
32
- def inherited(base)
33
- super
34
- base._reflections = _reflections.dup
35
- end
36
-
37
- # @param [Symbol] name of the association
38
- # @param [Hash<Symbol => any>] options for the reflection
39
- # @return [void]
40
- #
41
- # @example
42
- # has_many :comments, serializer: CommentSummarySerializer
43
- #
44
- def has_many(name, options = {}, &block)
45
- associate(HasManyReflection.new(name, options, block))
46
- end
47
-
48
- # @param [Symbol] name of the association
49
- # @param [Hash<Symbol => any>] options for the reflection
50
- # @return [void]
51
- #
52
- # @example
53
- # belongs_to :author, serializer: AuthorSerializer
54
- #
55
- def belongs_to(name, options = {}, &block)
56
- associate(BelongsToReflection.new(name, options, block))
57
- end
58
-
59
- # @param [Symbol] name of the association
60
- # @param [Hash<Symbol => any>] options for the reflection
61
- # @return [void]
62
- #
63
- # @example
64
- # has_one :author, serializer: AuthorSerializer
65
- #
66
- def has_one(name, options = {}, &block)
67
- associate(HasOneReflection.new(name, options, block))
68
- end
69
-
70
- private
71
-
72
- # Add reflection and define {name} accessor.
73
- # @param [ActiveModel::Serializer::Reflection] reflection
74
- # @return [void]
75
- #
76
- # @api private
77
- #
78
- def associate(reflection)
79
- self._reflections << reflection
80
- end
81
- end
82
-
83
- # @param [IncludeTree] include_tree (defaults to all associations when not provided)
84
- # @return [Enumerator<Association>]
85
- #
86
- def associations(include_tree = DEFAULT_INCLUDE_TREE)
87
- return unless object
88
-
89
- Enumerator.new do |y|
90
- self.class._reflections.each do |reflection|
91
- next if reflection.excluded?(self)
92
- key = reflection.options.fetch(:key, reflection.name)
93
- next unless include_tree.key?(key)
94
- y.yield reflection.build_association(self, instance_options)
95
- end
96
- end
97
- end
98
- end
99
- end
100
- 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,7 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- # @api private
4
- class CollectionReflection < Reflection
5
- end
6
- end
7
- end
@@ -1,35 +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.adapter = :attributes
23
- config.jsonapi_resource_type = :plural
24
- config.jsonapi_version = '1.0'
25
- config.jsonapi_toplevel_meta = {}
26
- # Make JSON API top-level jsonapi member opt-in
27
- # ref: http://jsonapi.org/format/#document-top-level
28
- config.jsonapi_include_toplevel_object = false
29
- config.key_transform = nil
30
-
31
- config.schema_path = 'test/support/schemas'
32
- end
33
- end
34
- end
35
- end
@@ -1,111 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- # TODO: description of this class, and overview of how it's used
4
- class IncludeTree
5
- module Parsing
6
- module_function
7
-
8
- # Translates a comma separated list of dot separated paths (JSON API format) into a Hash.
9
- #
10
- # @example
11
- # `'posts.author, posts.comments.upvotes, posts.comments.author'`
12
- #
13
- # would become
14
- #
15
- # `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`.
16
- #
17
- # @param [String] included
18
- # @return [Hash] a Hash representing the same tree structure
19
- def include_string_to_hash(included)
20
- # TODO: Needs comment walking through the process of what all this is doing.
21
- included.delete(' ').split(',').reduce({}) do |hash, path|
22
- include_tree = path.split('.').reverse_each.reduce({}) { |a, e| { e.to_sym => a } }
23
- hash.deep_merge!(include_tree)
24
- end
25
- end
26
-
27
- # Translates the arguments passed to the include option into a Hash. The format can be either
28
- # a String (see #include_string_to_hash), an Array of Symbols and Hashes, or a mix of both.
29
- #
30
- # @example
31
- # `posts: [:author, comments: [:author, :upvotes]]`
32
- #
33
- # would become
34
- #
35
- # `{ posts: { author: {}, comments: { author: {}, upvotes: {} } } }`.
36
- #
37
- # @example
38
- # `[:author, :comments => [:author]]`
39
- #
40
- # would become
41
- #
42
- # `{:author => {}, :comments => { author: {} } }`
43
- #
44
- # @param [Symbol, Hash, Array, String] included
45
- # @return [Hash] a Hash representing the same tree structure
46
- def include_args_to_hash(included)
47
- case included
48
- when Symbol
49
- { included => {} }
50
- when Hash
51
- included.each_with_object({}) do |(key, value), hash|
52
- hash[key] = include_args_to_hash(value)
53
- end
54
- when Array
55
- included.reduce({}) { |a, e| a.deep_merge!(include_args_to_hash(e)) }
56
- when String
57
- include_string_to_hash(included)
58
- else
59
- {}
60
- end
61
- end
62
- end
63
-
64
- # Builds an IncludeTree from a comma separated list of dot separated paths (JSON API format).
65
- # @example `'posts.author, posts.comments.upvotes, posts.comments.author'`
66
- #
67
- # @param [String] included
68
- # @return [IncludeTree]
69
- #
70
- def self.from_string(included)
71
- new(Parsing.include_string_to_hash(included))
72
- end
73
-
74
- # Translates the arguments passed to the include option into an IncludeTree.
75
- # The format can be either a String (see #from_string), an Array of Symbols and Hashes, or a mix of both.
76
- # @example `posts: [:author, comments: [:author, :upvotes]]`
77
- #
78
- # @param [Symbol, Hash, Array, String] included
79
- # @return [IncludeTree]
80
- #
81
- def self.from_include_args(included)
82
- return included if included.is_a?(IncludeTree)
83
-
84
- new(Parsing.include_args_to_hash(included))
85
- end
86
-
87
- # @param [Hash] hash
88
- def initialize(hash = {})
89
- @hash = hash
90
- end
91
-
92
- def key?(key)
93
- @hash.key?(key) || @hash.key?(:*) || @hash.key?(:**)
94
- end
95
-
96
- def [](key)
97
- # TODO(beauby): Adopt a lazy caching strategy for generating subtrees.
98
- case
99
- when @hash.key?(key)
100
- self.class.new(@hash[key])
101
- when @hash.key?(:*)
102
- self.class.new(@hash[:*])
103
- when @hash.key?(:**)
104
- self.class.new(:** => {})
105
- else
106
- nil
107
- end
108
- end
109
- end
110
- end
111
- 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
@@ -1,7 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- # @api private
4
- class SingularReflection < Reflection
5
- end
6
- end
7
- end
@@ -1,25 +0,0 @@
1
- module ActiveModel
2
- class Serializer
3
- module Type
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- with_options instance_writer: false, instance_reader: true do |serializer|
8
- serializer.class_attribute :_type # @api private
9
- end
10
-
11
- extend ActiveSupport::Autoload
12
- end
13
-
14
- module ClassMethods
15
- # Set the JSON API type of a serializer.
16
- # @example
17
- # class AdminAuthorSerializer < ActiveModel::Serializer
18
- # type 'authors'
19
- def type(type)
20
- self._type = type && type.to_s
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,70 +0,0 @@
1
- require 'active_support/core_ext/hash/keys'
2
-
3
- module ActiveModelSerializers
4
- module KeyTransform
5
- module_function
6
-
7
- # Transforms values to UpperCamelCase or PascalCase.
8
- #
9
- # @example:
10
- # "some_key" => "SomeKey",
11
- # @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L66-L76 ActiveSupport::Inflector.camelize}
12
- def camel(value)
13
- case value
14
- when Hash then value.deep_transform_keys! { |key| camel(key) }
15
- when Symbol then camel(value.to_s).to_sym
16
- when String then value.underscore.camelize
17
- else value
18
- end
19
- end
20
-
21
- # Transforms values to camelCase.
22
- #
23
- # @example:
24
- # "some_key" => "someKey",
25
- # @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L66-L76 ActiveSupport::Inflector.camelize}
26
- def camel_lower(value)
27
- case value
28
- when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
29
- when Symbol then camel_lower(value.to_s).to_sym
30
- when String then value.underscore.camelize(:lower)
31
- else value
32
- end
33
- end
34
-
35
- # Transforms values to dashed-case.
36
- # This is the default case for the JsonApi adapter.
37
- #
38
- # @example:
39
- # "some_key" => "some-key",
40
- # @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L185-L187 ActiveSupport::Inflector.dasherize}
41
- def dash(value)
42
- case value
43
- when Hash then value.deep_transform_keys! { |key| dash(key) }
44
- when Symbol then dash(value.to_s).to_sym
45
- when String then value.underscore.dasherize
46
- else value
47
- end
48
- end
49
-
50
- # Transforms values to underscore_case.
51
- # This is the default case for deserialization in the JsonApi adapter.
52
- #
53
- # @example:
54
- # "some-key" => "some_key",
55
- # @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L89-L98 ActiveSupport::Inflector.underscore}
56
- def underscore(value)
57
- case value
58
- when Hash then value.deep_transform_keys! { |key| underscore(key) }
59
- when Symbol then underscore(value.to_s).to_sym
60
- when String then value.underscore
61
- else value
62
- end
63
- end
64
-
65
- # Returns the value unaltered
66
- def unaltered(value)
67
- value
68
- end
69
- end
70
- end