diff --git a/lib/generators/jsonapi/swagger/swagger_generator.rb b/lib/generators/jsonapi/swagger/swagger_generator.rb index 1969ef2..65408c4 100644 --- a/lib/generators/jsonapi/swagger/swagger_generator.rb +++ b/lib/generators/jsonapi/swagger/swagger_generator.rb @@ -86,15 +86,15 @@ module Jsonapi end def resource_klass - "#{model_class_name}Resource".safe_constantize + @resource_klass ||= Jsonapi::Swagger::Resource.with(model_class_name) end def attributes - resource_klass._attributes.except(:id) + resource_klass.attributes.except(:id) end def relationships - resource_klass._relationships + resource_klass.relationships end def sortable_fields @@ -113,6 +113,10 @@ module Jsonapi resource_klass.filters end + def mutable? + resource_klass.mutable? + end + def columns_with_comment(need_encoding: true) @columns_with_comment ||= {}.tap do |clos| model_klass.columns.each do |col| diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 7ec1650..8c3a44c 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -128,7 +128,7 @@ }, description: tt(:related_ids, model: relation_name_camelize) } - if relation.belongs_to? + if relation.try(:belongs_to?) relat_props[relation_name][:properties][:data] = { type: :object, properties: { @@ -277,7 +277,7 @@ } } -if resource_klass.mutable? +if mutable? doc['paths']["/#{route_resouces}"].merge!({ post: { summary: "#{route_resouces} #{tt(:create)}", diff --git a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb index e19e6fe..1099dae 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb @@ -173,7 +173,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do end end -<% if resource_klass.mutable? -%> +<% if mutable? -%> path '/<%= route_resouces %>' do post '<%= route_resouces %> <%= t(:create) %>' do tags '<%= route_resouces %>' @@ -204,7 +204,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do <%= relation_name %>: { type: :object, properties: { -<% if relation.belongs_to? -%> +<% if relation.try(:belongs_to?) -%> data: { type: :object, properties: { diff --git a/lib/jsonapi/swagger.rb b/lib/jsonapi/swagger.rb index 36febac..4084ce6 100644 --- a/lib/jsonapi/swagger.rb +++ b/lib/jsonapi/swagger.rb @@ -3,6 +3,7 @@ require 'jsonapi/swagger/version' require 'jsonapi/swagger/railtie' if defined?(Rails) require 'jsonapi/swagger/json' +require 'jsonapi/swagger/resource' module Jsonapi module Swagger diff --git a/lib/jsonapi/swagger/resource.rb b/lib/jsonapi/swagger/resource.rb new file mode 100644 index 0000000..b7f54e7 --- /dev/null +++ b/lib/jsonapi/swagger/resource.rb @@ -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 \ No newline at end of file diff --git a/lib/jsonapi/swagger/resources/jsonapi_resource.rb b/lib/jsonapi/swagger/resources/jsonapi_resource.rb new file mode 100644 index 0000000..a62f082 --- /dev/null +++ b/lib/jsonapi/swagger/resources/jsonapi_resource.rb @@ -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 \ No newline at end of file diff --git a/lib/jsonapi/swagger/resources/serializable_resource.rb b/lib/jsonapi/swagger/resources/serializable_resource.rb new file mode 100644 index 0000000..2177476 --- /dev/null +++ b/lib/jsonapi/swagger/resources/serializable_resource.rb @@ -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 \ No newline at end of file