serega 0.37.0 → 0.37.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.
- checksums.yaml +4 -4
- data/README.md +30 -3
- data/VERSION +1 -1
- data/lib/serega/plugins/if/if.rb +5 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ca01b1c5668c5ce5963e5f2b71540dae1d023eed44c6e9e026d43e63fcead4c6
|
|
4
|
+
data.tar.gz: f526d737921efe1789751b51ff15b503b0febe2d5aafbb1a57666625a14b2228
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 643c12f65a16dbce5d05a8262df0f85fc6a5055436a65d2590f0615ccba5a9cb2d1287d4ec1262b9d0f252432262e88799b55d13ca7ce5fed252c58faf9b7a1a
|
|
7
|
+
data.tar.gz: fa8ad847f04617c8ea70cf227aca9bf9ede02c1b1449afb6547b821f09cd8a27638cb5d8904ddc576dfe12c01d5cd6991a99e7cb9fa1cf28d46d2053c7650978
|
data/README.md
CHANGED
|
@@ -530,12 +530,12 @@ attribute is serialized:
|
|
|
530
530
|
|
|
531
531
|
```ruby
|
|
532
532
|
class UserSerializer < Serega
|
|
533
|
+
attribute :name
|
|
534
|
+
attribute :posts, serializer: PostSerializer, preload: :posts
|
|
535
|
+
|
|
533
536
|
# `objects` is the gathered users; `preloads` is this attribute's
|
|
534
537
|
# `:preload` value (`:posts`). `MyORM.preload` does the eager loading.
|
|
535
538
|
preload_with { |objects, preloads| MyORM.preload(objects, preloads) }
|
|
536
|
-
|
|
537
|
-
attribute :name
|
|
538
|
-
attribute :posts, serializer: PostSerializer, preload: :posts
|
|
539
539
|
end
|
|
540
540
|
```
|
|
541
541
|
|
|
@@ -547,6 +547,33 @@ objects can be plain Ruby objects too: the handler decides where the data comes
|
|
|
547
547
|
from (another ORM, an HTTP API, a cache) and how to attach it to the objects,
|
|
548
548
|
by whatever rules you define.
|
|
549
549
|
|
|
550
|
+
The handler runs **once per preloaded attribute**, not once per distinct
|
|
551
|
+
`:preload` value. If the same `:preload` is declared on several attributes, the
|
|
552
|
+
handler is invoked once for each of them over the same objects, so it should
|
|
553
|
+
check whether the data is already loaded before loading it again. (The
|
|
554
|
+
`:activerecord_preloads` handler relies on `ActiveRecord::Associations::Preloader`,
|
|
555
|
+
which already skips associations that are loaded.)
|
|
556
|
+
|
|
557
|
+
Because you choose the `:preload` value, it doubles as a **discriminator**: give
|
|
558
|
+
two attributes different `:preload` values and branch on `preloads` inside the
|
|
559
|
+
handler — no need for the value to be a real association name.
|
|
560
|
+
|
|
561
|
+
```ruby
|
|
562
|
+
class PostSerializer < Serega
|
|
563
|
+
attribute :owner, serializer: UserSerializer, preload: :owner
|
|
564
|
+
attribute :author, serializer: UserSerializer, preload: :author
|
|
565
|
+
|
|
566
|
+
# Both attributes load the same person, but from different sources.
|
|
567
|
+
preload_with do |objects, preloads|
|
|
568
|
+
case preloads
|
|
569
|
+
when :owner then load_owners(objects) # e.g. from the DB
|
|
570
|
+
when :author then load_authors(objects) # e.g. from an external API
|
|
571
|
+
else MyORM.preload(objects, preloads)
|
|
572
|
+
end
|
|
573
|
+
end
|
|
574
|
+
end
|
|
575
|
+
```
|
|
576
|
+
|
|
550
577
|
---
|
|
551
578
|
|
|
552
579
|
## Plugins
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.37.
|
|
1
|
+
0.37.1
|
data/lib/serega/plugins/if/if.rb
CHANGED
|
@@ -275,8 +275,11 @@ class Serega
|
|
|
275
275
|
super
|
|
276
276
|
end
|
|
277
277
|
|
|
278
|
-
def attach_final_value(value, point,
|
|
279
|
-
|
|
278
|
+
def attach_final_value(value, point, container)
|
|
279
|
+
unless point.satisfy_if_value_conditions?(value, context)
|
|
280
|
+
container.delete(point.name) # remove reserved placeholder
|
|
281
|
+
return KEY_SKIPPED
|
|
282
|
+
end
|
|
280
283
|
|
|
281
284
|
super
|
|
282
285
|
end
|