blueprinter_schema 1.5.0 → 1.6.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 +4 -4
- data/README.md +4 -9
- data/lib/blueprinter_schema/generator.rb +7 -10
- data/lib/blueprinter_schema/model_attributes.rb +54 -0
- data/lib/blueprinter_schema/version.rb +1 -1
- data/lib/blueprinter_schema.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b4bead8620839a075c510b0f243d4e8c3d8ee38a3f5025e493bf0c3f59d01519
|
|
4
|
+
data.tar.gz: 6ee11ed05b4b0337af6e4276e02cee4ce546eb47f7de4d36ff024c6af229b356
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 807bc1360008eb60eaf2df3c55f1287dc90d11fc2d6bc71f4abe68153ab678fd267b14382b581adfbbd5e728f262183a6434901c50d4086de8f4b815248a558b
|
|
7
|
+
data.tar.gz: e18240976adffd74caff957b4be334c72acd88e0286f03fd20ff60f118b5e1e0e6a94be6dedc689216fa761f1a588e33c359eaa1ff3fd680a305aacf65fc0e35
|
data/README.md
CHANGED
|
@@ -186,13 +186,13 @@ class Restrictions
|
|
|
186
186
|
|
|
187
187
|
attribute :min_units, :integer
|
|
188
188
|
attribute :max_units, :integer
|
|
189
|
-
|
|
189
|
+
|
|
190
|
+
validates :min_units, presence: true
|
|
190
191
|
end
|
|
191
192
|
|
|
192
193
|
class RestrictionsSerializer < Blueprinter::Base
|
|
193
194
|
field :min_units
|
|
194
195
|
field :max_units
|
|
195
|
-
field :label, type: [:string, :null]
|
|
196
196
|
end
|
|
197
197
|
|
|
198
198
|
BlueprinterSchema.generate(serializer: RestrictionsSerializer, model: Restrictions)
|
|
@@ -207,19 +207,14 @@ BlueprinterSchema.generate(serializer: RestrictionsSerializer, model: Restrictio
|
|
|
207
207
|
"type" => "integer"
|
|
208
208
|
},
|
|
209
209
|
"max_units" => {
|
|
210
|
-
"type" => "integer"
|
|
211
|
-
},
|
|
212
|
-
"label" => {
|
|
213
|
-
"type" => ["string", "null"]
|
|
210
|
+
"type" => ["integer", "null"]
|
|
214
211
|
}
|
|
215
212
|
},
|
|
216
|
-
"required" => ["max_units", "min_units"
|
|
213
|
+
"required" => ["max_units", "min_units"],
|
|
217
214
|
"additionalProperties" => false
|
|
218
215
|
}
|
|
219
216
|
```
|
|
220
217
|
|
|
221
|
-
Note: Attributes are assumed to be non-null. If an attribute is nullable, specify it explicitly in the serializer.
|
|
222
|
-
|
|
223
218
|
## Development
|
|
224
219
|
|
|
225
220
|
Devcontainer / Codespaces / Native
|
|
@@ -88,14 +88,11 @@ module BlueprinterSchema
|
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
def model_attribute_to_json_schema(name)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
else
|
|
97
|
-
@fallback_definition.dup
|
|
98
|
-
end
|
|
91
|
+
type_to_json_schema(model_attributes.type(name), model_attributes.nullable?(name))
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def model_attributes
|
|
95
|
+
@model_attributes ||= ModelAttributes.new(@model)
|
|
99
96
|
end
|
|
100
97
|
|
|
101
98
|
def merge_field_options(type_definition, options)
|
|
@@ -147,12 +144,12 @@ module BlueprinterSchema
|
|
|
147
144
|
type
|
|
148
145
|
end
|
|
149
146
|
|
|
150
|
-
def association_to_json_schema(association)
|
|
147
|
+
def association_to_json_schema(association)
|
|
151
148
|
blueprint_class = association.options[:blueprint]
|
|
152
149
|
|
|
153
150
|
return { 'type' => 'object' } unless blueprint_class
|
|
154
151
|
|
|
155
|
-
ar_association = @model
|
|
152
|
+
ar_association = @model.try(:reflect_on_association, association.name)
|
|
156
153
|
is_collection = ar_association ? ar_association.collection? : association.options[:collection]
|
|
157
154
|
|
|
158
155
|
view = association.options[:view] || :default
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BlueprinterSchema
|
|
4
|
+
# Resolves an attribute's type and nullability from a model.
|
|
5
|
+
#
|
|
6
|
+
# ActiveRecord models (responding to +columns_hash+) infer both the type and
|
|
7
|
+
# nullability from the column. ActiveModel objects (responding to
|
|
8
|
+
# +type_for_attribute+) infer the type from the attribute; nullability is
|
|
9
|
+
# inferred from presence validations when available, otherwise assumed nullable.
|
|
10
|
+
class ModelAttributes
|
|
11
|
+
def initialize(model)
|
|
12
|
+
@model = model
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def type(name)
|
|
16
|
+
if active_record?
|
|
17
|
+
@model.columns_hash[name]&.type
|
|
18
|
+
elsif active_model?
|
|
19
|
+
@model.type_for_attribute(name)&.type
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def nullable?(name)
|
|
24
|
+
if active_record?
|
|
25
|
+
column = @model.columns_hash[name]
|
|
26
|
+
column ? column.null : false
|
|
27
|
+
elsif active_model?
|
|
28
|
+
active_model_nullable?(name)
|
|
29
|
+
else
|
|
30
|
+
false
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def active_record?
|
|
37
|
+
@model.respond_to?(:columns_hash)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def active_model?
|
|
41
|
+
@model.respond_to?(:type_for_attribute)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def active_model_nullable?(name)
|
|
45
|
+
return true unless @model.respond_to?(:validators_on)
|
|
46
|
+
|
|
47
|
+
!presence_validated?(name)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def presence_validated?(name)
|
|
51
|
+
@model.validators_on(name).any? { |validator| validator.kind == :presence }
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/blueprinter_schema.rb
CHANGED
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.
|
|
4
|
+
version: 1.6.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- thisismydesign
|
|
@@ -27,6 +27,7 @@ files:
|
|
|
27
27
|
- compose.yml
|
|
28
28
|
- lib/blueprinter_schema.rb
|
|
29
29
|
- lib/blueprinter_schema/generator.rb
|
|
30
|
+
- lib/blueprinter_schema/model_attributes.rb
|
|
30
31
|
- lib/blueprinter_schema/version.rb
|
|
31
32
|
- sig/blueprinter_schema.rbs
|
|
32
33
|
homepage: https://github.com/thisismydesign/blueprinter_schema
|