Skip to content

Commit

Permalink
Support for loading routes from engines
Browse files Browse the repository at this point in the history
Adding a recursion for getting routes form mounted engines.

Also checking if we need to add the api_base_url to the path instead
of removing the api_base_url when loading the path from routes:
doesn't play well with inheritance of api_base_url.
  • Loading branch information
iNecas committed Dec 17, 2014
1 parent 48bfe6c commit d35e0a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
31 changes: 21 additions & 10 deletions lib/apipie/application.rb
Expand Up @@ -29,17 +29,26 @@ def set_resource_id(controller, resource_id)
@controller_to_resource_id[controller] = resource_id
end

def apipie_routes
unless @apipie_api_routes
# ensure routes are loaded
Rails.application.reload_routes! unless Rails.application.routes.routes.any?

regex = Regexp.new("\\A#{Apipie.configuration.api_base_url.values.join('|')}")
@apipie_api_routes = Rails.application.routes.routes.select do |x|
regex =~ x.path.spec.to_s
def rails_routes(route_set = nil)
if route_set.nil? && @rails_routes
return @rails_routes
end
route_set ||= Rails.application.routes
# ensure routes are loaded
Rails.application.reload_routes! unless Rails.application.routes.routes.any?

flatten_routes = []

route_set.routes.each do |route|
if route.app.respond_to?(:routes) && route.app.routes.is_a?(ActionDispatch::Routing::RouteSet)
# recursively go though the moutned engines
flatten_routes.concat(rails_routes(route.app.routes))
else
flatten_routes << route
end
end
@apipie_api_routes

@rails_routes = flatten_routes
end

# the app might be nested when using contraints, namespaces etc.
Expand All @@ -50,10 +59,12 @@ def route_app_controller(app, route)
elsif app.respond_to?(:app)
return route_app_controller(app.app, route)
end
rescue ActionController::RoutingError
# some errors in the routes will not stop us here: just ignoring
end

def routes_for_action(controller, method, args)
routes = apipie_routes.select do |route|
routes = rails_routes.select do |route|
controller == route_app_controller(route.app, route) &&
method.to_s == route.defaults[:action]
end
Expand Down
5 changes: 4 additions & 1 deletion lib/apipie/method_description.rb
Expand Up @@ -104,7 +104,10 @@ def doc_url
end

def create_api_url(api)
path = "#{@resource._api_base_url}#{api.path}"
path = api.path
unless path.start_with?(@resource._api_base_url)
path = "#{@resource._api_base_url}#{path}"
end
path = path[0..-2] if path[-1..-1] == '/'
return path
end
Expand Down

0 comments on commit d35e0a1

Please sign in to comment.