fix api doc generation for rails 3

Change-Id: I15b7323e36eeee5b8104bf0c5eae7d37d87c5db1
Reviewed-on: https://gerrit.instructure.com/27031
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
Product-Review: James Williams  <jamesw@instructure.com>
QA-Review: James Williams  <jamesw@instructure.com>
This commit is contained in:
James Williams 2013-12-09 11:06:43 -07:00
parent ee95910ff3
commit 9725c540ec
4 changed files with 51 additions and 12 deletions

View File

@ -11,7 +11,11 @@ class RouteView < HashView
@route ||= begin
routes = ApiRouteSet::V1.api_methods_for_controller_and_action(@controller, @action)
# Choose shortest route (preferrably without .json suffix)
routes.sort_by { |r| r.segments.join.size }.first
if CANVAS_RAILS2
routes.sort_by { |r| r.segments.join.size }.first
else
routes.sort_by { |r| r.path.spec.to_s.size }.first
end
end
end
@ -26,7 +30,11 @@ class RouteView < HashView
end
def api_path
path = route.segments.inject("") { |str,s| str << s.to_s }
if CANVAS_RAILS2
path = route.segments.inject("") { |str,s| str << s.to_s }
else
path = route.path.spec.to_s
end
path.chop! if path.length > 1 # remove trailing slash
path
end
@ -42,7 +50,13 @@ class RouteView < HashView
end
def verb
route.conditions[:method].to_s.upcase
if CANVAS_RAILS2
route.conditions[:method].to_s.upcase
else
if route.verb.source =~ /\^?(\w*)\$/
$1.upcase
end
end
end
def reqs

View File

@ -1,7 +1,17 @@
<% @routes.each do |route| %>
<% route_path = route.segments.inject("") { |str,s| str << s.to_s }.sub(/\(\.:format\)\?/, '').sub(/\/$/, '')
next if route_path =~ /\.json$/ %>
<h3 class='endpoint'>
<%= route.conditions[:method].to_s.upcase %> <%= route_path %>
</h3>
<% if CANVAS_RAILS2
route_path = route.segments.inject("") { |str,s| str << s.to_s }.sub(/\(\.:format\)\?/, '').sub(/\/$/, '')
verb = route.conditions[:method].to_s.upcase
else
route_path = route.path.spec.to_s.sub("(.json)(.:format)", "")
if route.verb.source =~ /\^?(\w*)\$/
verb = $1.upcase
else
verb = route.verb.source
end
end
next if route_path =~ /\.json$/ %>
<h3 class='endpoint'>
<%= verb %> <%= route_path %>
</h3>
<% end %>

View File

@ -47,9 +47,13 @@ class ApiRouteSet
end
def self.routes_for(prefix)
builder = ActionController::Routing::RouteBuilder.new
segments = builder.segments_for_route_path(prefix)
ActionController::Routing::Routes.routes.select { |r| segments_match(r.segments[0,segments.size], segments) }
if CANVAS_RAILS2
builder = ActionController::Routing::RouteBuilder.new
segments = builder.segments_for_route_path(prefix)
ActionController::Routing::Routes.routes.select { |r| segments_match(r.segments[0,segments.size], segments) }
else
CanvasRails::Application.routes.set.select{|r| r.path.spec.to_s.start_with?(prefix)}
end
end
def self.segments_match(seg1, seg2)
@ -57,7 +61,16 @@ class ApiRouteSet
end
def self.api_methods_for_controller_and_action(controller, action)
self.routes_for(prefix).find_all { |r| r.matches_controller_and_action?(controller, action) }
@routes ||= self.routes_for(prefix)
@routes.find_all { |r| matches_controller_and_action?(r, controller, action) }
end
def self.matches_controller_and_action?(route, controller, action)
if CANVAS_RAILS2
route.matches_controller_and_action?(controller, action)
else
route.requirements[:controller] == controller && route.requirements[:action] == action
end
end
def method_missing(m, *a, &b)

View File

@ -22,6 +22,8 @@ Bundler.setup
require 'action_controller'
if CANVAS_RAILS2
require 'fake_rails3_routes'
else
CanvasRails::Application.routes.disable_clear_and_finalize = true
end
# load routing files, including those in plugins