active_model_serializers 0.10.0 → 0.10.2

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 (109) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -4
  3. data/.travis.yml +1 -0
  4. data/CHANGELOG.md +39 -2
  5. data/Gemfile +1 -1
  6. data/README.md +21 -24
  7. data/Rakefile +3 -3
  8. data/active_model_serializers.gemspec +19 -23
  9. data/docs/general/adapters.md +4 -2
  10. data/docs/general/configuration_options.md +6 -1
  11. data/docs/general/deserialization.md +1 -1
  12. data/docs/general/fields.md +31 -0
  13. data/docs/general/rendering.md +7 -2
  14. data/docs/general/serializers.md +62 -3
  15. data/docs/howto/add_pagination_links.md +2 -3
  16. data/docs/integrations/ember-and-json-api.md +25 -10
  17. data/docs/jsonapi/schema.md +1 -1
  18. data/lib/action_controller/serialization.rb +4 -3
  19. data/lib/active_model/serializer/adapter/base.rb +2 -0
  20. data/lib/active_model/serializer/array_serializer.rb +8 -5
  21. data/lib/active_model/serializer/associations.rb +6 -7
  22. data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
  23. data/lib/active_model/serializer/caching.rb +67 -112
  24. data/lib/active_model/serializer/collection_serializer.rb +30 -10
  25. data/lib/active_model/serializer/configuration.rb +1 -0
  26. data/lib/active_model/serializer/error_serializer.rb +11 -7
  27. data/lib/active_model/serializer/errors_serializer.rb +25 -20
  28. data/lib/active_model/serializer/has_many_reflection.rb +0 -3
  29. data/lib/active_model/serializer/has_one_reflection.rb +0 -3
  30. data/lib/active_model/serializer/lint.rb +134 -130
  31. data/lib/active_model/serializer/reflection.rb +3 -3
  32. data/lib/active_model/serializer/version.rb +1 -1
  33. data/lib/active_model/serializer.rb +57 -15
  34. data/lib/active_model_serializers/adapter/attributes.rb +2 -67
  35. data/lib/active_model_serializers/adapter/base.rb +38 -38
  36. data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
  37. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
  38. data/lib/active_model_serializers/adapter/json_api.rb +36 -28
  39. data/lib/active_model_serializers/adapter.rb +6 -0
  40. data/lib/active_model_serializers/deprecate.rb +1 -2
  41. data/lib/active_model_serializers/deserialization.rb +2 -0
  42. data/lib/active_model_serializers/model.rb +3 -1
  43. data/lib/active_model_serializers/railtie.rb +3 -1
  44. data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
  45. data/lib/active_model_serializers/serialization_context.rb +10 -3
  46. data/lib/active_model_serializers.rb +6 -0
  47. data/lib/generators/rails/serializer_generator.rb +3 -3
  48. data/lib/grape/active_model_serializers.rb +7 -5
  49. data/lib/grape/formatters/active_model_serializers.rb +19 -2
  50. data/lib/grape/helpers/active_model_serializers.rb +1 -0
  51. data/test/action_controller/adapter_selector_test.rb +4 -4
  52. data/test/action_controller/explicit_serializer_test.rb +5 -4
  53. data/test/action_controller/json/include_test.rb +106 -27
  54. data/test/action_controller/json_api/errors_test.rb +6 -7
  55. data/test/action_controller/json_api/linked_test.rb +29 -24
  56. data/test/action_controller/json_api/pagination_test.rb +19 -19
  57. data/test/action_controller/serialization_test.rb +10 -7
  58. data/test/active_model_serializers/json_pointer_test.rb +15 -13
  59. data/test/active_model_serializers/key_transform_test.rb +254 -252
  60. data/test/active_model_serializers/model_test.rb +6 -4
  61. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +143 -0
  62. data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
  63. data/test/adapter/json/collection_test.rb +14 -0
  64. data/test/adapter/json/transform_test.rb +14 -14
  65. data/test/adapter/json_api/collection_test.rb +4 -3
  66. data/test/adapter/json_api/errors_test.rb +17 -19
  67. data/test/adapter/json_api/has_many_test.rb +18 -18
  68. data/test/adapter/json_api/json_api_test.rb +5 -7
  69. data/test/adapter/json_api/linked_test.rb +9 -6
  70. data/test/adapter/json_api/links_test.rb +3 -1
  71. data/test/adapter/json_api/pagination_links_test.rb +19 -7
  72. data/test/adapter/json_api/relationships_test.rb +9 -4
  73. data/test/adapter/json_api/resource_identifier_test.rb +7 -2
  74. data/test/adapter/json_api/resource_meta_test.rb +3 -3
  75. data/test/adapter/json_api/transform_test.rb +251 -250
  76. data/test/adapter/json_api/type_test.rb +1 -1
  77. data/test/adapter/json_test.rb +8 -7
  78. data/test/adapter/null_test.rb +1 -2
  79. data/test/adapter/polymorphic_test.rb +5 -5
  80. data/test/adapter_test.rb +1 -1
  81. data/test/benchmark/app.rb +1 -1
  82. data/test/benchmark/bm_caching.rb +15 -15
  83. data/test/benchmark/bm_transform.rb +16 -5
  84. data/test/benchmark/controllers.rb +16 -17
  85. data/test/benchmark/fixtures.rb +72 -72
  86. data/test/cache_test.rb +101 -45
  87. data/test/collection_serializer_test.rb +2 -2
  88. data/test/fixtures/poro.rb +8 -7
  89. data/test/grape_test.rb +152 -56
  90. data/test/lint_test.rb +1 -1
  91. data/test/logger_test.rb +13 -11
  92. data/test/serializable_resource_test.rb +18 -22
  93. data/test/serializers/associations_test.rb +10 -10
  94. data/test/serializers/attribute_test.rb +1 -1
  95. data/test/serializers/attributes_test.rb +1 -1
  96. data/test/serializers/fieldset_test.rb +1 -1
  97. data/test/serializers/meta_test.rb +12 -6
  98. data/test/serializers/root_test.rb +1 -1
  99. data/test/serializers/serializer_for_test.rb +3 -1
  100. data/test/support/isolated_unit.rb +5 -2
  101. data/test/support/rails5_shims.rb +8 -2
  102. data/test/support/rails_app.rb +0 -9
  103. data/test/support/serialization_testing.rb +7 -5
  104. metadata +54 -24
  105. data/.rubocop_todo.yml +0 -167
  106. data/lib/active_model/serializer/include_tree.rb +0 -111
  107. data/test/include_tree/from_include_args_test.rb +0 -26
  108. data/test/include_tree/from_string_test.rb +0 -94
  109. data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -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,26 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class IncludeTree
6
- class FromStringTest < ActiveSupport::TestCase
7
- def test_simple_array
8
- input = [:comments, :author]
9
- actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
10
- assert(actual.key?(:author))
11
- assert(actual.key?(:comments))
12
- end
13
-
14
- def test_nested_array
15
- input = [:comments, posts: [:author, comments: [:author]]]
16
- actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
17
- assert(actual.key?(:posts))
18
- assert(actual[:posts].key?(:author))
19
- assert(actual[:posts].key?(:comments))
20
- assert(actual[:posts][:comments].key?(:author))
21
- assert(actual.key?(:comments))
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,94 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class IncludeTree
6
- class FromStringTest < ActiveSupport::TestCase
7
- def test_single_string
8
- input = 'author'
9
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
10
- assert(actual.key?(:author))
11
- end
12
-
13
- def test_multiple_strings
14
- input = 'author,comments'
15
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
16
- assert(actual.key?(:author))
17
- assert(actual.key?(:comments))
18
- end
19
-
20
- def test_multiple_strings_with_space
21
- input = 'author, comments'
22
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
23
- assert(actual.key?(:author))
24
- assert(actual.key?(:comments))
25
- end
26
-
27
- def test_nested_string
28
- input = 'posts.author'
29
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
30
- assert(actual.key?(:posts))
31
- assert(actual[:posts].key?(:author))
32
- end
33
-
34
- def test_multiple_nested_string
35
- input = 'posts.author,posts.comments.author,comments'
36
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
37
- assert(actual.key?(:posts))
38
- assert(actual[:posts].key?(:author))
39
- assert(actual[:posts].key?(:comments))
40
- assert(actual[:posts][:comments].key?(:author))
41
- assert(actual.key?(:comments))
42
- end
43
-
44
- def test_toplevel_star_string
45
- input = '*'
46
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
47
- assert(actual.key?(:comments))
48
- end
49
-
50
- def test_nested_star_string
51
- input = 'posts.*'
52
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
53
- assert(actual.key?(:posts))
54
- assert(actual[:posts].key?(:comments))
55
- end
56
-
57
- def test_nested_star_middle_string
58
- input = 'posts.*.author'
59
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
60
- assert(actual.key?(:posts))
61
- assert(actual[:posts].key?(:comments))
62
- assert(actual[:posts][:comments].key?(:author))
63
- refute(actual[:posts][:comments].key?(:unspecified))
64
- end
65
-
66
- def test_nested_star_lower_precedence_string
67
- input = 'posts.comments.author,posts.*'
68
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
69
- assert(actual.key?(:posts))
70
- assert(actual[:posts].key?(:comments))
71
- assert(actual[:posts][:comments].key?(:author))
72
- end
73
-
74
- def test_toplevel_double_star_string
75
- input = '**'
76
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
77
- assert(actual.key?(:posts))
78
- assert(actual[:posts].key?(:comments))
79
- assert(actual[:posts][:comments].key?(:posts))
80
- end
81
-
82
- def test_nested_double_star_string
83
- input = 'comments, posts.**'
84
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
85
- assert(actual.key?(:comments))
86
- refute(actual[:comments].key?(:author))
87
- assert(actual.key?(:posts))
88
- assert(actual[:posts].key?(:comments))
89
- assert(actual[:posts][:comments].key?(:posts))
90
- end
91
- end
92
- end
93
- end
94
- end
@@ -1,64 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class IncludeTree
6
- module Parsing
7
- class IncludeArgsToHashTest < MiniTest::Test
8
- def test_include_args_to_hash_from_symbol
9
- expected = { author: {} }
10
- input = :author
11
- actual = Parsing.include_args_to_hash(input)
12
-
13
- assert_equal(expected, actual)
14
- end
15
-
16
- def test_include_args_to_hash_from_array
17
- expected = { author: {}, comments: {} }
18
- input = [:author, :comments]
19
- actual = Parsing.include_args_to_hash(input)
20
-
21
- assert_equal(expected, actual)
22
- end
23
-
24
- def test_include_args_to_hash_from_nested_array
25
- expected = { author: {}, comments: { author: {} } }
26
- input = [:author, comments: [:author]]
27
- actual = Parsing.include_args_to_hash(input)
28
-
29
- assert_equal(expected, actual)
30
- end
31
-
32
- def test_include_args_to_hash_from_array_of_hashes
33
- expected = {
34
- author: {},
35
- blogs: { posts: { contributors: {} } },
36
- comments: { author: { blogs: { posts: {} } } }
37
- }
38
- input = [
39
- :author,
40
- blogs: [posts: :contributors],
41
- comments: { author: { blogs: :posts } }
42
- ]
43
- actual = Parsing.include_args_to_hash(input)
44
-
45
- assert_equal(expected, actual)
46
- end
47
-
48
- def test_array_of_string
49
- expected = {
50
- comments: { author: {}, attachment: {} }
51
- }
52
- input = [
53
- 'comments.author',
54
- 'comments.attachment'
55
- ]
56
- actual = Parsing.include_args_to_hash(input)
57
-
58
- assert_equal(expected, actual)
59
- end
60
- end
61
- end
62
- end
63
- end
64
- end