blueprinter_schema 1.6.1 → 1.7.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 +31 -7
- data/lib/blueprinter_schema/generator.rb +15 -4
- data/lib/blueprinter_schema/version.rb +1 -1
- 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: 10b97bd206d6715355d0156e5f4974fdde88e4bde379a55c4216ad86043256af
|
|
4
|
+
data.tar.gz: 553afa8720b17b25d338eeda16b2959654a942765efbe509bb1ee51ae37f949c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 19c19495d8b3a3e0803ee43ff817b261b0d06af1ea872c8ae2229c306235896a3ec957769fde57191fd4fc7594a637be7263d83f4a7c5ed54f31c198cacca1c7
|
|
7
|
+
data.tar.gz: 067fea318a9903c9af725b00cb27cc71012cffcaabdc31fe19df179798021e7a2fd18f52b9144dfa0cd35ead7e8031ba68c2723915601ce3575f6cd4eec0d931
|
data/README.md
CHANGED
|
@@ -195,22 +195,46 @@ class RestrictionsSerializer < Blueprinter::Base
|
|
|
195
195
|
field :max_units
|
|
196
196
|
end
|
|
197
197
|
|
|
198
|
-
|
|
198
|
+
class Product
|
|
199
|
+
include ActiveModel::Model
|
|
200
|
+
include ActiveModel::Attributes
|
|
201
|
+
|
|
202
|
+
attribute :reference, :string
|
|
203
|
+
attribute :restrictions
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
class ProductSerializer < Blueprinter::Base
|
|
207
|
+
field :reference
|
|
208
|
+
association :restrictions, blueprint: RestrictionsSerializer, model: Restrictions
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
BlueprinterSchema.generate(serializer: ProductSerializer, model: Product)
|
|
199
212
|
```
|
|
200
213
|
|
|
201
214
|
```rb
|
|
202
215
|
{
|
|
203
216
|
"type" => "object",
|
|
204
|
-
"title" => "
|
|
217
|
+
"title" => "Product",
|
|
205
218
|
"properties" => {
|
|
206
|
-
"
|
|
207
|
-
"type" => "
|
|
219
|
+
"reference" => {
|
|
220
|
+
"type" => ["string", "null"]
|
|
208
221
|
},
|
|
209
|
-
"
|
|
210
|
-
"type" =>
|
|
222
|
+
"restrictions" => {
|
|
223
|
+
"type" => "object",
|
|
224
|
+
"title" => "Restrictions",
|
|
225
|
+
"properties" => {
|
|
226
|
+
"min_units" => {
|
|
227
|
+
"type" => "integer"
|
|
228
|
+
},
|
|
229
|
+
"max_units" => {
|
|
230
|
+
"type" => ["integer", "null"]
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
"required" => ["max_units", "min_units"],
|
|
234
|
+
"additionalProperties" => false
|
|
211
235
|
}
|
|
212
236
|
},
|
|
213
|
-
"required" => ["
|
|
237
|
+
"required" => ["reference", "restrictions"],
|
|
214
238
|
"additionalProperties" => false
|
|
215
239
|
}
|
|
216
240
|
```
|
|
@@ -150,13 +150,24 @@ module BlueprinterSchema
|
|
|
150
150
|
return { 'type' => 'object' } unless blueprint_class
|
|
151
151
|
|
|
152
152
|
ar_association = @model.try(:reflect_on_association, association.name)
|
|
153
|
-
is_collection = ar_association ? ar_association.collection? : association.options[:collection]
|
|
154
|
-
|
|
155
153
|
view = association.options[:view] || :default
|
|
156
154
|
type = association.options[:optional] ? %w[object null] : 'object'
|
|
157
|
-
|
|
155
|
+
model = association_model(association, ar_association)
|
|
156
|
+
associated_schema = recursive_generate(blueprint_class, model, view, type:)
|
|
157
|
+
|
|
158
|
+
wrap_collection(associated_schema, association_collection?(association, ar_association))
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def association_model(association, ar_association)
|
|
162
|
+
ar_association&.klass || association.options[:model]
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def association_collection?(association, ar_association)
|
|
166
|
+
ar_association ? ar_association.collection? : association.options[:collection]
|
|
167
|
+
end
|
|
158
168
|
|
|
159
|
-
|
|
169
|
+
def wrap_collection(schema, is_collection)
|
|
170
|
+
is_collection ? { 'type' => 'array', 'items' => schema } : schema
|
|
160
171
|
end
|
|
161
172
|
|
|
162
173
|
def recursive_generate(serializer, model, view, type:)
|