serega 0.51.1 → 0.52.1

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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +49 -21
  3. data/VERSION +1 -1
  4. data/docs/README.md +42 -0
  5. data/docs/attributes.md +248 -0
  6. data/docs/batch-loading.md +207 -0
  7. data/docs/camel-case.md +86 -0
  8. data/docs/conditional-attributes.md +165 -0
  9. data/docs/configuration.md +47 -0
  10. data/docs/context.md +34 -0
  11. data/docs/errors.md +38 -0
  12. data/docs/formatters.md +262 -0
  13. data/docs/hash-records.md +93 -0
  14. data/docs/migrating-from-root-and-metadata.md +50 -0
  15. data/docs/preloads.md +125 -0
  16. data/docs/prepare-initial-objects.md +72 -0
  17. data/docs/presenter.md +87 -0
  18. data/docs/presets.md +174 -0
  19. data/docs/ractors.md +54 -0
  20. data/docs/relations.md +233 -0
  21. data/docs/responses-rails.md +287 -0
  22. data/docs/responses.md +293 -0
  23. data/docs/root-and-metadata.md +110 -0
  24. data/docs/selecting-fields.md +164 -0
  25. data/docs/serializing.md +113 -0
  26. data/docs/testing.md +230 -0
  27. data/lib/serega/attribute_normalizer.rb +26 -8
  28. data/lib/serega/attribute_normalizers/block.rb +3 -3
  29. data/lib/serega/config.rb +28 -13
  30. data/lib/serega/errors.rb +2 -2
  31. data/lib/serega/plan/cache.rb +13 -29
  32. data/lib/serega/plan/per_ractor_cache.rb +2 -2
  33. data/lib/serega/plan/point.rb +13 -2
  34. data/lib/serega/plan.rb +1 -1
  35. data/lib/serega/plugins/context_metadata/context_metadata.rb +5 -1
  36. data/lib/serega/plugins/metadata/metadata.rb +4 -0
  37. data/lib/serega/plugins/root/root.rb +14 -10
  38. data/lib/serega/plugins.rb +13 -0
  39. data/lib/serega/result_code/relation_point_code.rb +3 -2
  40. data/lib/serega/source_group.rb +1 -1
  41. data/lib/serega/validations/attribute/check_block.rb +4 -7
  42. data/lib/serega/validations/attribute/check_opt_hash_access.rb +2 -3
  43. data/lib/serega/validations/check_batch_loader_params.rb +2 -1
  44. data/lib/serega.rb +30 -26
  45. data/sig/serega.rbs +10 -8
  46. metadata +24 -1
data/docs/testing.md ADDED
@@ -0,0 +1,230 @@
1
+ # Testing Serializers
2
+
3
+ Serega has no testing DSL and no test helpers. A serializer is a Ruby class,
4
+ and its specs call `to_h` like the app does and compare the result.
5
+
6
+ This guide is an example for inspiration: the way the gem author tests
7
+ serializers, not a rule. Keep what fits your app. The examples use RSpec and
8
+ FactoryBot, and the ideas work with any test tools.
9
+
10
+ The examples test this serializer:
11
+
12
+ ```ruby
13
+ class UserSerializer < Serega
14
+ config.hide_by_default = :auto
15
+
16
+ attribute :first_name
17
+ attribute :email, if: proc { |user, ctx| user == ctx[:current_user] }
18
+ attribute :link, value: UserLink
19
+ attribute :followers_count, batch: FollowersCount, default: 0
20
+ end
21
+ ```
22
+
23
+ ## Table of Contents
24
+
25
+ <!-- toc -->
26
+
27
+ - [Testing one attribute at a time](#testing-one-attribute-at-a-time)
28
+ - [Testing the fields serialized by default](#testing-the-fields-serialized-by-default)
29
+ - [Testing attributes that read the context](#testing-attributes-that-read-the-context)
30
+ - [Moving complex values to their own classes](#moving-complex-values-to-their-own-classes)
31
+ - [Testing batch loaders](#testing-batch-loaders)
32
+ - [Counting queries with ActiveRecord](#counting-queries-with-activerecord)
33
+
34
+ <!-- tocstop -->
35
+
36
+ ## Testing one attribute at a time
37
+
38
+ `selection` holds the [field selection](selecting-fields.md) options.
39
+ `{only: :first_name}` serializes one attribute, so each example builds only the
40
+ data that attribute needs:
41
+
42
+ ```ruby
43
+ RSpec.describe UserSerializer do
44
+ subject(:result) { described_class.to_h(user, **selection, context: context) }
45
+
46
+ let(:user) { build_stubbed(:user, first_name: "Felonious", username: "gru", email: "gru@example.com") }
47
+ let(:context) { {} }
48
+
49
+ context "#first_name" do
50
+ let(:selection) { {only: :first_name} }
51
+
52
+ it "serializes the first name" do
53
+ expect(result).to eq(first_name: "Felonious")
54
+ end
55
+ end
56
+ end
57
+ ```
58
+
59
+ The user comes from `build_stubbed` and is never saved: a serializer only reads
60
+ the objects it gets, and such specs run fast. `build`, `User.new` or a `Struct`
61
+ works the same way.
62
+
63
+ ## Testing the fields serialized by default
64
+
65
+ An empty `selection` serializes the fields a response has by default. With
66
+ `hide_by_default = :auto`, it catches a batch or a relation that starts to load
67
+ for every response:
68
+
69
+ ```ruby
70
+ describe "default fields" do
71
+ let(:selection) { {} }
72
+
73
+ it "skips the batch-loaded fields" do
74
+ expect(result.keys).to eq(%i[first_name link])
75
+ end
76
+ end
77
+ ```
78
+
79
+ ## Testing attributes that read the context
80
+
81
+ A context per value of the [context](context.md) shows each outcome:
82
+
83
+ ```ruby
84
+ context "#email" do
85
+ let(:selection) { {only: :email} }
86
+
87
+ context "when the user is the current user" do
88
+ let(:context) { {current_user: user} }
89
+
90
+ it "serializes the email" do
91
+ expect(result).to eq(email: "gru@example.com")
92
+ end
93
+ end
94
+
95
+ context "when the user is another user" do
96
+ let(:context) { {current_user: build_stubbed(:user)} }
97
+
98
+ it "skips the email" do
99
+ expect(result).to eq({})
100
+ end
101
+ end
102
+ end
103
+ ```
104
+
105
+ ## Moving complex values to their own classes
106
+
107
+ When a `value:` or `batch:` proc grows past a line or two, it can move to its
108
+ own class with its own spec. `:value` and `:batch` accept any object that
109
+ responds to `#call`:
110
+
111
+ ```ruby
112
+ class UserLink
113
+ def self.call(user)
114
+ return unless user.username
115
+
116
+ "https://example.com/@#{user.username}"
117
+ end
118
+ end
119
+ ```
120
+
121
+ The class spec covers the edge cases:
122
+
123
+ ```ruby
124
+ RSpec.describe UserLink do
125
+ subject(:link) { described_class.call(user) }
126
+
127
+ let(:user) { build_stubbed(:user, username: username) }
128
+
129
+ context "when the user has a username" do
130
+ let(:username) { "gru" }
131
+
132
+ it "links to the user page" do
133
+ expect(link).to eq("https://example.com/@gru")
134
+ end
135
+ end
136
+
137
+ context "when the user has no username" do
138
+ let(:username) { nil }
139
+
140
+ it "returns nil" do
141
+ expect(link).to be_nil
142
+ end
143
+ end
144
+ end
145
+ ```
146
+
147
+ The serializer spec then needs one example that the attribute uses the class:
148
+
149
+ ```ruby
150
+ context "#link" do
151
+ let(:selection) { {only: :link} }
152
+
153
+ it "serializes the user link" do
154
+ expect(result).to eq(link: "https://example.com/@gru")
155
+ end
156
+ end
157
+ ```
158
+
159
+ ## Testing batch loaders
160
+
161
+ A loader class runs a query, so its spec saves records. A record of a user
162
+ that is not passed to the loader shows that the loader reads only the given
163
+ users:
164
+
165
+ ```ruby
166
+ class FollowersCount
167
+ def self.call(users)
168
+ Follow.where(user: users).group(:user_id).count
169
+ end
170
+ end
171
+
172
+ RSpec.describe FollowersCount do
173
+ subject(:counts) { described_class.call(users) }
174
+
175
+ let(:gru) { create(:user) }
176
+ let(:lucy) { create(:user) }
177
+ let(:margo) { create(:user) }
178
+ let(:users) { [gru, lucy] }
179
+
180
+ before do
181
+ create(:follow, user: gru, follower: lucy)
182
+ create(:follow, user: gru, follower: margo)
183
+ create(:follow, user: margo, follower: gru)
184
+ end
185
+
186
+ context "when some users have followers" do
187
+ it "counts the followers of each user" do
188
+ expect(counts).to eq(gru.id => 2)
189
+ end
190
+ end
191
+ end
192
+ ```
193
+
194
+ ## Counting queries with ActiveRecord
195
+
196
+ A spec can count the SQL queries of a serialization, to see that
197
+ [preloads](preloads.md) and [batch loaders](batch-loading.md) keep one query
198
+ per relation. A helper in `spec/support` can count them for every spec. It
199
+ skips the schema queries ActiveRecord runs when it first loads a model:
200
+
201
+ ```ruby
202
+ # spec/support/count_queries.rb
203
+ module CountQueries
204
+ def count_queries(&block)
205
+ count = 0
206
+ callback = ->(*, payload) { count += 1 unless payload[:name] == "SCHEMA" }
207
+ ActiveSupport::Notifications.subscribed(callback, "sql.active_record", &block)
208
+ count
209
+ end
210
+ end
211
+
212
+ RSpec.configure do |config|
213
+ config.include CountQueries
214
+ end
215
+ ```
216
+
217
+ The spec saves records, so the queries run against real data:
218
+
219
+ ```ruby
220
+ context "with several users" do
221
+ let!(:users) { create_list(:user, 2) }
222
+ let(:selection) { {only: %i[first_name followers_count]} }
223
+
224
+ it "runs one query for the followers of all users" do
225
+ queries = count_queries { described_class.to_h(users, **selection) }
226
+
227
+ expect(queries).to eq(1)
228
+ end
229
+ end
230
+ ```
@@ -5,17 +5,35 @@ class Serega
5
5
  # Prepares provided attribute options
6
6
  #
7
7
  class SeregaAttributeNormalizer # :nodoc:
8
+ # Value of a lazy instance variable that is not prepared yet. The
9
+ # prepared value can be nil or false.
10
+ UNPREPARED = Object.new.freeze
11
+ private_constant :UNPREPARED
12
+
8
13
  # Attribute initial params
9
14
  attr_reader :init_name, :init_opts, :init_block
10
15
 
11
- # Instantiates attribute options normalizer
16
+ # Instantiates attribute options normalizer. The readers prepare their
17
+ # values lazily and call each other in an order that depends on the
18
+ # options, thus every instance variable is declared here, and objects
19
+ # of this class share one shape.
12
20
  def initialize(serializer_class, initials)
13
21
  @serializer_class = serializer_class
14
22
  @init_name = initials[:name]
15
23
  @init_opts = initials[:opts]
16
24
  @init_block = initials[:block]
17
- @method_name = nil
25
+ @name = nil
18
26
  @key = nil
27
+ @method_name = nil
28
+ @value_block = nil
29
+ @value_block_signature = nil
30
+ @batch_loaders = nil
31
+ @hide = UNPREPARED
32
+ @many = UNPREPARED
33
+ @serializer = UNPREPARED
34
+ @default = UNPREPARED
35
+ @preloads = UNPREPARED
36
+ @batch_option = UNPREPARED
19
37
  end
20
38
 
21
39
  # Attribute name, the serialized key as a Symbol
@@ -46,21 +64,21 @@ class Serega
46
64
 
47
65
  # Shows if attribute is specified to be hidden
48
66
  def hide
49
- return @hide if instance_variable_defined?(:@hide)
67
+ return @hide unless UNPREPARED.equal?(@hide)
50
68
 
51
69
  @hide = prepare_hide
52
70
  end
53
71
 
54
72
  # Shows if attribute is specified to be a one-to-many relationship
55
73
  def many
56
- return @many if instance_variable_defined?(:@many)
74
+ return @many unless UNPREPARED.equal?(@many)
57
75
 
58
76
  @many = prepare_many
59
77
  end
60
78
 
61
79
  # Shows specified attribute serializer
62
80
  def serializer
63
- return @serializer if instance_variable_defined?(:@serializer)
81
+ return @serializer unless UNPREPARED.equal?(@serializer)
64
82
 
65
83
  @serializer = prepare_serializer
66
84
  end
@@ -78,14 +96,14 @@ class Serega
78
96
  #
79
97
  # When custom :default is not specified, we set empty array as default when `many: true` specified
80
98
  def default
81
- return @default if instance_variable_defined?(:@default)
99
+ return @default unless UNPREPARED.equal?(@default)
82
100
 
83
101
  @default = prepare_default
84
102
  end
85
103
 
86
104
  # Preloads declared for this attribute, passed through as provided.
87
105
  def preloads
88
- return @preloads if instance_variable_defined?(:@preloads)
106
+ return @preloads unless UNPREPARED.equal?(@preloads)
89
107
 
90
108
  @preloads = prepare_preloads
91
109
  end
@@ -221,7 +239,7 @@ class Serega
221
239
 
222
240
  # The parsed :batch option, `nil` without it
223
241
  def batch_option
224
- return @batch_option if instance_variable_defined?(:@batch_option)
242
+ return @batch_option unless UNPREPARED.equal?(@batch_option)
225
243
 
226
244
  batch = init_opts[:batch]
227
245
  @batch_option = batch && AttributeNormalizers::Batch.new(batch, name)
@@ -71,9 +71,9 @@ class Serega
71
71
  return base_serializer if base_serializer
72
72
 
73
73
  raise SeregaError,
74
- "Attribute block requires a base serializer for the nested serializer." \
75
- " Provide the `base_serializer: <SerializerClass>` attribute option" \
76
- " or set `config.base_serializer = <SerializerClass>`"
74
+ "Attribute block requires a base serializer for the nested serializer\n" \
75
+ "(hint: provide the `base_serializer: <BaseSerializer>` attribute option" \
76
+ " or set `config.base_serializer = <BaseSerializer>`)"
77
77
  end
78
78
  end
79
79
  end
data/lib/serega/config.rb CHANGED
@@ -23,7 +23,7 @@ class Serega
23
23
  serialize_keys: %i[context many].freeze,
24
24
  check_attribute_name: true,
25
25
  require_many: false,
26
- check_selection: true,
26
+ many: nil,
27
27
  delegate_default_allow_nil: false,
28
28
  max_cached_plans_per_serializer_count: 20,
29
29
  depth_limit: nil,
@@ -34,7 +34,8 @@ class Serega
34
34
  key_transform: nil,
35
35
  batch_id_option: :id,
36
36
  base_serializer: nil,
37
- hash_access: {default_mode: :symbol, default_allow_missing_key: false}
37
+ hash_access: {default_mode: :symbol, default_allow_missing_key: false},
38
+ deprecation_warnings: true
38
39
  }.freeze
39
40
  # :nocov:
40
41
 
@@ -61,17 +62,6 @@ class Serega
61
62
  opts.fetch(:serialize_keys)
62
63
  end
63
64
 
64
- # Whether unknown fields of `only:`, `except:` and `with:` raise
65
- # `Serega::AttributeNotExist`. `true` by default.
66
- def check_selection
67
- opts.fetch(:check_selection)
68
- end
69
-
70
- # Sets #check_selection
71
- def check_selection=(value)
72
- opts[:check_selection] = checked_boolean(value)
73
- end
74
-
75
65
  # Default `allow_nil:` of the `:delegate` attribute option. `false` by
76
66
  # default.
77
67
  def delegate_default_allow_nil
@@ -144,6 +134,17 @@ class Serega
144
134
  opts[:compact] = checked_boolean(value)
145
135
  end
146
136
 
137
+ # Whether loading a deprecated plugin prints a warning. `true` by
138
+ # default.
139
+ def deprecation_warnings
140
+ opts.fetch(:deprecation_warnings)
141
+ end
142
+
143
+ # Sets #deprecation_warnings
144
+ def deprecation_warnings=(value)
145
+ opts[:deprecation_warnings] = checked_boolean(value)
146
+ end
147
+
147
148
  # How attribute names become serialized keys: `nil` (names as they are,
148
149
  # the default), `:camel_case` (`first_name` gives `firstName`), or a
149
150
  # callable that receives the name Symbol and returns the key. A String
@@ -239,6 +240,20 @@ class Serega
239
240
  opts[:require_many] = checked_boolean(value)
240
241
  end
241
242
 
243
+ # How a call without the `:many` option serializes the object: `true` as a
244
+ # list, `false` as one object. `nil`, the default, serializes an
245
+ # `Enumerable` that is not a `Hash` or `Struct` as a list.
246
+ def many
247
+ opts.fetch(:many)
248
+ end
249
+
250
+ # Sets #many
251
+ def many=(value)
252
+ raise SeregaError, "Must have true, false or nil, #{value.inspect} provided" unless value.nil? || value == true || value == false
253
+
254
+ opts[:many] = value
255
+ end
256
+
242
257
  # Method that returns the id of an object for batch loaders, `:id` by
243
258
  # default
244
259
  def batch_id_option
data/lib/serega/errors.rb CHANGED
@@ -36,7 +36,7 @@ class Serega
36
36
  # # => Serega::PresetNotExist: Preset :admin is not defined in UserSerializer
37
37
  # ```
38
38
  #
39
- # See [Presets](https://github.com/andreyruby/serega/blob/master/docs/selecting-fields.md#presets).
39
+ # See [Presets](https://github.com/andreyruby/serega/blob/master/docs/presets.md).
40
40
  class PresetNotExist < SeregaError
41
41
  # Serializer that has no such preset
42
42
  attr_reader :serializer
@@ -60,7 +60,7 @@ class Serega
60
60
  # # => Serega::DepthLimitError: Depth limit was exceeded
61
61
  # ```
62
62
  #
63
- # See [Depth limit](https://github.com/andreyruby/serega/blob/master/docs/configuration.md#depth-limit).
63
+ # See [Limiting nesting depth](https://github.com/andreyruby/serega/blob/master/docs/relations.md#limiting-nesting-depth).
64
64
  class DepthLimitError < SeregaError
65
65
  # Path that exceeds the limit or closes the cycle, and on the next line a hint:
66
66
  #
@@ -9,21 +9,12 @@ class Serega
9
9
  # the plans.
10
10
  #
11
11
  class Cache # :nodoc:
12
- # Cached plan, whether its parameters were validated, and the
13
- # parameters as provided
14
- CACHED_PLAN = Struct.new(:plan, :validated, :param1, :param2, :param3, :param4) do
12
+ # Cached plan and the parameters as provided
13
+ CACHED_PLAN = Struct.new(:plan, :param1, :param2, :param3, :param4) do
15
14
  # Whether the given parameters equal the cached ones
16
15
  def same_params?(param1, param2, param3, param4)
17
16
  param1.eql?(self.param1) && param2.eql?(self.param2) && param3.eql?(self.param3) && param4.eql?(self.param4)
18
17
  end
19
-
20
- # Validates the cached parameters with the planner, once
21
- def validate(planner, serializer_class)
22
- return if validated
23
-
24
- planner.validate(serializer_class, planner.parse(serializer_class, param1, param2, param3, param4))
25
- self.validated = true
26
- end
27
18
  end
28
19
  private_constant :CACHED_PLAN
29
20
 
@@ -46,18 +37,14 @@ class Serega
46
37
  # The plan without parameters is always cached; plans with parameters
47
38
  # are cached by parameters as provided when
48
39
  # `max_cached_plans_per_serializer_count` is positive. Parameters are
49
- # parsed when a plan is built, and validated when
50
- # `check_selection` is true, or nil with
51
- # `config.check_selection` on.
52
- def fetch(param1 = nil, param2 = nil, param3 = nil, param4 = nil, check_selection: nil)
40
+ # parsed and validated when a plan is built.
41
+ def fetch(param1 = nil, param2 = nil, param3 = nil, param4 = nil)
53
42
  return default_plan if param1.nil? && param2.nil? && param3.nil? && param4.nil?
54
43
 
55
- config = @serializer_class.config
56
- check_selection = config.check_selection if check_selection.nil?
57
- max_size = config.max_cached_plans_per_serializer_count
58
- return build(param1, param2, param3, param4, check_selection) if max_size.zero?
44
+ max_size = @serializer_class.config.max_cached_plans_per_serializer_count
45
+ return build(param1, param2, param3, param4) if max_size.zero?
59
46
 
60
- cached_plan(param1, param2, param3, param4, max_size, check_selection)
47
+ cached_plan(param1, param2, param3, param4, max_size)
61
48
  end
62
49
 
63
50
  private
@@ -70,16 +57,13 @@ class Serega
70
57
  # Keeps plans by the key of the parameters, and compares each
71
58
  # parameter with the cached one. Parameters with the key of other
72
59
  # cached parameters replace them.
73
- def cached_plan(param1, param2, param3, param4, max_size, check_selection)
60
+ def cached_plan(param1, param2, param3, param4, max_size)
74
61
  key = cache_key(param1, param2, param3, param4)
75
62
  cached = @plans[key]
76
- if cached&.same_params?(param1, param2, param3, param4)
77
- cached.validate(@planner, @serializer_class) if check_selection
78
- return cached.plan
79
- end
63
+ return cached.plan if cached&.same_params?(param1, param2, param3, param4)
80
64
 
81
- plan = build(param1, param2, param3, param4, check_selection)
82
- @plans[key] = CACHED_PLAN.new(plan, check_selection, *frozen_copy([param1, param2, param3, param4]))
65
+ plan = build(param1, param2, param3, param4)
66
+ @plans[key] = CACHED_PLAN.new(plan, *frozen_copy([param1, param2, param3, param4]))
83
67
  @plans.shift if @plans.length > max_size
84
68
  plan
85
69
  end
@@ -103,11 +87,11 @@ class Serega
103
87
  # rubocop:enable Security/CompoundHash
104
88
  end
105
89
 
106
- def build(param1, param2, param3, param4, check_selection)
90
+ def build(param1, param2, param3, param4)
107
91
  params = @planner.parse(@serializer_class, param1, param2, param3, param4)
108
92
  return default_plan if @planner.empty?(params)
109
93
 
110
- @planner.validate(@serializer_class, params) if check_selection
94
+ @planner.validate(@serializer_class, params)
111
95
  @planner.plan(@serializer_class, params)
112
96
  end
113
97
 
@@ -19,9 +19,9 @@ class Serega
19
19
  # Returns serialization plan from the plans cache of the current Ractor.
20
20
  # Takes the parameters one by one like Cache#fetch: a splat allocates
21
21
  # an Array on every `Serega.new`.
22
- def fetch(param1 = nil, param2 = nil, param3 = nil, param4 = nil, check_selection: nil)
22
+ def fetch(param1 = nil, param2 = nil, param3 = nil, param4 = nil)
23
23
  plan_cache = SeregaUtils::RactorLocal.fetch(@key) { SeregaPlan::Cache.new(@serializer_class, @planner) }
24
- plan_cache.fetch(param1, param2, param3, param4, check_selection: check_selection)
24
+ plan_cache.fetch(param1, param2, param3, param4)
25
25
  end
26
26
  end
27
27
  end
@@ -48,9 +48,20 @@ class Serega
48
48
  names
49
49
  end
50
50
 
51
- # Attribute `many` option
51
+ # Attribute `many` option, or `config.many` of the relation serializer
52
+ # when the attribute has no `many` option
52
53
  def many
53
- attribute.many
54
+ many = attribute.many
55
+ return many unless many.nil? && @child_plan
56
+
57
+ @child_plan.serializer_class.config.many
58
+ end
59
+
60
+ # Whether a nil relation value serializes as `[]`: `config.many` of
61
+ # the relation serializer is true, and the attribute has neither the
62
+ # `:many` nor the `:default` option
63
+ def nil_as_empty_list?
64
+ attribute.many.nil? && many == true && !attribute.initials[:opts].key?(:default)
54
65
  end
55
66
 
56
67
  # Attribute `batch_loaders`
data/lib/serega/plan.rb CHANGED
@@ -59,7 +59,7 @@ class Serega
59
59
  rescue *RACTOR_ISOLATION_ERRORS
60
60
  raise if serializer_class.shareable?
61
61
 
62
- raise SeregaError, "#{serializer_class} can not serialize in other Ractors. Call `make_shareable!` at the end of its definition"
62
+ raise SeregaError, "#{serializer_class} can not serialize in other Ractors\n(hint: call `make_shareable!` at the end of its definition)"
63
63
  # :nocov:
64
64
  end
65
65
 
@@ -2,6 +2,10 @@
2
2
 
3
3
  class Serega
4
4
  module SeregaPlugins
5
+ # Deprecated: a response serializer with `data` and `meta` attributes
6
+ # replaces it, see `docs/responses.md` in the gem or
7
+ # [Response Serializers](https://github.com/andreyruby/serega/blob/master/docs/responses.md).
8
+ #
5
9
  # `plugin :context_metadata` adds metadata given per serialization with
6
10
  # the `:meta` option. Load `plugin :root` first.
7
11
  #
@@ -10,7 +14,7 @@ class Serega
10
14
  # plugin :context_metadata
11
15
  #
12
16
  # UserSerializer.to_h(user, meta: {version: "1.0.1"})
13
- # # => {data: {username: "batman"}, version: "1.0.1"}
17
+ # # => {data: {username: "gru"}, version: "1.0.1"}
14
18
  # ```
15
19
  #
16
20
  # See [Metadata per serialization](https://github.com/andreyruby/serega/blob/master/docs/root-and-metadata.md#metadata-per-serialization).
@@ -2,6 +2,10 @@
2
2
 
3
3
  class Serega
4
4
  module SeregaPlugins
5
+ # Deprecated: a response serializer with `data` and `meta` attributes
6
+ # replaces it, see `docs/responses.md` in the gem or
7
+ # [Response Serializers](https://github.com/andreyruby/serega/blob/master/docs/responses.md).
8
+ #
5
9
  # `plugin :metadata` adds metadata defined with
6
10
  # Serega::SeregaPlugins::Metadata::ClassMethods#meta_attribute. Load
7
11
  # `plugin :root` first.
@@ -2,6 +2,10 @@
2
2
 
3
3
  class Serega
4
4
  module SeregaPlugins
5
+ # Deprecated: a response serializer with `data` and `meta` attributes
6
+ # replaces it, see `docs/responses.md` in the gem or
7
+ # [Response Serializers](https://github.com/andreyruby/serega/blob/master/docs/responses.md).
8
+ #
5
9
  # `plugin :root` wraps results in a root key, `:data` by default
6
10
  #
7
11
  # ```ruby
@@ -9,8 +13,8 @@ class Serega
9
13
  # plugin :root, root: :users # one key for objects and lists
10
14
  # plugin :root, root_one: :user, root_many: :users # separate keys
11
15
  #
12
- # UserSerializer.to_h(user, root: :user) # => {user: {username: "batman"}}
13
- # UserSerializer.to_h(user, root: nil) # => {username: "batman"}
16
+ # UserSerializer.to_h(user, root: :user) # => {user: {username: "gru"}}
17
+ # UserSerializer.to_h(user, root: nil) # => {username: "gru"}
14
18
  # ```
15
19
  #
16
20
  # See [Root key](https://github.com/andreyruby/serega/blob/master/docs/root-and-metadata.md#root-key).
@@ -38,23 +42,23 @@ class Serega
38
42
 
39
43
  # Class methods the plugin adds to serializers
40
44
  module ClassMethods
41
- def call(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
42
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
45
+ def call(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil, **opts) # :nodoc:
46
+ new(only: only, with: with, except: except, preset: preset)
43
47
  .to_h(object, context: context, many: many, **opts)
44
48
  end
45
49
 
46
- def to_data(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
47
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
50
+ def to_data(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil, **opts) # :nodoc:
51
+ new(only: only, with: with, except: except, preset: preset)
48
52
  .to_data(object, context: context, many: many, **opts)
49
53
  end
50
54
 
51
- def to_struct(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
52
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
55
+ def to_struct(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil, **opts) # :nodoc:
56
+ new(only: only, with: with, except: except, preset: preset)
53
57
  .to_struct(object, context: context, many: many, **opts)
54
58
  end
55
59
 
56
- def to_json(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil, **opts) # :nodoc:
57
- new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
60
+ def to_json(object, only: nil, with: nil, except: nil, preset: nil, context: nil, many: nil, **opts) # :nodoc:
61
+ new(only: only, with: with, except: except, preset: preset)
58
62
  .to_json(object, context: context, many: many, **opts)
59
63
  end
60
64
 
@@ -13,6 +13,9 @@ class Serega
13
13
  activerecord_preloads: :ActiverecordPreloads
14
14
  }.freeze
15
15
 
16
+ # Names of deprecated plugins, replaced by response serializers
17
+ DEPRECATED = %i[root metadata context_metadata].freeze # :nodoc:
18
+
16
19
  class << self
17
20
  # Loads the code of the plugin and returns its module. Raises
18
21
  # SeregaError when the plugin does not exist.
@@ -21,6 +24,16 @@ class Serega
21
24
  require_relative "plugins/#{name}/#{name}"
22
25
  const_get(module_name)
23
26
  end
27
+
28
+ # Warning text for a deprecated plugin, `nil` for the others
29
+ def deprecation_warning(serializer_class, name) # :nodoc:
30
+ return unless DEPRECATED.include?(name)
31
+
32
+ "#{serializer_class}: plugin #{name.inspect} is deprecated. Migrate to a response serializer with data and " \
33
+ "meta attributes: " \
34
+ "https://github.com/andreyruby/serega/blob/master/docs/migrating-from-root-and-metadata.md. " \
35
+ "Set config.deprecation_warnings = false to hide this warning."
36
+ end
24
37
  end
25
38
  end
26
39
  end