class SwaggerApi::ComponentSchema

Attributes

controller[RW]

Public Instance Methods

create() click to toggle source
# File lib/swagger_api/component_schema.rb, line 9
def create
  {
    required: required,
    properties: properties.merge(virtual_properties),
    type: 'object'
  }
end

Private Instance Methods

absence_attributes() click to toggle source
# File lib/swagger_api/component_schema.rb, line 40
def absence_attributes
  @absence_attributes ||= model.validators.map do |validator|
    validator.attributes if validator.is_a?(ActiveRecord::Validations::AbsenceValidator)
  end.compact.flatten.uniq.map(&:to_s)
end
clean_required_attributes() click to toggle source
# File lib/swagger_api/component_schema.rb, line 24
def clean_required_attributes
  required_attributes.map do |attribute|
    if model.reflect_on_all_associations(:belongs_to).map(&:name).map(&:to_s).include?(attribute)
      "#{attribute}_id"
    else
      attribute
    end
  end.compact.uniq.map(&:to_s)
end
column_should_skip(column_name) click to toggle source
# File lib/swagger_api/component_schema.rb, line 60
def column_should_skip(column_name)
  column_name.end_with?('_iv') || absence_attributes.include?(column_name)
end
model() click to toggle source
# File lib/swagger_api/component_schema.rb, line 64
def model
  controller.model.constantize
end
properties() click to toggle source
# File lib/swagger_api/component_schema.rb, line 46
def properties
  columns.map do |column|
    next if column_should_skip column.name
    attribute_name = column.name.gsub(/^encrypted_/, '')
    [attribute_name, ColumnSchema.new(column: column).create]
  end.compact.to_h
end
required() click to toggle source
# File lib/swagger_api/component_schema.rb, line 19
def required
  controller.try(:columns).try(:required) ||
    columns.map(&:name).map { |name| name.gsub(/^encrypted_/, '') } & clean_required_attributes
end
required_attributes() click to toggle source
# File lib/swagger_api/component_schema.rb, line 34
def required_attributes
  model.validators.map do |validator|
    validator.attributes if validator.is_a?(ActiveRecord::Validations::PresenceValidator)
  end.compact.flatten.uniq.map(&:to_s)
end
virtual_properties() click to toggle source
# File lib/swagger_api/component_schema.rb, line 54
def virtual_properties
  (controller.try(:columns).try(:virtual) || []).map do |property|
    [property.name, property.schema.to_h]
  end.to_h
end