Skip to content

Commit 67c8195

Browse files
committed
move API.scope to Routing DSL
1 parent 3563c5b commit 67c8195

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

lib/grape/api.rb

+4-16
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class << self
1313

1414
# Clears all defined routes, endpoints, etc., on this API.
1515
def reset!
16-
@endpoints = []
17-
@routes = nil
16+
reset_endpoints!
17+
reset_routes!
1818
reset_validations!
1919
end
2020

@@ -43,20 +43,10 @@ def call!(env)
4343
instance.call(env)
4444
end
4545

46-
# Create a scope without affecting the URL.
47-
#
48-
# @param _name [Symbol] Purely placebo, just allows to name the scope to
49-
# make the code more readable.
50-
def scope(_name = nil, &block)
51-
within_namespace do
52-
nest(block)
53-
end
54-
end
55-
5646
# (see #cascade?)
5747
def cascade(value = nil)
5848
if value.nil?
59-
inheritable_setting.namespace_inheritable.keys.include?(:cascade) ? !!namespace_inheritable(:cascade) : true
49+
inheritable_setting.namespace_inheritable.keys.include?(:cascade) ? !namespace_inheritable(:cascade).nil? : true
6050
else
6151
namespace_inheritable(:cascade, value)
6252
end
@@ -90,9 +80,7 @@ def inherited(subclass)
9080
def inherit_settings(other_settings)
9181
top_level_setting.inherit_from other_settings.point_in_time_copy
9282

93-
endpoints.each(&:reset_routes!)
94-
95-
@routes = nil
83+
reset_routes!
9684
end
9785
end
9886

lib/grape/dsl/routing.rb

+15
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ def prefix(prefix = nil)
5858
namespace_inheritable(:root_prefix, prefix)
5959
end
6060

61+
# Create a scope without affecting the URL.
62+
#
63+
# @param _name [Symbol] Purely placebo, just allows to name the scope to
64+
# make the code more readable.
65+
def scope(_name = nil, &block)
66+
within_namespace do
67+
nest(block)
68+
end
69+
end
70+
6171
# Do not route HEAD requests to GET requests automatically.
6272
def do_not_route_head!
6373
namespace_inheritable(:do_not_route_head, true)
@@ -175,9 +185,14 @@ def routes
175185

176186
# Remove all defined routes.
177187
def reset_routes!
188+
endpoints.each(&:reset_routes!)
178189
@routes = nil
179190
end
180191

192+
def reset_endpoints!
193+
@endpoints = []
194+
end
195+
181196
# Thie method allows you to quickly define a parameter route segment
182197
# in your API.
183198
#

spec/grape/dsl/routing_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class Dummy
3131
end
3232
end
3333

34+
describe '.scope' do
35+
it 'create a scope without affecting the URL' do
36+
expect(subject).to receive(:within_namespace)
37+
subject.scope {}
38+
end
39+
end
40+
3441
describe '.do_not_route_head!' do
3542
it 'sets do not route head option' do
3643
expect(subject).to receive(:namespace_inheritable).with(:do_not_route_head, true)

0 commit comments

Comments
 (0)