Skip to content

Commit 426d645

Browse files
committed
Merge pull request #527 from myitcv/before_validation_callback
Make before_validation its own distinct callback
2 parents 535fd7d + 6c56e52 commit 426d645

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Next Release
99
* [#525](https://github.com/intridea/grape/pull/525): The default status code returned from `error!` has been changed from 403 to 500 - [@dblock](https://github.com/dblock).
1010
* [#526](https://github.com/intridea/grape/pull/526): Allow specifying headers in `error!` - [@dblock](https://github.com/dblock).
1111
* [#523](https://github.com/intridea/grape/pull/523): Aliased `before` as `before_validation` - [@myitcv](https://github.com/myitcv).
12+
* [#527](https://github.com/intridea/grape/pull/527): `before_validation` now a distinct callback (supersedes [#523](https://github.com/intridea/grape/pull/523)) - [@myitcv](https://github.com/myitcv).
1213
* Your contribution here.
1314

1415
#### Fixes

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,11 +1211,12 @@ Blocks can be executed before or after every API call, using `before`, `after`,
12111211

12121212
Before and after callbacks execute in the following order:
12131213

1214-
1. `before` and `before_validation` (these are aliases - no ordering between them,
1215-
execution could be interleaved)
1216-
2. `after_validation`
1217-
3. the API call
1218-
4. `after`
1214+
1. `before`
1215+
2. `before_validation`
1216+
3. _validations_
1217+
4. `after_validation`
1218+
5. _the API call_
1219+
6. `after`
12191220

12201221
Steps 2, 3 and 4 only happen if validation succeeds.
12211222

lib/grape/api.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ def before(&block)
361361
imbue(:befores, [block])
362362
end
363363

364-
alias_method :before_validation, :before
364+
def before_validation(&block)
365+
imbue(:before_validations, [block])
366+
end
365367

366368
def after_validation(&block)
367369
imbue(:after_validations, [block])

lib/grape/endpoint.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ def run(env)
377377

378378
run_filters befores
379379

380+
run_filters before_validations
381+
380382
# Retieve validations from this namespace and all parent namespaces.
381383
validation_errors = []
382384
settings.gather(:validations).each do |validator|
@@ -475,6 +477,10 @@ def befores
475477
aggregate_setting(:befores)
476478
end
477479

480+
def before_validations
481+
aggregate_setting(:before_validations)
482+
end
483+
478484
def after_validations
479485
aggregate_setting(:after_validations)
480486
end

spec/grape/api_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,36 @@ def subject.enable_root_route!
692692
last_response.status.should eql 400
693693
last_response.body.should eql 'id is invalid'
694694
end
695+
696+
it 'calls filters in the correct order' do
697+
i = 0
698+
a = double('before mock')
699+
b = double('before_validation mock')
700+
c = double('after_validation mock')
701+
d = double('after mock')
702+
703+
subject.params do
704+
requires :id, type: Integer
705+
end
706+
subject.resource ':id' do
707+
before { a.here(i += 1) }
708+
before_validation { b.here(i += 1) }
709+
after_validation { c.here(i += 1) }
710+
after { d.here(i += 1) }
711+
get do
712+
'got it'
713+
end
714+
end
715+
716+
a.should_receive(:here).with(1).exactly(1).times
717+
b.should_receive(:here).with(2).exactly(1).times
718+
c.should_receive(:here).with(3).exactly(1).times
719+
d.should_receive(:here).with(4).exactly(1).times
720+
721+
get '/123'
722+
last_response.status.should eql 200
723+
last_response.body.should eql 'got it'
724+
end
695725
end
696726

697727
context 'format' do

0 commit comments

Comments
 (0)