module ModelApi::OpenApiExtensions::ClassMethods

Public Instance Methods

add_open_api_action(action, operation, opts = {}) click to toggle source
# File lib/model-api/open_api_extensions.rb, line 4
def add_open_api_action(action, operation, opts = {})
  return unless respond_to?(:open_api_action) # Must have open_api gem installed
  opts = opts.merge(action: action, operation: operation)
  if ENV['ADMIN'].present? && ENV['ADMIN'].to_s != '0'
    opts[:admin_content] = true
  elsif opts[:admin_only]
    open_api_action action, hidden: :true
    return
  end
  open_api_spec = {}
  open_api_spec[:description] = opts[:description] if opts.include?(:description)
  response_class = opts[:response] || model_class
  if operation == :index || opts[:collection]
    response = Utils.define_api_collection_response(self, response_class, opts)
    open_api_spec[:query_string] = Utils.filter_and_sort_params(self, response_class, opts)
  else
    response = Utils.define_api_response(self, response_class,
        opts.merge(operation: :show))
  end
  open_api_spec[:responses] = { 200 => { schema: response } } if response.present?
  if [:create, :update, :patch].include?(operation)
    payload = opts[:payload] || model_class
    if payload.present?
      open_api_spec[:body] = { description: 'Payload', schema: Utils.define_open_api_object(
          self, payload, opts.merge(object_context: :payload)) }
    end
  end
  open_api_action action, open_api_spec
end