Skip to content

Commit 84e5ec6

Browse files
committed
Merge pull request #1286 from totothink/master
clean Endpoint
2 parents 126f561 + 4a69b30 commit 84e5ec6

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

lib/grape/endpoint.rb

+22-18
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ module Grape
55
# from inside a `get`, `post`, etc.
66
class Endpoint
77
include Grape::DSL::Settings
8+
include Grape::DSL::InsideRoute
89

910
attr_accessor :block, :source, :options
1011
attr_reader :env, :request, :headers, :params
1112

12-
include Grape::DSL::InsideRoute
13-
1413
class << self
1514
def new(*args, &block)
1615
if self == Endpoint
@@ -115,7 +114,7 @@ def routes
115114
end
116115

117116
def reset_routes!
118-
endpoints.map(&:reset_routes!) if endpoints
117+
endpoints.each(&:reset_routes!) if endpoints
119118
@namespace = nil
120119
@routes = nil
121120
end
@@ -126,7 +125,7 @@ def mount_in(route_set)
126125
e.mount_in(route_set)
127126
end
128127
else
129-
@routes = nil
128+
reset_routes!
130129

131130
routes.each do |route|
132131
methods = [route.route_method]
@@ -243,20 +242,7 @@ def run
243242

244243
run_filters before_validations, :before_validation
245244

246-
# Retrieve validations from this namespace and all parent namespaces.
247-
validation_errors = []
248-
249-
route_setting(:saved_validations).each do |validator|
250-
begin
251-
validator.validate(@request)
252-
rescue Grape::Exceptions::Validation => e
253-
validation_errors << e
254-
end
255-
end
256-
257-
if validation_errors.any?
258-
fail Grape::Exceptions::ValidationErrors, errors: validation_errors, headers: header
259-
end
245+
run_validators validations, request
260246

261247
run_filters after_validations, :after_validation
262248

@@ -346,6 +332,20 @@ def lazy_initialize!
346332
end
347333
end
348334

335+
def run_validators(validators, request)
336+
validation_errors = []
337+
338+
validators.each do |validator|
339+
begin
340+
validator.validate(request)
341+
rescue Grape::Exceptions::Validation => e
342+
validation_errors << e
343+
end
344+
end
345+
346+
validation_errors.any? && fail(Grape::Exceptions::ValidationErrors, errors: validation_errors, headers: header)
347+
end
348+
349349
def run_filters(filters, type = :other)
350350
ActiveSupport::Notifications.instrument('endpoint_run_filters.grape', endpoint: self, filters: filters, type: type) do
351351
(filters || []).each do |filter|
@@ -371,5 +371,9 @@ def after_validations
371371
def afters
372372
namespace_stackable(:afters) || []
373373
end
374+
375+
def validations
376+
route_setting(:saved_validations) || []
377+
end
374378
end
375379
end

0 commit comments

Comments
 (0)