Class: YARD::Rails::Plugin::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/yard-rails_plugin/routes.rb

Overview

Handles Rails’s route documentations

Instance Method Summary collapse

Constructor Details

#initializeRoutes

Returns a new instance of Routes.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yard-rails_plugin/routes.rb', line 10

def initialize
  puts '[rails-plugin] Analyzing Routes...'
  @routes = load_routes.collect do |route|
    next if route.path.spec.to_s =~ %r{/rails/*|^/assets|^/cable}

    reqs = route.requirements.dup

    next if reqs.blank?

    { name: route.name.to_s,
      verb: route.verb.to_s,
      path: route.path.spec.to_s,
      action: reqs[:action],
      controller: get_controller(reqs),
      rack_app: get_rack_app(route),
      constraints: get_constraints(reqs) }
  end
  @routes.compact!
end

Instance Method Details

#enrich_controllersObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/yard-rails_plugin/routes.rb', line 69

def enrich_controllers
  @routes.each do |r|
    next unless r[:controller]

    node = YARD::Registry.resolve(nil, r[:controller], true)
    next if node.nil?

    (node[:routes] ||= []) << r

    next unless r[:action]

    node = YARD::Registry.resolve(nil, "#{r[:controller]}##{r[:action]}", true)
    next if node.nil?

    (node[:routes] ||= []) << r
  end
end

#generate_routes_description_file(filename) ⇒ Object



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
# File 'lib/yard-rails_plugin/routes.rb', line 30

def generate_routes_description_file(filename)
  template = %q(
  <html>
  <head></head>
  <body>
    <h1>Routes</h1>
    <table>
      <tr style='background: #EAF0FF; font-weight: bold; line-height: 28px; text-align: left'>
        <th>Rails Method</th>
        <th>Verb</th>
        <th>Endpoint</th>
        <th>Destination</th>
      </tr>
      <% @routes.each_with_index do |r, i|
        next if r == nil
      %>
      <tr class='<%=(i.even? ? 'even' : 'odd')%>'>
        <td><%= r[:name] %></td>
        <td><%= r[:verb] %></td>
        <td><%= r[:path].gsub(/(:|\*)\w+/) do |m|
          "<span style='font-family: monospace; color: green'>#{m}</span>"
        end %></td>
        <td><%= r[:rack_app].present? ? "<pre>#{r[:rack_app].inspect} #{r[:constraints]}</pre>" :
          "{#{r[:controller]} #{r[:controller]}}##{r[:action]}  #{r[:constraints]}" %></td>
      </tr>
      <% end %>
      </table>
    </body>
  </html>).gsub(/^\s+/, '')
  erb = ERB.new(template, trim_mode: '%<>')

  b = binding

  File.write(
    File.join(Dir.pwd, filename),
    erb.result(b)
  )
end