serega 0.33.2 → 0.34.0
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.
- checksums.yaml +4 -4
- data/README.md +21 -24
- data/VERSION +1 -1
- data/lib/serega/attribute_normalizer.rb +8 -8
- data/lib/serega/config.rb +19 -14
- data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bbda67f020296ab580f7cde1b9f867c5ab68b84f3c4cfa9c7f7517d87367640b
|
|
4
|
+
data.tar.gz: bfea118712593fc12ea96bdea8dd3d8245bf01239474b6a034d408f1fdf04342
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 29d6e54cd973362751eda5533765cb44d8dac8621b77f335956de25d85e436e8c71889c72d4bb599a2114f98b9e2bc459c66f3a24f1e3e44441230ca32f923e3
|
|
7
|
+
data.tar.gz: bf4ea013fbd1a69e8c742964116834ae3bd4d5c4dc8dd62dd864acfcc53268277fb187a3b4f0b9a6cfb077464e6f24e4fd6222d54e6a54c53054775fe2c1707f
|
data/README.md
CHANGED
|
@@ -319,7 +319,7 @@ Serega includes built-in batch loading functionality to efficiently load data on
|
|
|
319
319
|
|
|
320
320
|
#### Defining Named Batch Loaders
|
|
321
321
|
|
|
322
|
-
Named loaders can be defined using the `
|
|
322
|
+
Named loaders can be defined using the `batch` class method and reused across attributes:
|
|
323
323
|
|
|
324
324
|
```ruby
|
|
325
325
|
class UserSerializer < Serega
|
|
@@ -385,10 +385,14 @@ class AppSerializer < Serega
|
|
|
385
385
|
# It helps to preload associations automatically, omitting N+1 requests.
|
|
386
386
|
config.auto_preload = false
|
|
387
387
|
|
|
388
|
-
#
|
|
389
|
-
#
|
|
390
|
-
#
|
|
391
|
-
|
|
388
|
+
# Hides attributes by default. Accepts:
|
|
389
|
+
# false - nothing hidden (default)
|
|
390
|
+
# true - all attributes hidden
|
|
391
|
+
# [:preload] - attributes with :preload option hidden
|
|
392
|
+
# [:batch] - attributes with :batch option hidden
|
|
393
|
+
# [:preload, :batch] - either option triggers hiding
|
|
394
|
+
# Useful to avoid extra DB requests for attributes that were not requested.
|
|
395
|
+
config.hide_by_default = false
|
|
392
396
|
|
|
393
397
|
# Default method used on serialized object to resolve batch value
|
|
394
398
|
# For example:
|
|
@@ -418,9 +422,8 @@ into a single associations hash.
|
|
|
418
422
|
|
|
419
423
|
Configuration options:
|
|
420
424
|
|
|
421
|
-
- `config.
|
|
422
|
-
- `config.
|
|
423
|
-
- `config.auto_hide_attributes_with_preload` - default `false`
|
|
425
|
+
- `config.auto_preload` - default `false` (use `true` or `{ has_delegate_option: true, has_serializer_option: true }`)
|
|
426
|
+
- `config.hide_by_default` - default `false` (use `[:preload]` to hide attributes with preloads)
|
|
424
427
|
|
|
425
428
|
These options are extremely useful if you want to forget about finding
|
|
426
429
|
preloads manually.
|
|
@@ -433,9 +436,8 @@ For some examples, **please read the comments in the code below**
|
|
|
433
436
|
|
|
434
437
|
```ruby
|
|
435
438
|
class AppSerializer < Serega
|
|
436
|
-
config.
|
|
437
|
-
config.
|
|
438
|
-
config.auto_hide_attributes_with_preload = true
|
|
439
|
+
config.auto_preload = true
|
|
440
|
+
config.hide_by_default = [:preload]
|
|
439
441
|
end
|
|
440
442
|
|
|
441
443
|
class UserSerializer < AppSerializer
|
|
@@ -447,11 +449,11 @@ class UserSerializer < AppSerializer
|
|
|
447
449
|
value: proc { |user| user.user_stats.followers_count }
|
|
448
450
|
|
|
449
451
|
# `preload: :user_stats` added automatically, as
|
|
450
|
-
# `
|
|
452
|
+
# `auto_preload` includes `has_delegate_option: true`
|
|
451
453
|
attribute :comments_count, delegate: { to: :user_stats }
|
|
452
454
|
|
|
453
455
|
# `preload: :albums` added automatically as
|
|
454
|
-
# `
|
|
456
|
+
# `auto_preload` includes `has_serializer_option: true`
|
|
455
457
|
attribute :albums, serializer: 'AlbumSerializer'
|
|
456
458
|
end
|
|
457
459
|
|
|
@@ -459,7 +461,7 @@ class AlbumSerializer < AppSerializer
|
|
|
459
461
|
attribute :images_count, delegate: { to: :album_stats }
|
|
460
462
|
end
|
|
461
463
|
|
|
462
|
-
# By default, preloads are empty, as we specify `
|
|
464
|
+
# By default, preloads are empty, as we specify `hide_by_default = [:preload]`
|
|
463
465
|
# so attributes with preloads will be skipped and nothing will be preloaded
|
|
464
466
|
UserSerializer.new.preloads
|
|
465
467
|
# => {}
|
|
@@ -487,10 +489,8 @@ other user associations. You should specify `preload: nil` to preload
|
|
|
487
489
|
|
|
488
490
|
```ruby
|
|
489
491
|
class AppSerializer < Serega
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
auto_preload_attributes_with_serializer: true,
|
|
493
|
-
auto_hide_attributes_with_preload: true
|
|
492
|
+
config.auto_preload = true
|
|
493
|
+
config.hide_by_default = [:preload]
|
|
494
494
|
end
|
|
495
495
|
|
|
496
496
|
class UserSerializer < AppSerializer
|
|
@@ -511,9 +511,7 @@ achieve this:
|
|
|
511
511
|
|
|
512
512
|
```ruby
|
|
513
513
|
class AppSerializer < Serega
|
|
514
|
-
|
|
515
|
-
auto_preload_attributes_with_delegate: true,
|
|
516
|
-
auto_preload_attributes_with_serializer: true
|
|
514
|
+
config.auto_preload = true
|
|
517
515
|
end
|
|
518
516
|
|
|
519
517
|
class UserSerializer < AppSerializer
|
|
@@ -570,9 +568,8 @@ uses ActiveRecord::Associations::Preloader to preload associations to objects.
|
|
|
570
568
|
|
|
571
569
|
```ruby
|
|
572
570
|
class AppSerializer < Serega
|
|
573
|
-
config.
|
|
574
|
-
config.
|
|
575
|
-
config.auto_hide_attributes_with_preload = false
|
|
571
|
+
config.auto_preload = true
|
|
572
|
+
config.hide_by_default = false
|
|
576
573
|
|
|
577
574
|
plugin :activerecord_preloads
|
|
578
575
|
end
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.34.0
|
|
@@ -153,18 +153,18 @@ class Serega
|
|
|
153
153
|
end
|
|
154
154
|
|
|
155
155
|
def prepare_hide
|
|
156
|
-
# Return provided
|
|
156
|
+
# Return provided directly value
|
|
157
157
|
hide = init_opts[:hide]
|
|
158
158
|
return hide if (hide == true) || (hide == false)
|
|
159
159
|
|
|
160
|
-
|
|
161
|
-
if config.auto_hide.fetch(:has_preload_option)
|
|
162
|
-
return true if preloads
|
|
163
|
-
end
|
|
160
|
+
hide_setting = config.hide_by_default
|
|
164
161
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return true
|
|
162
|
+
case hide_setting
|
|
163
|
+
when true
|
|
164
|
+
return true
|
|
165
|
+
when Array
|
|
166
|
+
return true if hide_setting.include?(:preload) && preloads # hide when attribute has `:preload` option
|
|
167
|
+
return true if hide_setting.include?(:batch) && batch_loaders.any? # hide when attribute has `:batch` option
|
|
168
168
|
end
|
|
169
169
|
|
|
170
170
|
# Return nil for undefined value which means "not hide" but allows
|
data/lib/serega/config.rb
CHANGED
|
@@ -32,7 +32,7 @@ class Serega
|
|
|
32
32
|
delegate_default_allow_nil: false,
|
|
33
33
|
max_cached_plans_per_serializer_count: 0,
|
|
34
34
|
auto_preload: {has_delegate_option: false, has_serializer_option: false},
|
|
35
|
-
|
|
35
|
+
hide_by_default: false,
|
|
36
36
|
batch_id_option: :id
|
|
37
37
|
}.freeze
|
|
38
38
|
# :nocov:
|
|
@@ -115,23 +115,28 @@ class Serega
|
|
|
115
115
|
opts[:delegate_default_allow_nil] = value
|
|
116
116
|
end
|
|
117
117
|
|
|
118
|
-
#
|
|
119
|
-
|
|
120
|
-
|
|
118
|
+
# Returns :hide_by_default config option
|
|
119
|
+
# @return [Boolean, Array<Symbol>] Current :hide_by_default config option
|
|
120
|
+
def hide_by_default
|
|
121
|
+
opts.fetch(:hide_by_default)
|
|
121
122
|
end
|
|
122
123
|
|
|
123
|
-
#
|
|
124
|
-
#
|
|
125
|
-
|
|
126
|
-
|
|
124
|
+
# Sets :hide_by_default config option
|
|
125
|
+
#
|
|
126
|
+
# @param value [Boolean, Array<Symbol>] false, true, or array of :preload/:batch symbols
|
|
127
|
+
#
|
|
128
|
+
# @return [Boolean, Array<Symbol>] New :hide_by_default config option
|
|
129
|
+
def hide_by_default=(value)
|
|
130
|
+
opts[:hide_by_default] =
|
|
127
131
|
case value
|
|
128
|
-
when true
|
|
129
|
-
|
|
130
|
-
when
|
|
131
|
-
|
|
132
|
-
|
|
132
|
+
when true, false
|
|
133
|
+
value
|
|
134
|
+
when Array
|
|
135
|
+
invalid = value - %i[preload batch]
|
|
136
|
+
raise SeregaError, "Must have true, false, or an Array of [:preload, :batch], #{value.inspect} provided" if invalid.any?
|
|
137
|
+
value
|
|
133
138
|
else
|
|
134
|
-
raise SeregaError, "Must have
|
|
139
|
+
raise SeregaError, "Must have true, false, or an Array of [:preload, :batch], #{value.inspect} provided"
|
|
135
140
|
end
|
|
136
141
|
end
|
|
137
142
|
|
|
@@ -15,7 +15,7 @@ class Serega
|
|
|
15
15
|
# class AppSerializer < Serega
|
|
16
16
|
# config.auto_preload_attributes_with_delegate = true
|
|
17
17
|
# config.auto_preload_attributes_with_serializer = true
|
|
18
|
-
# config.
|
|
18
|
+
# config.hide_by_default = [:preload]
|
|
19
19
|
#
|
|
20
20
|
# plugin :activerecord_preloads
|
|
21
21
|
# end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: serega
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.34.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrey Glushkov
|
|
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
121
121
|
- !ruby/object:Gem::Version
|
|
122
122
|
version: '0'
|
|
123
123
|
requirements: []
|
|
124
|
-
rubygems_version:
|
|
124
|
+
rubygems_version: 4.0.9
|
|
125
125
|
specification_version: 4
|
|
126
126
|
summary: JSON Serializer
|
|
127
127
|
test_files: []
|