Skip to content

Commit 017e762

Browse files
committed
Add routes as a non overridable methods so that memoization works properly. Added a better specs so that a private method is not expected and memoization is really tested.
1 parent 7cfc817 commit 017e762

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

lib/grape/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Grape
55
# should subclass this class in order to build an API.
66
class API
77
# Class methods that we want to call on the API rather than on the API object
8-
NON_OVERRIDABLE = %i[call call! configuration compile! inherited recognize_path].freeze
8+
NON_OVERRIDABLE = %i[call call! configuration compile! inherited recognize_path routes].freeze
99

1010
Helpers = Grape::DSL::Helpers::BaseHelper
1111

lib/grape/api/instance.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ def recognize_path(path)
103103

104104
protected
105105

106-
def prepare_routes
107-
endpoints.map(&:routes).flatten
108-
end
109-
110106
# Execute first the provided block, then each of the
111107
# block passed in. Allows for simple 'before' setups
112108
# of settings stack pushes.

lib/grape/dsl/routing.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def namespace(space = nil, options = {}, &block)
195195

196196
# An array of API routes.
197197
def routes
198-
@routes ||= prepare_routes
198+
@routes ||= endpoints.map(&:routes).flatten
199199
end
200200

201201
# Remove all defined routes.

spec/grape/dsl/routing_spec.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,22 +274,23 @@ def self.route_setting_hash
274274
end
275275

276276
describe '.routes' do
277-
let(:routes) { Object.new }
277+
let(:main_app) { Class.new(Grape::API) }
278+
let(:first_app) { Class.new(Grape::API) }
279+
let(:second_app) { Class.new(Grape::API) }
278280

279-
it 'returns value received from #prepare_routes' do
280-
expect(subject).to receive(:prepare_routes).and_return(routes)
281-
expect(subject.routes).to eq routes
281+
before do
282+
main_app.mount(first_app => '/first_app', second_app => '/second_app')
282283
end
283284

284-
context 'when #routes was already called once' do
285-
before do
286-
allow(subject).to receive(:prepare_routes).and_return(routes)
287-
subject.routes
288-
end
285+
it 'returns flatten endpoints routes' do
286+
expect(main_app.endpoints).not_to be_empty
287+
expect(main_app.routes).to eq(main_app.endpoints.map(&:routes).flatten)
288+
end
289289

290-
it 'does not call prepare_routes again' do
291-
expect(subject).not_to receive(:prepare_routes)
292-
expect(subject.routes).to eq routes
290+
context 'when #routes was already called once' do
291+
it 'memoizes' do
292+
object_id = main_app.routes.object_id
293+
expect(main_app.routes.object_id).to eq(object_id)
293294
end
294295
end
295296
end

0 commit comments

Comments
 (0)