mirror of
https://github.com/ditkrg/jsonapi-swagger.git
synced 2026-01-22 22:06:50 +00:00
support jsonapi rails
This commit is contained in:
parent
1150c75b00
commit
8e32f7dc88
@ -86,15 +86,15 @@ module Jsonapi
|
|||||||
end
|
end
|
||||||
|
|
||||||
def resource_klass
|
def resource_klass
|
||||||
"#{model_class_name}Resource".safe_constantize
|
@resource_klass ||= Jsonapi::Swagger::Resource.with(model_class_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
resource_klass._attributes.except(:id)
|
resource_klass.attributes.except(:id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def relationships
|
def relationships
|
||||||
resource_klass._relationships
|
resource_klass.relationships
|
||||||
end
|
end
|
||||||
|
|
||||||
def sortable_fields
|
def sortable_fields
|
||||||
@ -113,6 +113,10 @@ module Jsonapi
|
|||||||
resource_klass.filters
|
resource_klass.filters
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def mutable?
|
||||||
|
resource_klass.mutable?
|
||||||
|
end
|
||||||
|
|
||||||
def columns_with_comment(need_encoding: true)
|
def columns_with_comment(need_encoding: true)
|
||||||
@columns_with_comment ||= {}.tap do |clos|
|
@columns_with_comment ||= {}.tap do |clos|
|
||||||
model_klass.columns.each do |col|
|
model_klass.columns.each do |col|
|
||||||
|
|||||||
@ -128,7 +128,7 @@
|
|||||||
},
|
},
|
||||||
description: tt(:related_ids, model: relation_name_camelize)
|
description: tt(:related_ids, model: relation_name_camelize)
|
||||||
}
|
}
|
||||||
if relation.belongs_to?
|
if relation.try(:belongs_to?)
|
||||||
relat_props[relation_name][:properties][:data] = {
|
relat_props[relation_name][:properties][:data] = {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
@ -277,7 +277,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if resource_klass.mutable?
|
if mutable?
|
||||||
doc['paths']["/#{route_resouces}"].merge!({
|
doc['paths']["/#{route_resouces}"].merge!({
|
||||||
post: {
|
post: {
|
||||||
summary: "#{route_resouces} #{tt(:create)}",
|
summary: "#{route_resouces} #{tt(:create)}",
|
||||||
|
|||||||
@ -173,7 +173,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
<% if resource_klass.mutable? -%>
|
<% if mutable? -%>
|
||||||
path '/<%= route_resouces %>' do
|
path '/<%= route_resouces %>' do
|
||||||
post '<%= route_resouces %> <%= t(:create) %>' do
|
post '<%= route_resouces %> <%= t(:create) %>' do
|
||||||
tags '<%= route_resouces %>'
|
tags '<%= route_resouces %>'
|
||||||
@ -204,7 +204,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
<%= relation_name %>: {
|
<%= relation_name %>: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
<% if relation.belongs_to? -%>
|
<% if relation.try(:belongs_to?) -%>
|
||||||
data: {
|
data: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
require 'jsonapi/swagger/version'
|
require 'jsonapi/swagger/version'
|
||||||
require 'jsonapi/swagger/railtie' if defined?(Rails)
|
require 'jsonapi/swagger/railtie' if defined?(Rails)
|
||||||
require 'jsonapi/swagger/json'
|
require 'jsonapi/swagger/json'
|
||||||
|
require 'jsonapi/swagger/resource'
|
||||||
|
|
||||||
module Jsonapi
|
module Jsonapi
|
||||||
module Swagger
|
module Swagger
|
||||||
|
|||||||
26
lib/jsonapi/swagger/resource.rb
Normal file
26
lib/jsonapi/swagger/resource.rb
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
require 'forwardable'
|
||||||
|
module Jsonapi
|
||||||
|
module Swagger
|
||||||
|
class Resource
|
||||||
|
def self.with(model_class_name)
|
||||||
|
if Object.const_defined?("#{model_class_name}Resource")
|
||||||
|
@resource_class = "#{model_class_name}Resource".safe_constantize
|
||||||
|
unless @resource_class < JSONAPI::Resource
|
||||||
|
raise Jsonapi::Swagger::Error, "#{@resource_class.class} is not Subclass of JSONAPI::Resource!"
|
||||||
|
end
|
||||||
|
require 'jsonapi/swagger/resources/jsonapi_resource'
|
||||||
|
return Jsonapi::Swagger::JsonapiResource.new(@resource_class)
|
||||||
|
elsif Object.const_defined?("Serializable#{model_class_name}")
|
||||||
|
@resource_class = "Serializable#{model_class_name}".safe_constantize
|
||||||
|
unless @resource_class < JSONAPI::Serializable::Resource
|
||||||
|
raise Jsonapi::Swagger::Error, "#{@resource_class.class} is not Subclass of JSONAPI::Serializable::Resource!"
|
||||||
|
end
|
||||||
|
require 'jsonapi/swagger/resources/serializable_resource'
|
||||||
|
return Jsonapi::Swagger::SerializableResource.new(@resource_class)
|
||||||
|
else
|
||||||
|
raise Jsonapi::Swagger::Error, "#{model_class_name} not support!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
18
lib/jsonapi/swagger/resources/jsonapi_resource.rb
Normal file
18
lib/jsonapi/swagger/resources/jsonapi_resource.rb
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
require 'forwardable'
|
||||||
|
module Jsonapi
|
||||||
|
module Swagger
|
||||||
|
class JsonapiResource
|
||||||
|
extend Forwardable
|
||||||
|
|
||||||
|
def_delegators :@jr, :_attributes, :_relationships, :sortable_fields,
|
||||||
|
:creatable_fields, :updatable_fields, :filters, :mutable?
|
||||||
|
|
||||||
|
def initialize(jr)
|
||||||
|
@jr = jr
|
||||||
|
end
|
||||||
|
|
||||||
|
alias attributes _attributes
|
||||||
|
alias relationships _relationships
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
45
lib/jsonapi/swagger/resources/serializable_resource.rb
Normal file
45
lib/jsonapi/swagger/resources/serializable_resource.rb
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
require 'forwardable'
|
||||||
|
module Jsonapi
|
||||||
|
module Swagger
|
||||||
|
class SerializableResource
|
||||||
|
extend Forwardable
|
||||||
|
|
||||||
|
def_delegators :@sr, :type_val, :attribute_blocks, :relationship_blocks, :link_blocks
|
||||||
|
|
||||||
|
def initialize(sr)
|
||||||
|
@sr = sr
|
||||||
|
end
|
||||||
|
|
||||||
|
alias attributes attribute_blocks
|
||||||
|
|
||||||
|
def relationships
|
||||||
|
{}.tap do |relations|
|
||||||
|
relationship_blocks.each do |rel, block|
|
||||||
|
relations[rel] = OpenStruct.new(class_name: rel.to_s)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# TODO: from jsonapi serializable resource
|
||||||
|
def sortable_fields
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
|
def creatable_fields
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
|
def updatable_fields
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
|
def filters
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
|
def mutable?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user