jat 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jat/attribute.rb +36 -4
  3. data/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads.rb +7 -3
  4. data/lib/jat/plugins/_activerecord_preloads/lib/preloader.rb +7 -13
  5. data/lib/jat/plugins/_lower_camel_case/_lower_camel_case.rb +32 -0
  6. data/lib/jat/plugins/_preloads/_preloads.rb +8 -2
  7. data/lib/jat/plugins/cache/cache.rb +9 -5
  8. data/lib/jat/plugins/json_api/json_api.rb +145 -105
  9. data/lib/jat/plugins/json_api/lib/fields_param_parser.rb +40 -0
  10. data/lib/jat/plugins/json_api/lib/include_param_parser.rb +84 -0
  11. data/lib/jat/plugins/json_api/lib/map.rb +92 -27
  12. data/lib/jat/plugins/json_api/lib/params/fields/validate.rb +8 -5
  13. data/lib/jat/plugins/json_api/lib/response.rb +84 -196
  14. data/lib/jat/plugins/json_api/lib/response_piece.rb +166 -0
  15. data/lib/jat/plugins/json_api_activerecord/json_api_activerecord.rb +31 -0
  16. data/lib/jat/plugins/{_json_api_activerecord → json_api_activerecord}/lib/preloads.rb +16 -24
  17. data/lib/jat/plugins/json_api_lower_camel_case/json_api_lower_camel_case.rb +30 -0
  18. data/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache.rb +54 -0
  19. data/lib/jat/plugins/json_api_validate_params/json_api_validate_params.rb +57 -0
  20. data/lib/jat/plugins/json_api_validate_params/lib/params_error.rb +6 -0
  21. data/lib/jat/plugins/json_api_validate_params/lib/validate_fields_param.rb +59 -0
  22. data/lib/jat/plugins/json_api_validate_params/lib/validate_include_param.rb +33 -0
  23. data/lib/jat/plugins/simple_api/lib/fields_param_parser.rb +97 -0
  24. data/lib/jat/plugins/simple_api/lib/map.rb +80 -10
  25. data/lib/jat/plugins/simple_api/lib/response.rb +78 -89
  26. data/lib/jat/plugins/simple_api/lib/response_piece.rb +84 -0
  27. data/lib/jat/plugins/simple_api/simple_api.rb +83 -24
  28. data/lib/jat/plugins/simple_api_activerecord/lib/preloads.rb +55 -0
  29. data/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord.rb +31 -0
  30. data/lib/jat/plugins/simple_api_lower_camel_case/simple_api_lower_camel_case.rb +30 -0
  31. data/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache.rb +48 -0
  32. data/lib/jat/plugins/simple_api_validate_params/lib/fields_error.rb +6 -0
  33. data/lib/jat/plugins/simple_api_validate_params/lib/validate_fields_param.rb +45 -0
  34. data/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params.rb +45 -0
  35. data/lib/jat/plugins/to_str/to_str.rb +10 -4
  36. data/lib/jat/plugins.rb +3 -16
  37. data/lib/jat.rb +28 -30
  38. data/test/lib/jat/attribute_test.rb +15 -5
  39. data/test/lib/jat/plugins/_activerecord_preloads/_activerecord_preloads_test.rb +34 -15
  40. data/test/lib/jat/plugins/_activerecord_preloads/lib/preloader_test.rb +10 -24
  41. data/test/lib/jat/plugins/_camel_lower/_camel_lower_test.rb +26 -0
  42. data/test/lib/jat/plugins/_preloads/lib/format_user_preloads_test.rb +1 -1
  43. data/test/lib/jat/plugins/_preloads/lib/preloads_with_path_test.rb +1 -1
  44. data/test/lib/jat/plugins/cache/cache_test.rb +11 -11
  45. data/test/lib/jat/plugins/json_api/json_api_test.rb +63 -47
  46. data/test/lib/jat/plugins/json_api/lib/{params/fields_test.rb → fields_param_parser_test.rb} +7 -6
  47. data/test/lib/jat/plugins/json_api/lib/{params/include_test.rb → include_param_parser_test.rb} +4 -4
  48. data/test/lib/jat/plugins/json_api/lib/map_test.rb +150 -79
  49. data/test/lib/jat/plugins/json_api/lib/response_test.rb +32 -32
  50. data/test/lib/jat/plugins/{_json_api_activerecord/_json_api_activerecord_test.rb → json_api_activerecord/json_api_activerecord_test.rb} +14 -5
  51. data/test/lib/jat/plugins/{_json_api_activerecord → json_api_activerecord}/lib/preloads_test.rb +11 -10
  52. data/test/lib/jat/plugins/json_api_camel_lower/json_api_camel_lower_test.rb +79 -0
  53. data/test/lib/jat/plugins/json_api_maps_cache/json_api_maps_cache_test.rb +107 -0
  54. data/test/lib/jat/plugins/json_api_validate_params/json_api_validate_params_test.rb +84 -0
  55. data/test/lib/jat/plugins/simple_api/lib/{params/parse_test.rb → fields_param_parser_test.rb} +10 -4
  56. data/test/lib/jat/plugins/simple_api/lib/map_test.rb +111 -34
  57. data/test/lib/jat/plugins/simple_api/lib/response_test.rb +80 -74
  58. data/test/lib/jat/plugins/simple_api/simple_api_test.rb +91 -25
  59. data/test/lib/jat/plugins/simple_api_activerecord/lib/preloads_test.rb +135 -0
  60. data/test/lib/jat/plugins/simple_api_activerecord/simple_api_activerecord_test.rb +38 -0
  61. data/test/lib/jat/plugins/simple_api_camel_lower/simple_api_camel_lower_test.rb +48 -0
  62. data/test/lib/jat/plugins/simple_api_maps_cache/simple_api_maps_cache_test.rb +95 -0
  63. data/test/lib/jat/plugins/simple_api_validate_params/simple_api_validate_params_test.rb +89 -0
  64. data/test/lib/jat/plugins/to_str/to_str_test.rb +3 -3
  65. data/test/lib/jat_test.rb +47 -24
  66. data/test/lib/plugin_test.rb +3 -3
  67. data/test/test_helper.rb +0 -3
  68. data/test/test_plugin.rb +9 -12
  69. metadata +60 -71
  70. data/CHANGELOG.md +0 -7
  71. data/README.md +0 -21
  72. data/jat.gemspec +0 -37
  73. data/lib/jat/plugins/_json_api_activerecord/_json_api_activerecord.rb +0 -22
  74. data/lib/jat/plugins/camel_lower/camel_lower.rb +0 -18
  75. data/lib/jat/plugins/json_api/lib/construct_traversal_map.rb +0 -91
  76. data/lib/jat/plugins/json_api/lib/presenters/document_links_presenter.rb +0 -48
  77. data/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter.rb +0 -48
  78. data/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter.rb +0 -48
  79. data/lib/jat/plugins/json_api/lib/presenters/links_presenter.rb +0 -48
  80. data/lib/jat/plugins/json_api/lib/presenters/meta_presenter.rb +0 -48
  81. data/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter.rb +0 -53
  82. data/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter.rb +0 -53
  83. data/lib/jat/plugins/json_api/lib/traversal_map.rb +0 -34
  84. data/lib/jat/plugins/simple_api/lib/construct_traversal_map.rb +0 -45
  85. data/lib/jat/plugins/simple_api/lib/params/parse.rb +0 -68
  86. data/lib/jat/presenter.rb +0 -51
  87. data/test/lib/jat/plugins/camel_lower/camel_lower_test.rb +0 -78
  88. data/test/lib/jat/plugins/json_api/lib/construct_traversal_map_test.rb +0 -119
  89. data/test/lib/jat/plugins/json_api/lib/params/fields/parse_test.rb +0 -24
  90. data/test/lib/jat/plugins/json_api/lib/params/fields/validate_test.rb +0 -47
  91. data/test/lib/jat/plugins/json_api/lib/params/include/parse_test.rb +0 -46
  92. data/test/lib/jat/plugins/json_api/lib/params/include/validate_test.rb +0 -51
  93. data/test/lib/jat/plugins/json_api/lib/presenters/document_links_presenter_test.rb +0 -69
  94. data/test/lib/jat/plugins/json_api/lib/presenters/document_meta_presenter_test.rb +0 -69
  95. data/test/lib/jat/plugins/json_api/lib/presenters/jsonapi_presenter_test.rb +0 -69
  96. data/test/lib/jat/plugins/json_api/lib/presenters/links_presenter_test.rb +0 -69
  97. data/test/lib/jat/plugins/json_api/lib/presenters/meta_presenter_test.rb +0 -69
  98. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_links_presenter_test.rb +0 -75
  99. data/test/lib/jat/plugins/json_api/lib/presenters/relationship_meta_presenter_test.rb +0 -75
  100. data/test/lib/jat/plugins/json_api/lib/traversal_map_test.rb +0 -58
  101. data/test/lib/jat/plugins/simple_api/lib/construct_traversal_map_test.rb +0 -100
  102. data/test/lib/jat/presenter_test.rb +0 -61
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Jat
4
- module Presenters
5
- class MetaPresenter
6
- module InstanceMethods
7
- # Presented object
8
- attr_reader :object
9
-
10
- # Presented context
11
- attr_reader :context
12
-
13
- def initialize(object, context)
14
- @object = object
15
- @context = context
16
- end
17
- end
18
-
19
- module ClassMethods
20
- attr_accessor :jat_class
21
-
22
- def inspect
23
- "#{jat_class.inspect}::Presenters::MetaPresenter"
24
- end
25
-
26
- def add_method(name, block)
27
- # Warning-free method redefinition
28
- remove_method(name) if method_defined?(name, false)
29
- define_method(name, &block_without_args(block))
30
- end
31
-
32
- private
33
-
34
- def block_without_args(block)
35
- case block.parameters.count
36
- when 0 then block
37
- when 1 then -> { instance_exec(object, &block) }
38
- when 2 then -> { instance_exec(object, context, &block) }
39
- else raise Error, "Invalid block arguments count"
40
- end
41
- end
42
- end
43
-
44
- extend ClassMethods
45
- include InstanceMethods
46
- end
47
- end
48
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Jat
4
- module Presenters
5
- class RelationshipLinksPresenter
6
- module InstanceMethods
7
- # Relationship parent object
8
- attr_reader :parent_object
9
-
10
- # Presented relationship
11
- attr_reader :object
12
-
13
- # Presented context
14
- attr_reader :context
15
-
16
- def initialize(parent_object, object, context)
17
- @parent_object = parent_object
18
- @object = object
19
- @context = context
20
- end
21
- end
22
-
23
- module ClassMethods
24
- attr_accessor :jat_class
25
-
26
- def inspect
27
- "#{jat_class.inspect}::Presenters::RelationshipLinksPresenter"
28
- end
29
-
30
- def add_method(name, block)
31
- # Warning-free method redefinition
32
- remove_method(name) if method_defined?(name, false)
33
- define_method(name, &block_without_args(block))
34
- end
35
-
36
- private
37
-
38
- def block_without_args(block)
39
- case block.parameters.count
40
- when 0 then block
41
- when 1 then -> { instance_exec(parent_object, &block) }
42
- when 2 then -> { instance_exec(parent_object, object, &block) }
43
- when 3 then -> { instance_exec(parent_object, object, context, &block) }
44
- else raise Error, "Invalid block arguments count"
45
- end
46
- end
47
- end
48
-
49
- extend ClassMethods
50
- include InstanceMethods
51
- end
52
- end
53
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Jat
4
- module Presenters
5
- class RelationshipMetaPresenter
6
- module InstanceMethods
7
- # Relationship parent object
8
- attr_reader :parent_object
9
-
10
- # Presented relationship
11
- attr_reader :object
12
-
13
- # Presented context
14
- attr_reader :context
15
-
16
- def initialize(parent_object, object, context)
17
- @parent_object = parent_object
18
- @object = object
19
- @context = context
20
- end
21
- end
22
-
23
- module ClassMethods
24
- attr_accessor :jat_class
25
-
26
- def inspect
27
- "#{jat_class.inspect}::Presenters::RelationshipMetaPresenter"
28
- end
29
-
30
- def add_method(name, block)
31
- # Warning-free method redefinition
32
- remove_method(name) if method_defined?(name, false)
33
- define_method(name, &block_without_args(block))
34
- end
35
-
36
- private
37
-
38
- def block_without_args(block)
39
- case block.parameters.count
40
- when 0 then block
41
- when 1 then -> { instance_exec(parent_object, &block) }
42
- when 2 then -> { instance_exec(parent_object, object, &block) }
43
- when 3 then -> { instance_exec(parent_object, object, context, &block) }
44
- else raise Error, "Invalid block arguments count"
45
- end
46
- end
47
- end
48
-
49
- extend ClassMethods
50
- include InstanceMethods
51
- end
52
- end
53
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "./construct_traversal_map"
4
- require_relative "./map"
5
-
6
- class Jat
7
- module Plugins
8
- module JsonApi
9
- class TraversalMap
10
- module InstanceMethods
11
- attr_reader :jat
12
-
13
- def initialize(jat)
14
- @jat = jat
15
- end
16
-
17
- def full
18
- @full ||= ConstructTraversalMap.new(jat.class, :all).to_h
19
- end
20
-
21
- def exposed
22
- @exposed ||= ConstructTraversalMap.new(jat.class, :exposed).to_h
23
- end
24
-
25
- def current
26
- @current ||= Map.call(jat)
27
- end
28
- end
29
-
30
- include InstanceMethods
31
- end
32
- end
33
- end
34
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Combines all serializer and its relations exposed fields into one hash.
4
- class Jat
5
- module Plugins
6
- module SimpleApi
7
- class ConstructTraversalMap
8
- attr_reader :jat_class, :exposed, :manually_exposed
9
-
10
- def initialize(jat_class, exposed, manually_exposed: nil)
11
- @jat_class = jat_class
12
- @exposed = exposed.to_sym
13
- @manually_exposed = manually_exposed || {}
14
- end
15
-
16
- def to_h
17
- jat_class.attributes.each_with_object({}) do |(name, attribute), result|
18
- next unless expose?(attribute)
19
-
20
- result[name] =
21
- if attribute.relation?
22
- self.class.new(attribute.serializer.call, exposed, manually_exposed: manually_exposed[name]).to_h
23
- else
24
- {}
25
- end
26
- end
27
- end
28
-
29
- private
30
-
31
- def expose?(attribute)
32
- case exposed
33
- when :all then true
34
- when :none then manually_exposed?(attribute)
35
- else attribute.exposed? || manually_exposed?(attribute)
36
- end
37
- end
38
-
39
- def manually_exposed?(attribute)
40
- manually_exposed.include?(attribute.name)
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Jat
4
- module Plugins
5
- module SimpleApi
6
- module Params
7
- class Parse
8
- module InstanceMethods
9
- COMMA = ","
10
- OPEN_BRACKET = "("
11
- CLOSE_BRACKET = ")"
12
-
13
- def initialize(fields)
14
- @fields = fields
15
- @map = []
16
- @res = {}
17
- end
18
-
19
- # user => { user: {} }
20
- # user(id) => { user: { id: true } }
21
- # user(id, name) => { user: { id: true, name: true } }
22
- # user, comments => { user: {}, comments: {} }
23
- # user(comments(text)) => { user: {}, comments: {} }
24
- def parse
25
- return {} unless fields
26
-
27
- current_attr = nil
28
-
29
- fields.each_char do |char|
30
- case char
31
- when COMMA
32
- add_attribute(current_attr)
33
- current_attr = nil
34
- when CLOSE_BRACKET
35
- add_attribute(current_attr)
36
- map.pop
37
- current_attr = nil
38
- when OPEN_BRACKET
39
- add_attribute(current_attr)
40
- map << current_attr.to_sym if current_attr
41
- current_attr = nil
42
- else
43
- current_attr = current_attr ? current_attr.insert(-1, char) : char
44
- end
45
- end
46
- add_attribute(current_attr)
47
-
48
- res
49
- end
50
-
51
- private
52
-
53
- attr_reader :fields, :map, :res
54
-
55
- def add_attribute(current_attr)
56
- return unless current_attr
57
-
58
- current_resource = map.empty? ? res : res.dig(*map)
59
- current_resource[current_attr.to_sym] = {}
60
- end
61
- end
62
-
63
- include InstanceMethods
64
- end
65
- end
66
- end
67
- end
68
- end
data/lib/jat/presenter.rb DELETED
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Jat
4
- # Core class that encapsulates attributes presentation logic
5
- class Presenter
6
- module InstanceMethods
7
- # Presented object
8
- attr_reader :object
9
-
10
- # Presented context
11
- attr_reader :context
12
-
13
- def initialize(object, context)
14
- @object = object
15
- @context = context
16
- end
17
- end
18
-
19
- module ClassMethods
20
- # Returns the Jat class that this presenter class is namespaced under.
21
- attr_accessor :jat_class
22
-
23
- # Since Presenter is anonymously subclassed when Jat is subclassed,
24
- # and then assigned to a constant of the Jat subclass, make inspect
25
- # reflect the likely name for the class.
26
- def inspect
27
- "#{jat_class.inspect}::Presenter"
28
- end
29
-
30
- def add_method(name, block)
31
- # Warning-free method redefinition
32
- remove_method(name) if method_defined?(name, false)
33
- define_method(name, &block_without_args(block))
34
- end
35
-
36
- private
37
-
38
- def block_without_args(block)
39
- case block.parameters.count
40
- when 0 then block
41
- when 1 then -> { instance_exec(object, &block) }
42
- when 2 then -> { instance_exec(object, context, &block) }
43
- else raise Error, "Invalid block arguments count"
44
- end
45
- end
46
- end
47
-
48
- extend ClassMethods
49
- include InstanceMethods
50
- end
51
- end
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::CamelLower" do
6
- let(:jat_class) do
7
- new_class = Class.new(Jat)
8
- new_class.plugin(:camel_lower)
9
- new_class
10
- end
11
-
12
- describe "Attribute" do
13
- describe "#name" do
14
- it "returns name in camel_lower case" do
15
- attribute = jat_class.attribute(:foo)
16
- assert :foo, attribute.name
17
-
18
- attribute = jat_class.attribute(:foo_bar)
19
- assert :fooBar, attribute.name
20
-
21
- attribute = jat_class.attribute(:foo_bar_bazz)
22
- assert :fooBarBazz, attribute.name
23
- end
24
- end
25
- end
26
-
27
- describe "Test responses with simple_api plugin" do
28
- before { jat_class.plugin(:simple_api) }
29
-
30
- it "returns attributes in camelLower case" do
31
- jat_class.attribute(:foo_bar) { 1 }
32
-
33
- response = jat_class.to_h(true)
34
- assert_equal({fooBar: 1}, response)
35
- end
36
-
37
- it "accepts fields in camelLower format" do
38
- jat_class.attribute(:foo_bar, exposed: false) { 1 }
39
-
40
- response = jat_class.to_h(true, params: {fields: "fooBar"})
41
- assert_equal({fooBar: 1}, response)
42
- end
43
- end
44
-
45
- describe "Test responses with json_api plugin" do
46
- before do
47
- jat_class.plugin(:json_api)
48
- jat_class.type :foo
49
- jat_class.attribute(:id) { object }
50
- end
51
-
52
- it "returns attributes in camelLower case" do
53
- jat_class.attribute(:foo_bar) { 1 }
54
-
55
- assert_equal({fooBar: 1}, jat_class.to_h(true).dig(:data, :attributes))
56
- end
57
-
58
- it "accepts `fields` in camelLower format" do
59
- jat_class.attribute(:foo_bar, exposed: false) { 1 }
60
-
61
- response = jat_class.to_h(true, params: {fields: {foo: "fooBar"}})
62
- assert_equal({fooBar: 1}, response.dig(:data, :attributes))
63
- end
64
-
65
- it "accepts `include` in camelLower format" do
66
- new_serializer = Class.new(Jat)
67
- new_serializer.plugin(:json_api)
68
- new_serializer.type :new
69
- new_serializer.attribute(:id) { object }
70
-
71
- jat_class.relationship(:foo_bar, serializer: new_serializer) { 1 }
72
-
73
- response = jat_class.to_h(true, params: {include: "fooBar"})
74
- response_relationships = response.dig(:data, :relationships).keys
75
- assert_includes(response_relationships, :fooBar)
76
- end
77
- end
78
- end
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::ConstructTraversalMap" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:described_class) { Jat::Plugins::JsonApi::ConstructTraversalMap }
9
- let(:base_class) { Class.new(Jat) { plugin :json_api } }
10
-
11
- let(:a) do
12
- ser = Class.new(base_class)
13
- ser.type :a
14
-
15
- ser.attribute :a1
16
- ser.attribute :a2
17
- ser.attribute :a3, exposed: false
18
-
19
- ser.relationship :b, serializer: b
20
- ser.relationship :c, serializer: c
21
- ser.relationship :d, serializer: d, exposed: true
22
- ser
23
- end
24
-
25
- let(:b) do
26
- ser = Class.new(base_class)
27
- ser.type :b
28
- ser.attribute :b1
29
- ser.attribute :b2
30
- ser.attribute :b3, exposed: false
31
- ser
32
- end
33
-
34
- let(:c) do
35
- ser = Class.new(base_class)
36
- ser.type :c
37
- ser.attribute :c1
38
- ser.attribute :c2
39
- ser.attribute :c3, exposed: false
40
- ser
41
- end
42
-
43
- let(:d) do
44
- ser = Class.new(base_class)
45
- ser.type :d
46
- ser.attribute :d1
47
- ser.attribute :d2
48
- ser.attribute :d3, exposed: false
49
- ser
50
- end
51
-
52
- it "returns all attributes" do
53
- result = described_class.new(a, :all).to_h
54
- expected_result = {
55
- a: {serializer: a, attributes: %i[a1 a2 a3], relationships: %i[b c d]},
56
- b: {serializer: b, attributes: %i[b1 b2 b3], relationships: []},
57
- c: {serializer: c, attributes: %i[c1 c2 c3], relationships: []},
58
- d: {serializer: d, attributes: %i[d1 d2 d3], relationships: []}
59
- }
60
-
61
- assert_equal expected_result, result
62
- end
63
-
64
- it "returns exposed attributes" do
65
- result = described_class.new(a, :exposed).to_h
66
- expected_result = {
67
- a: {serializer: a, attributes: %i[a1 a2], relationships: %i[d]},
68
- d: {serializer: d, attributes: %i[d1 d2], relationships: []}
69
- }
70
-
71
- assert_equal expected_result, result
72
- end
73
-
74
- it "returns only manually exposed per-type attributes or exposed by default when no manual type provided" do
75
- exposed = {
76
- a: %i[a2 a3 c d],
77
- c: %i[c2 c3],
78
- d: %i[d2 d3]
79
- }
80
- result = described_class.new(a, :manual, manually_exposed: exposed).to_h
81
- expected_result = {
82
- a: {serializer: a, attributes: %i[a2 a3], relationships: %i[c d]},
83
- c: {serializer: c, attributes: %i[c2 c3], relationships: []},
84
- d: {serializer: d, attributes: %i[d2 d3], relationships: []}
85
- }
86
-
87
- assert_equal expected_result, result
88
- end
89
-
90
- it "returns manually exposed per-type attributes or exposed by default when no manual type provided" do
91
- exposed = {
92
- a: %i[a2 a3 b c],
93
- c: %i[c2 c3]
94
- }
95
- result = described_class.new(a, :manual, manually_exposed: exposed).to_h
96
- expected_result = {
97
- a: {serializer: a, attributes: %i[a2 a3], relationships: %i[b c]},
98
- b: {serializer: b, attributes: %i[b1 b2], relationships: []},
99
- c: {serializer: c, attributes: %i[c2 c3], relationships: []}
100
- }
101
-
102
- assert_equal expected_result, result
103
- end
104
-
105
- it "returns combined auto-exposed and manualy exposed attributes" do
106
- exposed = {
107
- a: %i[c],
108
- c: %i[c3]
109
- }
110
- result = described_class.new(a, :exposed, manually_exposed: exposed).to_h
111
- expected_result = {
112
- a: {serializer: a, attributes: %i[a1 a2], relationships: %i[c d]},
113
- c: {serializer: c, attributes: %i[c1 c2 c3], relationships: []},
114
- d: {serializer: d, attributes: %i[d1 d2], relationships: []}
115
- }
116
-
117
- assert_equal expected_result, result
118
- end
119
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::Params::Fields::Parse" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:described_class) { Jat::Plugins::JsonApi::Params::Fields::Parse }
9
-
10
- it "returns empty hash when param not provided" do
11
- result = described_class.call(nil)
12
- assert_equal({}, result)
13
- end
14
-
15
- it "returns hash with parsed keys" do
16
- result = described_class.call(a: "a1,a2", b: "b1")
17
- assert_equal({a: %i[a1 a2], b: %i[b1]}, result)
18
- end
19
-
20
- it "symbolizes types" do
21
- result = described_class.call("a" => "a1,a2", "b" => "b1")
22
- assert_equal({a: %i[a1 a2], b: %i[b1]}, result)
23
- end
24
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::Params::Fields::Validate" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:described_class) { Jat::Plugins::JsonApi::Params::Fields::Validate }
9
- let(:base_class) { Class.new(Jat) { plugin :json_api } }
10
- let(:a_serializer) { Class.new(base_class) }
11
- let(:b_serializer) { Class.new(base_class) }
12
-
13
- before do
14
- ser = a_serializer
15
- ser.type :a
16
- ser.attribute :a1
17
- ser.relationship :a2, serializer: b_serializer
18
-
19
- ser = b_serializer
20
- ser.type :b
21
- ser.attribute :b1
22
- ser.relationship :b2, serializer: a_serializer
23
- end
24
-
25
- it "does not raises when serializer has all requested keys" do
26
- assert described_class.call(a_serializer.allocate, a: %i[a1 a2], b: %i[b1 b2])
27
- end
28
-
29
- it "does not raises when requested only fields for nested serializer" do
30
- assert described_class.call(a_serializer.allocate, b: %i[b1 b2])
31
- end
32
-
33
- it "raises error when some type can not be in response" do
34
- error = assert_raises(Jat::Error) { described_class.call(a_serializer.allocate, a: %i[a1 a2], foo: %i[b1 b2]) }
35
- assert_equal "#{a_serializer} and its children have no requested type `foo`", error.message
36
- end
37
-
38
- it "raises error when some key is not present in main serializer" do
39
- error = assert_raises(Jat::Error) { described_class.call(a_serializer.allocate, a: %i[b1]) }
40
- assert_equal "#{a_serializer} has no requested attribute or relationship `b1`", error.message
41
- end
42
-
43
- it "raises error when some key is not present in nested serializer" do
44
- error = assert_raises(Jat::Error) { described_class.call(a_serializer.allocate, a: %i[a1], b: %i[a1]) }
45
- assert_equal "#{b_serializer} has no requested attribute or relationship `a1`", error.message
46
- end
47
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- describe "Jat::Plugins::JsonApi::Params::Include::Parse" do
6
- before { Jat::Plugins.load_plugin(:json_api) }
7
-
8
- let(:described_class) { Jat::Plugins::JsonApi::Params::Include::Parse }
9
-
10
- it "returns empty hash when param not provided" do
11
- result = described_class.call(nil)
12
-
13
- assert_equal({}, result)
14
- end
15
-
16
- it "returns hash when single element" do
17
- result = described_class.call("foo")
18
-
19
- assert_equal({foo: {}}, result)
20
- end
21
-
22
- it "returns hash when multiple elements" do
23
- result = described_class.call("foo,bar,bazz")
24
-
25
- assert_equal({foo: {}, bar: {}, bazz: {}}, result)
26
- end
27
-
28
- it "returns hash when nested elements" do
29
- result = described_class.call("foo.bar.bazz")
30
-
31
- assert_equal({foo: {bar: {bazz: {}}}}, result)
32
- end
33
-
34
- it "returns hash when multiple nested elements" do
35
- result = described_class.call("foo,bar.bazz,bar.bazzz,test.test1.test2,test.test1.test3")
36
-
37
- assert_equal(
38
- {
39
- foo: {},
40
- bar: {bazz: {}, bazzz: {}},
41
- test: {test1: {test2: {}, test3: {}}}
42
- },
43
- result
44
- )
45
- end
46
- end