mirror of
https://github.com/ditkrg/jsonapi-swagger.git
synced 2026-01-23 06:16:50 +00:00
add jsonapi swagger generator
This commit is contained in:
parent
02404a07a0
commit
46d3c3b366
@ -1,48 +1,68 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Jsonapi
|
module Jsonapi
|
||||||
class SwaggerGenerator < ::Rails::Generators::NamedBase
|
class SwaggerGenerator < Rails::Generators::NamedBase
|
||||||
|
desc 'Create a JSONAPI Swagger.'
|
||||||
source_root File.expand_path('templates', __dir__)
|
source_root File.expand_path('templates', __dir__)
|
||||||
|
|
||||||
def copy_serializable_file
|
def create_swagger_file
|
||||||
template 'swagger.rb.erb',
|
swagger_file = File.join(
|
||||||
File.join('spec/requests', class_path,
|
'spec/requests',
|
||||||
"#{spec_file_name}.rb")
|
class_path,
|
||||||
|
spec_file_name
|
||||||
|
)
|
||||||
|
template 'swagger.rb.erb', swagger_file
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def spec_file_name
|
def spec_file_name
|
||||||
"#{file_name}_spec.rb"
|
"#{file_name.downcase.pluralize}_spec.rb"
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_class_name
|
def resouces_name
|
||||||
(class_path + [serializable_file_name]).map!(&:camelize).join('::')
|
model_class_name.pluralize
|
||||||
|
end
|
||||||
|
|
||||||
|
def route_resouces
|
||||||
|
resouces_name.downcase.gsub('::', '/')
|
||||||
|
end
|
||||||
|
|
||||||
|
def model_class_name
|
||||||
|
(class_path + [file_name]).map!(&:camelize).join("::")
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_klass
|
def model_klass
|
||||||
# TODO(beauby): Ensure the model class exists.
|
model_class_name.safe_constantize
|
||||||
class_name.safe_constantize
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def type
|
def resource_klass
|
||||||
model_klass.model_name.plural
|
"#{model_class_name}Resource".safe_constantize
|
||||||
end
|
end
|
||||||
|
|
||||||
def attr_names
|
def attributes
|
||||||
attrs = model_klass.new.attribute_names - ['id']
|
resource_klass._attributes.except(:id)
|
||||||
fk_attrs = model_klass.reflect_on_all_associations(:belongs_to)
|
|
||||||
.map(&:foreign_key)
|
|
||||||
attrs - fk_attrs
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_one_rel_names
|
def relationships
|
||||||
model_klass.reflect_on_all_associations(:has_one).map(&:name) +
|
resource_klass._relationships
|
||||||
model_klass.reflect_on_all_associations(:belongs_to).map(&:name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_many_rel_names
|
def columns_with_comment
|
||||||
model_klass.reflect_on_all_associations(:has_many).map(&:name)
|
@columns_with_comment ||= {}.tap do |clos|
|
||||||
|
model_klass.columns.each do |col|
|
||||||
|
clos[col.name.to_sym] = { type: swagger_type(col.type), comment: safe_encode(col.comment) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def swagger_type(type)
|
||||||
|
case type
|
||||||
|
when :bigint, :integer then 'integer'
|
||||||
|
else 'string'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def safe_encode(comment)
|
||||||
|
comment&.force_encoding('ASCII-8BIT')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Loading…
Reference in New Issue
Block a user