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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b4bead8620839a075c510b0f243d4e8c3d8ee38a3f5025e493bf0c3f59d01519
4
- data.tar.gz: 6ee11ed05b4b0337af6e4276e02cee4ce546eb47f7de4d36ff024c6af229b356
3
+ metadata.gz: 10b97bd206d6715355d0156e5f4974fdde88e4bde379a55c4216ad86043256af
4
+ data.tar.gz: 553afa8720b17b25d338eeda16b2959654a942765efbe509bb1ee51ae37f949c
5
5
  SHA512:
6
- metadata.gz: 807bc1360008eb60eaf2df3c55f1287dc90d11fc2d6bc71f4abe68153ab678fd267b14382b581adfbbd5e728f262183a6434901c50d4086de8f4b815248a558b
7
- data.tar.gz: e18240976adffd74caff957b4be334c72acd88e0286f03fd20ff60f118b5e1e0e6a94be6dedc689216fa761f1a588e33c359eaa1ff3fd680a305aacf65fc0e35
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
- BlueprinterSchema.generate(serializer: RestrictionsSerializer, model: Restrictions)
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" => "Restrictions",
217
+ "title" => "Product",
205
218
  "properties" => {
206
- "min_units" => {
207
- "type" => "integer"
219
+ "reference" => {
220
+ "type" => ["string", "null"]
208
221
  },
209
- "max_units" => {
210
- "type" => ["integer", "null"]
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" => ["max_units", "min_units"],
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
- associated_schema = recursive_generate(blueprint_class, ar_association&.klass, view, type:)
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
- is_collection ? { 'type' => 'array', 'items' => associated_schema } : associated_schema
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:)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlueprinterSchema
4
- VERSION = '1.6.1'
4
+ VERSION = '1.7.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprinter_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thisismydesign