Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use route.namespace instead instance_variable_get(@options) #931

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ def combine_routes(app, doc_klass)
end

def determine_namespaced_routes(name, parent_route, routes)
if parent_route.nil?
routes.values.flatten
else
parent_route.reject do |route|
!route_path_start_with?(route, name) || !route_instance_variable_equals?(route, name)
end
return routes.values.flatten if parent_route.nil?

parent_route.select do |route|
route_path_start_with?(route, name) || route_namespace_equals?(route, name)
end
end

Expand Down Expand Up @@ -94,20 +92,27 @@ def extract_parent_route(name)
matches.nil? ? route_name : matches[0].delete('/')
end

def route_instance_variable(route)
route.instance_variable_get(:@options)[:namespace]
end
def route_namespace_equals?(route, name)
patterns = Enumerator.new do |yielder|
yielder << "/#{name}"
yielder << "/:version/#{name}"
end

def route_instance_variable_equals?(route, name)
route_instance_variable(route) == "/#{name}" ||
route_instance_variable(route) == "/:version/#{name}"
patterns.any? { |p| route.namespace == p }
end

def route_path_start_with?(route, name)
route_prefix = route.prefix ? "/#{route.prefix}/#{name}" : "/#{name}"
route_versioned_prefix = route.prefix ? "/#{route.prefix}/:version/#{name}" : "/:version/#{name}"
patterns = Enumerator.new do |yielder|
if route.prefix
yielder << "/#{route.prefix}/#{name}"
yielder << "/#{route.prefix}/:version/#{name}"
else
yielder << "/#{name}"
yielder << "/:version/#{name}"
end
end

route.path.start_with?(route_prefix, route_versioned_prefix)
patterns.any? { |p| route.path.start_with?(p) }
end
end

Expand Down