Class: Lina::ApidocController

Inherits:
BaseController show all
Defined in:
app/controllers/lina/apidoc_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



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
# File 'app/controllers/lina/apidoc_controller.rb', line 5

def index
  all_routes = Rails.application.routes.routes
  inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)

  # 过滤出所有 API 相关的路由
  api_routes = []
  @user_api_controllers = user_api_controllers
  @user_api_controllers.each do |controller|
    api_routes += inspector.send(:filter_routes, controller)
  end

  @routes = inspector.send(:collect_routes, api_routes)
  # add schema info to route
  @routes.select! do |route|
    controller, action = route[:reqs].split('#')
    route[:path].gsub!('(.:format)', '')
    route[:schema] = str2controller(controller).action_schema(action)
    str2controller(controller).action_methods.include?(action)
  end
  @_tree = Lina::Tree.new(@routes)
  @tree = @_tree.to_tree
  respond_to do |format|
    format.html
    format.json { render json: @tree }
  end
end