Class: JsonschemaSerializer::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonschema_serializer/active_record.rb

Overview

The JsonschemaSerializer::ActiveRecord class provides a from_model class method to serialize some ActiveRecord classes with the minimum effort

Constant Summary collapse

TYPE_CONVERSIONS =

Mapping Ruby types on Jsonschema types. This could be moved to a separate module later

{
  boolean: :boolean,
  datetime: :string,
  decimal: :number,
  float: :number,
  integer: :integer,
  text: :string,
  varchar: :string
}.freeze

Class Method Summary collapse

Class Method Details

.format_column_element(col) ⇒ Object

Format a ActiveRecord::ConnectionAdapters::<Adapter>::Column as an Hash

Params:

klass

ActiveRecord::ConnectionAdapters::<Adapter>::Column column



140
141
142
143
144
145
146
# File 'lib/jsonschema_serializer/active_record.rb', line 140

def format_column_element(col)
  {}.tap do |h|
    h[:name] = col.name
    h[:type] = TYPE_CONVERSIONS[sql_type(col)] || :string
    # col.default.tap { |d| h[:default] = d if d}
  end
end

.format_schema_attributes(klass, builder) ⇒ Object

Format JsonSchema general attributes such as title, required

Params:

klass

ActiveRecord::Base class name

builder

JsonschemaSerializer::Builder an instance of the builder



45
46
47
48
49
50
# File 'lib/jsonschema_serializer/active_record.rb', line 45

def format_schema_attributes(klass, builder)
  builder.title schema_title(klass)
  required(klass).tap do |required|
    builder.required(*required) unless required.empty?
  end
end

.format_schema_properties(columns, builder) ⇒ Object

Format JsonSchema properties

Params:

columns

Array array of formatted

builder

JsonschemaSerializer::Builder an instance of the builder



58
59
60
61
62
63
64
65
66
# File 'lib/jsonschema_serializer/active_record.rb', line 58

def format_schema_properties(columns, builder)
  builder.properties.tap do |prop|
    columns.each do |col|
      el = format_column_element(col)
      # Handle basic case of attribute type and attribute name
      prop.merge! builder.send(el[:type], el[:name])
    end
  end
end

.from_model(klass, only: nil, except: nil) ⇒ Object

Serialize an ActiveRecord class into a JsonschemaSerializer::Builder object

Params:

klass

ActiveRecord::Base class name

only

Array columns as String

except

Array columns as String



19
20
21
22
23
24
25
# File 'lib/jsonschema_serializer/active_record.rb', line 19

def from_model(klass, only: nil, except: nil)
  validate_arguments(only, except)
  JsonschemaSerializer::Builder.build do |b|
    format_schema_attributes(klass, b)
    format_schema_properties(selected_columns(klass), b)
  end
end

.required(klass) ⇒ Object

Filter required class attributes with only/filters attributes This method can be overridden when inheriting from this class

Params:

klass

ActiveRecord::Base class name



89
90
91
92
93
94
# File 'lib/jsonschema_serializer/active_record.rb', line 89

def required(klass)
  required_from_class(klass).tap do |req|
    return req & @only if @only
    return req - @except if @except
  end
end

.required_from_class(klass) ⇒ Object

Extract required attributes from ActiveRecord class implementation This method can be overridden when inheriting from this class

Params:

klass

ActiveRecord::Base class name



102
103
104
105
106
# File 'lib/jsonschema_serializer/active_record.rb', line 102

def required_from_class(klass)
  klass.validators.select do |validator|
    validator.class.to_s == 'ActiveRecord::Validations::PresenceValidator'
  end.map(&:attributes).flatten
end

.schema_title(klass) ⇒ Object

Extract schema title from ActiveRecord class This method can be overridden when inheriting from this class

Params:

klass

ActiveRecord::Base class name



74
75
76
# File 'lib/jsonschema_serializer/active_record.rb', line 74

def schema_title(klass)
  klass.model_name.human
end

.selected_columns(klass) ⇒ Object

Retrieves the columns and keep/discard some elements if needed

Params:

klass

ActiveRecord::Base class name

only

Array columns as String

except

Array columns as String



115
116
117
118
119
120
# File 'lib/jsonschema_serializer/active_record.rb', line 115

def selected_columns(klass)
  klass.columns.dup.tap do |cols|
    cols.select! { |col| @only.include?(col.name) } if @only
    cols.reject! { |col| @except.include?(col.name) } if @except
  end
end

.sql_type(col) ⇒ Object

Retrieves type from ActiveRecord::ConnectionAdapters::SqlTypeMetadata

Params:

col

ActiveRecord::ConnectionAdapters::<Adapter>::Column column



153
154
155
156
157
# File 'lib/jsonschema_serializer/active_record.rb', line 153

def sql_type(col)
  return col..type if col.respond_to?(:sql_type_metadata)
  # Rails 4 backward compatibility
  col.sql_type.downcase.to_sym
end

.validate_arguments(only, except) ⇒ Object

Raise if only and except are both provided

Params:

only

Array columns as String

except

Array columns as String

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/jsonschema_serializer/active_record.rb', line 33

def validate_arguments(only, except)
  raise ArgumentError, 'only and except options both provided' if only && except
  @only = only
  @except = except
end