class JsonschemaSerializer::ActiveRecord
The JsonschemaSerializer::ActiveRecord
class provides a from_model
class method to serialize some ActiveRecord
classes with the minimum effort
Constants
- TYPE_CONVERSIONS
Mapping Ruby types on Jsonschema types. This could be moved to a separate module later
Public Class Methods
Format a ActiveRecord::ConnectionAdapters::<Adapter>::Column as an Hash
Params:
klass
-
+ActiveRecord::ConnectionAdapters::<Adapter>::Column+ column
# File lib/jsonschema_serializer/active_record.rb 140 def format_column_element(col) 141 {}.tap do |h| 142 h[:name] = col.name 143 h[:type] = TYPE_CONVERSIONS[sql_type(col)] || :string 144 # col.default.tap { |d| h[:default] = d if d} 145 end 146 end
Format JsonSchema general attributes such as title, required
Params:
klass
-
ActiveRecord::Base
class name builder
-
JsonschemaSerializer::Builder
an instance of the builder
# File lib/jsonschema_serializer/active_record.rb 45 def format_schema_attributes(klass, builder) 46 builder.title schema_title(klass) 47 required(klass).tap do |required| 48 builder.required(*required) unless required.empty? 49 end 50 end
Format JsonSchema properties
Params:
columns
-
Array
array of formatted builder
-
JsonschemaSerializer::Builder
an instance of the builder
# File lib/jsonschema_serializer/active_record.rb 58 def format_schema_properties(columns, builder) 59 builder.properties.tap do |prop| 60 columns.each do |col| 61 el = format_column_element(col) 62 # Handle basic case of attribute type and attribute name 63 prop.merge! builder.send(el[:type], el[:name]) 64 end 65 end 66 end
Serialize an ActiveRecord
class into a JsonschemaSerializer::Builder
object
Params:
klass
-
ActiveRecord::Base
class name only
-
Array
columns asString
except
-
Array
columns asString
# File lib/jsonschema_serializer/active_record.rb 19 def from_model(klass, only: nil, except: nil) 20 validate_arguments(only, except) 21 JsonschemaSerializer::Builder.build do |b| 22 format_schema_attributes(klass, b) 23 format_schema_properties(selected_columns(klass), b) 24 end 25 end
Filter required class attributes with only/filters attributes This method can be overridden when inheriting from this class
Params:
klass
-
ActiveRecord::Base
class name
# File lib/jsonschema_serializer/active_record.rb 89 def required(klass) 90 required_from_class(klass).tap do |req| 91 return req & @only if @only 92 return req - @except if @except 93 end 94 end
Extract required attributes from ActiveRecord
class implementation This method can be overridden when inheriting from this class
Params:
klass
-
ActiveRecord::Base
class name
# File lib/jsonschema_serializer/active_record.rb 102 def required_from_class(klass) 103 klass.validators.select do |validator| 104 validator.class.to_s == 'ActiveRecord::Validations::PresenceValidator' 105 end.map(&:attributes).flatten 106 end
Extract schema title from ActiveRecord
class This method can be overridden when inheriting from this class
Params:
klass
-
ActiveRecord::Base
class name
# File lib/jsonschema_serializer/active_record.rb 74 def schema_title(klass) 75 klass.model_name.human 76 end
Retrieves the columns and keep/discard some elements if needed
Params:
klass
-
ActiveRecord::Base
class name only
-
Array
columns asString
except
-
Array
columns asString
# File lib/jsonschema_serializer/active_record.rb 115 def selected_columns(klass) 116 klass.columns.dup.tap do |cols| 117 cols.select! { |col| @only.include?(col.name) } if @only 118 cols.reject! { |col| @except.include?(col.name) } if @except 119 end 120 end
Retrieves type from ActiveRecord::ConnectionAdapters::SqlTypeMetadata
Params:
col
-
+ActiveRecord::ConnectionAdapters::<Adapter>::Column+ column
# File lib/jsonschema_serializer/active_record.rb 153 def sql_type(col) 154 return col.sql_type_metadata.type if col.respond_to?(:sql_type_metadata) 155 # Rails 4 backward compatibility 156 col.sql_type.downcase.to_sym 157 end
Raise if only
and except
are both provided
Params:
only
-
Array
columns asString
except
-
Array
columns asString
# File lib/jsonschema_serializer/active_record.rb 33 def validate_arguments(only, except) 34 raise ArgumentError, 'only and except options both provided' if only && except 35 @only = only 36 @except = except 37 end