grape_serializer 0.1.2 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/grape_serializer.rb +9 -2
- 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: fa4e4df474e763a22abed0ffb925b97217557d9c26a97a00a211ff46c7edeb49
|
4
|
+
data.tar.gz: 142444464287db55fd57b7168def5624be1521ef6062cca7291560dec24f5456
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e99fc0d52af691c3fad9335d544da4557e175c9b20caba4015bdd9161b5e4d7467393b8cbc3ae4b816f779333550b250dc57de131efba1175f80586ab98e6882
|
7
|
+
data.tar.gz: 0243e96fc109ee5288b31fe24c5fdbb18a6a15b4d187f29b0fd2a276751179b483f1e21d12c61f724d37af12b633122fb38f8eaebb7215042ccd0ec1bc0667a1
|
data/lib/grape_serializer.rb
CHANGED
@@ -12,19 +12,26 @@ class GrapeSerializer
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
attr_reader :options
|
16
|
+
|
15
17
|
def initialize(params = {})
|
16
18
|
@params = params
|
17
19
|
end
|
18
20
|
|
19
21
|
def represent(resource, opts = {})
|
22
|
+
@options = opts.merge!(@params)
|
20
23
|
preload_associations(resource) if defined?(::Rails::Engine) && defined?(::ActiveRecord) && (resource.is_a?(ActiveRecord::Base) || resource.is_a?(ActiveRecord::Relation))
|
21
|
-
self.class.entity_class.represent(resource,
|
24
|
+
self.class.entity_class.represent(resource, @options).as_json
|
22
25
|
end
|
23
26
|
|
24
27
|
protected
|
25
28
|
|
26
29
|
def preload_associations(resource)
|
27
|
-
|
30
|
+
if Rails::VERSION::MAJOR >= 7
|
31
|
+
ActiveRecord::Associations::Preloader.new(records: [resource].flatten, associations: association_array).call
|
32
|
+
else
|
33
|
+
ActiveRecord::Associations::Preloader.new.preload(resource, association_array)
|
34
|
+
end
|
28
35
|
end
|
29
36
|
|
30
37
|
def association_array
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JiaRou Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape-entity
|