model_driven_api 3.4.6 → 3.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 429b78e0b1d6d4a1fad0f20d4e5d614f1fbc0b1c7b55fe24481a23286a241685
4
- data.tar.gz: 7fe2351ea6411a773d811ed5eecc7a2c3376bb975b0828866308c8444d5da85b
3
+ metadata.gz: 81f4bf59354a604bb9040be285c3e9ed38f4e847ed945254c6ef2969f72190af
4
+ data.tar.gz: 83fd3fd82615dc3a23e8a5bad639c2fb9bbacb9ac699df1544e46f6219ec960f
5
5
  SHA512:
6
- metadata.gz: 0c646e1b5a7533f4513b2ce1466ef6f2907fcfa2a6385f603382b1ba19ede14da9b50d7d611d72108f8f47d018129dbac16d6a1d3192aff7b102a72be51a668d
7
- data.tar.gz: f0bdacb0f2625083ab1453ccddce7dfca4c3ff469a7ee88d0c8035d826f72ca9f3609fb60bdcf2e5e136b99998ee4af49ab1bbfb1c10a5f032f7a0cb5fa62a13
6
+ metadata.gz: 9cd024384b0ee5ab85e40b30b0f986307f04c5ffd4a9d89b45742a02e7f629777c7da57f4af3d118b553cfc78803783baa376ccbb262f55764f513ab7d1d5958
7
+ data.tar.gz: 3f23e0f14efd400f79edfa762143f6ccbd6b6e5dab6e536b6f27631aa6260640737452a1971632cc729c2245e931ae5bf6ddc3ee74435f75c2016fff0bc5465b
@@ -0,0 +1,109 @@
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
+ Rails.logger.debug "Building includes from: #{include_option.inspect} from #{caller_locations(1,1).first.label}"
7
+ case include_option
8
+ when Array
9
+ include_option
10
+ when Hash
11
+ include_option.each_with_object({}) do |(assoc, value), hash|
12
+ if value.is_a?(Hash) && value[:include]
13
+ hash[assoc] = build_includes(value[:include])
14
+ else
15
+ hash[assoc] = {}
16
+ end
17
+ end
18
+ else
19
+ []
20
+ end
21
+ end
22
+
23
+ # Recursively sanitize include options to ensure only, except, methods propagate
24
+ def sanitize_includes(include_option)
25
+ case include_option
26
+ when Array
27
+ include_option
28
+ when Hash
29
+ include_option.each_with_object({}) do |(assoc, value), hash|
30
+ hash[assoc] = {}
31
+
32
+ if value.is_a?(Hash)
33
+ hash[assoc][:only] = value[:only] if value[:only]
34
+ hash[assoc][:except] = value[:except] if value[:except]
35
+ hash[assoc][:methods] = value[:methods] if value[:methods]
36
+
37
+ if value[:include]
38
+ hash[assoc][:include] = sanitize_includes(value[:include])
39
+ end
40
+ end
41
+ end
42
+ else
43
+ []
44
+ end
45
+ end
46
+ end
47
+
48
+ # For ActiveRecord::Relation (collections)
49
+ module AutoIncludeRelationJson
50
+ include AutoIncludeJson
51
+
52
+ def to_json(options = nil)
53
+ Rails.logger.debug "Relation JSON to_json from #{caller_locations(1,1).first.label}"
54
+ return super unless options.is_a?(Hash) && options[:include]
55
+
56
+ includes_hash = build_includes(options[:include])
57
+ sanitized_includes = sanitize_includes(options[:include])
58
+ options = options.merge(include: sanitized_includes)
59
+
60
+ self.includes(includes_hash).load.to_a.to_json(options)
61
+ end
62
+
63
+ def as_json(options = nil)
64
+ Rails.logger.debug "Relation JSON as_json from #{caller_locations(1,1).first.label}"
65
+ return super unless options.is_a?(Hash) && options[:include]
66
+
67
+ includes_hash = build_includes(options[:include])
68
+ sanitized_includes = sanitize_includes(options[:include])
69
+ options = options.merge(include: sanitized_includes)
70
+
71
+ self.includes(includes_hash).load.to_a.as_json(options)
72
+ end
73
+ end
74
+
75
+ # For ActiveRecord::Base (single records)
76
+ module AutoIncludeInstanceJson
77
+ include AutoIncludeJson
78
+
79
+ def to_json(options = nil)
80
+ preload_and_sanitize!(options)
81
+ super
82
+ end
83
+
84
+ def as_json(options = nil)
85
+ preload_and_sanitize!(options)
86
+ super
87
+ end
88
+
89
+ private
90
+
91
+ def preload_and_sanitize!(options)
92
+ Rails.logger.debug "Instance JSON preload_and_sanitize! from #{caller_locations(1,1).first.label}"
93
+ return unless options.is_a?(Hash) && options[:include]
94
+
95
+ includes_hash = build_includes(options[:include])
96
+ sanitized_includes = sanitize_includes(options[:include])
97
+ options[:include] = sanitized_includes
98
+
99
+ ActiveRecord::Associations::Preloader.new(
100
+ records: [self],
101
+ associations: includes_hash
102
+ ).call
103
+ end
104
+
105
+ end
106
+
107
+ # Activate extensions
108
+ ActiveRecord::Relation.prepend(AutoIncludeRelationJson)
109
+ ActiveRecord::Base.prepend(AutoIncludeInstanceJson)
@@ -1,3 +1,3 @@
1
1
  module ModelDrivenApi
2
- VERSION = "3.4.6".freeze
2
+ VERSION = "3.5.1".freeze
3
3
  end
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.6
4
+ version: 3.5.1
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