5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# 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.
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 = " parameter name: \"\#{model}\#{action_map[action.to_sym]}\", in: :\#{action_in[action.to_sym]},schema: \#{schema}\n"
if isSearch
render_params = "\#{properties.join(\"\\n \")}\n"
end
if (includes_actions.include?(action.to_s))
api_string = " path \"\#{path.gsub(\"(.:format)\", \"\").gsub(\":id\", \"{id}\")}\" do\n \#{action_method[action.to_sym]} '\#{model}\#{action_map[action.to_sym]}' do\ntags '\#{model.classify} \#{name} \u6A21\u5757'\nsecurity [access_token: [],user_session_key:[]]\nconsumes 'application/json'\n\#{render_params}\nresponse '20000', '\u8BF7\u6C42\u6210\u529F' do\n # noinspection RubyArgCount\n examples 'application/json' =>\n \#{examples}\n run_test! do |response|\n data = JSON.parse(response.body)\n end\nend\nresponse '50000', '\u8BF7\u6C42\u5931\u8D25' do\n run_test!\nend\n end\n end\n"
end
api_string
end
routes.join("\n")
end
|