blueprinter_schema 1.6.1 → 1.8.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: 51982caa98d5ae3d364c33024d21cd2acbe0a9350ffc9696ef3ff42ba0177427
4
+ data.tar.gz: 671bab5263c4c03a5ece305359a457da839b1363b547679de678159938a87bc4
5
5
  SHA512:
6
- metadata.gz: 807bc1360008eb60eaf2df3c55f1287dc90d11fc2d6bc71f4abe68153ab678fd267b14382b581adfbbd5e728f262183a6434901c50d4086de8f4b815248a558b
7
- data.tar.gz: e18240976adffd74caff957b4be334c72acd88e0286f03fd20ff60f118b5e1e0e6a94be6dedc689216fa761f1a588e33c359eaa1ff3fd680a305aacf65fc0e35
6
+ metadata.gz: b7dc67b5ebbf4e3b6aca4d8ece274d96f66cda67caff677fbfa4352de73a3374c6485f2cf0a8ad48d879b46f43e92151ccf6ae6b836d72f90c3d4c6a18bf0a59
7
+ data.tar.gz: dc3b0317a497218a9d4047031426e7c89714a9d55d15c195c490a843e4fe16c22b4af17455477f28939b184cad32b8fb9a4a9756168481061e25da644ed68668
data/README.md CHANGED
@@ -20,6 +20,7 @@ class UserSerializer < Blueprinter::Base
20
20
  field :email, type: :string, format: :email
21
21
  field :role, type: :string, enum: ["user", "admin"]
22
22
  field :tags, type: :array, items: { type: :string }
23
+ field :legacy_name, type: :string, deprecated: true
23
24
  end
24
25
 
25
26
  BlueprinterSchema.generate(serializer: UserSerializer)
@@ -50,9 +51,13 @@ BlueprinterSchema.generate(serializer: UserSerializer)
50
51
  "tags" => {
51
52
  "type" => "array",
52
53
  "items" => { "type" => "string" }
54
+ },
55
+ "legacy_name" => {
56
+ "type" => "string",
57
+ "deprecated" => true
53
58
  }
54
59
  },
55
- "required" => ["first_name", "last_name", "full_name", "email", "role", "tags"],
60
+ "required" => ["first_name", "last_name", "full_name", "email", "role", "tags", "legacy_name"],
56
61
  "additionalProperties" => false
57
62
  }
58
63
  ```
@@ -195,22 +200,46 @@ class RestrictionsSerializer < Blueprinter::Base
195
200
  field :max_units
196
201
  end
197
202
 
198
- BlueprinterSchema.generate(serializer: RestrictionsSerializer, model: Restrictions)
203
+ class Product
204
+ include ActiveModel::Model
205
+ include ActiveModel::Attributes
206
+
207
+ attribute :reference, :string
208
+ attribute :restrictions
209
+ end
210
+
211
+ class ProductSerializer < Blueprinter::Base
212
+ field :reference
213
+ association :restrictions, blueprint: RestrictionsSerializer, model: Restrictions
214
+ end
215
+
216
+ BlueprinterSchema.generate(serializer: ProductSerializer, model: Product)
199
217
  ```
200
218
 
201
219
  ```rb
202
220
  {
203
221
  "type" => "object",
204
- "title" => "Restrictions",
222
+ "title" => "Product",
205
223
  "properties" => {
206
- "min_units" => {
207
- "type" => "integer"
224
+ "reference" => {
225
+ "type" => ["string", "null"]
208
226
  },
209
- "max_units" => {
210
- "type" => ["integer", "null"]
227
+ "restrictions" => {
228
+ "type" => "object",
229
+ "title" => "Restrictions",
230
+ "properties" => {
231
+ "min_units" => {
232
+ "type" => "integer"
233
+ },
234
+ "max_units" => {
235
+ "type" => ["integer", "null"]
236
+ }
237
+ },
238
+ "required" => ["max_units", "min_units"],
239
+ "additionalProperties" => false
211
240
  }
212
241
  },
213
- "required" => ["max_units", "min_units"],
242
+ "required" => ["reference", "restrictions"],
214
243
  "additionalProperties" => false
215
244
  }
216
245
  ```
@@ -100,6 +100,7 @@ module BlueprinterSchema
100
100
  type_definition['items'] = options[:items].deep_stringify_keys if options[:items]
101
101
  type_definition['format'] = options[:format] if options[:format]
102
102
  type_definition['description'] = options[:description] if options[:description]
103
+ type_definition['deprecated'] = true if options[:deprecated]
103
104
  type_definition
104
105
  end
105
106
 
@@ -150,13 +151,24 @@ module BlueprinterSchema
150
151
  return { 'type' => 'object' } unless blueprint_class
151
152
 
152
153
  ar_association = @model.try(:reflect_on_association, association.name)
153
- is_collection = ar_association ? ar_association.collection? : association.options[:collection]
154
-
155
154
  view = association.options[:view] || :default
156
155
  type = association.options[:optional] ? %w[object null] : 'object'
157
- associated_schema = recursive_generate(blueprint_class, ar_association&.klass, view, type:)
156
+ model = association_model(association, ar_association)
157
+ associated_schema = recursive_generate(blueprint_class, model, view, type:)
158
+
159
+ wrap_collection(associated_schema, association_collection?(association, ar_association))
160
+ end
161
+
162
+ def association_model(association, ar_association)
163
+ ar_association&.klass || association.options[:model]
164
+ end
165
+
166
+ def association_collection?(association, ar_association)
167
+ ar_association ? ar_association.collection? : association.options[:collection]
168
+ end
158
169
 
159
- is_collection ? { 'type' => 'array', 'items' => associated_schema } : associated_schema
170
+ def wrap_collection(schema, is_collection)
171
+ is_collection ? { 'type' => 'array', 'items' => schema } : schema
160
172
  end
161
173
 
162
174
  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.8.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.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thisismydesign