model_driven_api 3.4.5 → 3.5.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a314e7709d9c9c7b77fd4f5ccdd39a8effa795026d0e69b09a586945776ce89
|
4
|
+
data.tar.gz: 77c639a19a4c0da657b4436022e30b4468d27f885ac3f16a8bd3cffd2e47c357
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9020280fe2d5cc0a41e32e25e198e0681c6a76730bd00f8c782fba35ae85209b7c5c9eacba8aa7e57cf72c3fd56c93eb73543a3556e3df960e209f11ac3bc657
|
7
|
+
data.tar.gz: 7d2ce4d012dc38bbb8a6edbbefb8161bd242c751cb924dde1c876ed517b05d628afdee7d0854a141c93170b331bc456a3747df79543064019b04b71ea548eacc
|
@@ -15,7 +15,7 @@ class AuthorizeApiRequest
|
|
15
15
|
|
16
16
|
def api_user
|
17
17
|
Rails.logger.debug "AuthorizeApiRequest: api_user -> #{decoded_auth_token}"
|
18
|
-
@api_user ||= (decoded_auth_token.blank? ? User.find(decoded_auth_token[:user_id])
|
18
|
+
@api_user ||= (decoded_auth_token.blank? ? nil : User.find(decoded_auth_token[:user_id]))
|
19
19
|
unless @api_user.blank?
|
20
20
|
return @api_user
|
21
21
|
else
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# config/initializers/auto_include_json.rb
|
2
|
+
|
3
|
+
module AutoIncludeJson
|
4
|
+
# Recursively builds a hash suitable for ActiveRecord eager loading
|
5
|
+
def build_includes(include_option)
|
6
|
+
case include_option
|
7
|
+
when Array
|
8
|
+
include_option
|
9
|
+
when Hash
|
10
|
+
include_option.each_with_object({}) do |(assoc, value), hash|
|
11
|
+
if value.is_a?(Hash) && value[:include]
|
12
|
+
hash[assoc] = build_includes(value[:include])
|
13
|
+
else
|
14
|
+
hash[assoc] = {}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
else
|
18
|
+
[]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Recursively sanitize include options to ensure only, except, methods propagate
|
23
|
+
def sanitize_includes(include_option)
|
24
|
+
case include_option
|
25
|
+
when Array
|
26
|
+
include_option
|
27
|
+
when Hash
|
28
|
+
include_option.each_with_object({}) do |(assoc, value), hash|
|
29
|
+
hash[assoc] = {}
|
30
|
+
|
31
|
+
if value.is_a?(Hash)
|
32
|
+
hash[assoc][:only] = value[:only] if value[:only]
|
33
|
+
hash[assoc][:except] = value[:except] if value[:except]
|
34
|
+
hash[assoc][:methods] = value[:methods] if value[:methods]
|
35
|
+
|
36
|
+
if value[:include]
|
37
|
+
hash[assoc][:include] = sanitize_includes(value[:include])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
else
|
42
|
+
[]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# For ActiveRecord::Relation (collections)
|
48
|
+
module AutoIncludeRelationJson
|
49
|
+
include AutoIncludeJson
|
50
|
+
|
51
|
+
def to_json(options = nil)
|
52
|
+
return super unless options.is_a?(Hash) && options[:include]
|
53
|
+
|
54
|
+
includes_hash = build_includes(options[:include])
|
55
|
+
sanitized_includes = sanitize_includes(options[:include])
|
56
|
+
options = options.merge(include: sanitized_includes)
|
57
|
+
|
58
|
+
self.includes(includes_hash).load.to_a.to_json(options)
|
59
|
+
end
|
60
|
+
|
61
|
+
def as_json(options = nil)
|
62
|
+
return super unless options.is_a?(Hash) && options[:include]
|
63
|
+
|
64
|
+
includes_hash = build_includes(options[:include])
|
65
|
+
sanitized_includes = sanitize_includes(options[:include])
|
66
|
+
options = options.merge(include: sanitized_includes)
|
67
|
+
|
68
|
+
self.includes(includes_hash).load.to_a.as_json(options)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# For ActiveRecord::Base (single records)
|
73
|
+
module AutoIncludeInstanceJson
|
74
|
+
include AutoIncludeJson
|
75
|
+
|
76
|
+
def to_json(options = nil)
|
77
|
+
preload_and_sanitize!(options)
|
78
|
+
super
|
79
|
+
end
|
80
|
+
|
81
|
+
def as_json(options = nil)
|
82
|
+
preload_and_sanitize!(options)
|
83
|
+
super
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def preload_and_sanitize!(options)
|
89
|
+
return unless options.is_a?(Hash) && options[:include]
|
90
|
+
|
91
|
+
includes_hash = build_includes(options[:include])
|
92
|
+
sanitized_includes = sanitize_includes(options[:include])
|
93
|
+
options[:include] = sanitized_includes
|
94
|
+
|
95
|
+
ActiveRecord::Associations::Preloader.new(
|
96
|
+
records: [self],
|
97
|
+
associations: includes_hash
|
98
|
+
).call
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
# Activate extensions
|
104
|
+
ActiveRecord::Relation.prepend(AutoIncludeRelationJson)
|
105
|
+
ActiveRecord::Base.prepend(AutoIncludeInstanceJson)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_driven_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriele Tassoni
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- app/models/test_api.rb
|
131
131
|
- app/models/used_token.rb
|
132
132
|
- config/initializers/after_initialize_for_model_driven_api.rb
|
133
|
+
- config/initializers/auto_include_json.rb
|
133
134
|
- config/initializers/cors_api_thecore.rb
|
134
135
|
- config/initializers/knock.rb
|
135
136
|
- config/initializers/time_with_zone.rb
|