module JsonapiSwaggerHelpers::Writeable

Public Class Methods

included(klass) click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 5
def self.included(klass)
  klass.class_eval do
    attr_reader :node,
                :controller,
                :description,
                :tags,
                :singular
  end
end
new(node, controller, description: nil, tags: [], singular: false) click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 15
def initialize(node, controller, description: nil, tags: [], singular: false)
  @node = node
  @controller = controller
  @resource = controller._jsonapi_compliable
  @description = description || default_description
  @tags = tags
  @singular = singular
end

Public Instance Methods

action_name() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 36
def action_name
  raise 'override me'
end
all_tags() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 48
def all_tags
  tags + payload_tags
end
context() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 62
def context
  JsonapiSwaggerHelpers.docs_controller
end
default_description() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 40
def default_description
  "#{action_name.to_s.capitalize} Action"
end
define_schema() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 100
def define_schema
  generate_request_schema!

  _self = self
  context.send(:swagger_schema, :"#{strong_resource.name}_#{action_name}") do
    _self.strong_resource.attributes.each_pair do |attribute, config|
      property attribute do
        key :type, config[:type]
      end
    end
  end

  _self.each_strong_relation(_self.strong_resource) do |relation_name, relation_config|
    context.send(:swagger_schema, :"#{strong_resource.name}_#{relation_name}_#{action_name}") do
      relation_config[:resource].attributes.each_pair do |attribute, config|
        property attribute do
          key :type, config[:type]
        end
      end
    end
  end
end
each_strong_relation(strong_resource) { |relation_name, relation_config| ... } click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 90
def each_strong_relation(strong_resource)
  strong_resource.relations.each_pair do |relation_name, relation_config|
    yield relation_name, relation_config

    each_strong_relation(relation_config[:resource]) do |sub_relation_name, sub_relation_config|
      yield sub_relation_name, sub_relation_config
    end
  end
end
generate() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 123
def generate
  raise 'override me'
end
generate_request_schema!() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 74
def generate_request_schema!
  _self = self

  JsonapiSwaggerHelpers.docs_controller.send(:swagger_schema, request_schema_id) do
    property _self.strong_resource.name do
      key :'$ref', :"#{_self.strong_resource.name}_#{_self.action_name}"
    end

    _self.each_strong_relation(_self.strong_resource) do |relation_name, _relation_config|
      property relation_name do
        key :'$ref', :"#{_self.strong_resource.name}_#{relation_name}_#{_self.action_name}"
      end
    end
  end
end
operation_id() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 44
def operation_id
  "#{controller.name.gsub('::', '-')}-#{action_name}"
end
payload_tags() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 52
def payload_tags
  tags = [:"payload-#{strong_resource.name}_#{action_name}"]

  strong_resource.relations.each_pair do |relation_name, _relation_config|
    tags << :"payload-#{strong_resource.name}_#{relation_name}_#{action_name}"
  end

  tags
end
request_schema_id() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 70
def request_schema_id
  "#{operation_id}_#{action_name}_request"
end
resource() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 24
def resource
  @resource = controller._jsonapi_compliable
  if @resource.is_a?(Hash)
    @resource = @resource[action_name]
  end
  @resource
end
strong_resource() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 66
def strong_resource
  controller._strong_resources[action_name]
end
util() click to toggle source
# File lib/jsonapi_swagger_helpers/writeable.rb, line 32
def util
  JsonapiSwaggerHelpers::Util
end