class Blade::Setting::SwaggerTemplate
Public Class Methods
generate_template(model, name)
click to toggle source
# File lib/blade/setting/swagger_template.rb, line 130 def generate_template(model, name) all_routes = Rails.application.routes.routes.to_a routes = all_routes.filter {|route| route.path.spec.to_s.include?("/#{model.pluralize}")} routes.uniq! {|p| p.defaults} render_routes(routes, model, name) end
render_routes(routes, model, name)
click to toggle source
# File lib/blade/setting/swagger_template.rb, line 5 def render_routes(routes, model, name) routes.map! do |route| api_string = "" path = route.path.spec.to_s action = route.defaults[:action] action_map = {update: "更新", index: "列表", create: "创建", destroy: "删除"} action_method = {update: "put", index: "get", create: "post", destroy: "delete"} action_in = {update: "body", index: "query", create: "body", destroy: "query"} ActiveRecord::Base.connection model_class = ActiveSupport::Dependencies.constantize(model.classify) properties = {} isSearch = (action == "index" || action == "destroy") examples = { data: { }, status: { code: "20000", messages: [] } } property_prefix = nil includes_actions = ["index", "create", "update", "destroy"] if !includes_actions.include?(action) next end model_example = {} case action when "create" property_prefix = :create when "update" property_prefix = :update end if (property_prefix.present?) properties[property_prefix] = {type: :object, properties: {}} end if (action == "index" || action == "destroy") properties = [] end required = [] excluded_columns = ["created_at", "deleted_at", "updated_at", "id"] model_class.columns.each do |column| if (!excluded_columns.include?(column.name)) if (action == "index") properties.push("parameter name: :'search[#{column.name}]',description: '#{column.comment}', in: :query, type: :#{column.type} ") end if (property_prefix.present?) properties[property_prefix][:properties][:"#{column.name}"] = {type: "#{column.type}".to_sym} end end model_example[:"#{column.name}"] = column.comment end if (action == "update") properties[:"#{model}_id"] = {type: :string} end if (action == "destroy") properties.push("parameter name: :'#{model}_id', in: :query, type: :integer ") end if (action == "index") examples[:data] = { "#{model.pluralize}": [ model_example ], "total_pages": 1, "total_count": 1 } end if (action == "update" || action == "create") examples[:data] = { "#{model}": model_example } end schema = { type: :object, properties: properties, required: required } render_params = <<File parameter name: "#{model}#{action_map[action.to_sym]}", in: :#{action_in[action.to_sym]},schema: #{schema} File if isSearch render_params = <<File #{properties.join("\n ")} File end if (includes_actions.include?(action.to_s)) api_string = <<file path "#{path.gsub("(.:format)", "").gsub(":id", "{id}")}" do #{action_method[action.to_sym]} '#{model}#{action_map[action.to_sym]}' do tags '#{model.classify} #{name} 模块' security [access_token: [],user_session_key:[]] consumes 'application/json' #{render_params} response '20000', '请求成功' do # noinspection RubyArgCount examples 'application/json' => #{examples} run_test! do |response| data = JSON.parse(response.body) end end response '50000', '请求失败' do run_test! end end end file end api_string end routes.join("\n") end