Skip to content

Commit 2502722

Browse files
committed
Merge pull request ruby-grape#192 from ppadron/fix_multiple_calls_to_helpers
fixes ruby-grape#186: helpers should allow multiple calls with modules and blocks
2 parents 2bc15ad + 9018102 commit 2502722

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/grape/api.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,14 @@ def represent(model_class, options)
208208
# end
209209
# end
210210
# end
211-
def helpers(mod = nil, &block)
212-
if block_given? || mod
213-
mod ||= settings.peek[:helpers] || Module.new
211+
def helpers(new_mod = nil, &block)
212+
if block_given? || new_mod
213+
mod = settings.peek[:helpers] || Module.new
214+
if new_mod
215+
mod.class_eval do
216+
include new_mod
217+
end
218+
end
214219
mod.class_eval &block if block_given?
215220
set(:helpers, mod)
216221
else

spec/grape/api_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,28 @@ def hello
588588
get '/howdy'
589589
last_response.body.should eql 'Hello, world.'
590590
end
591+
592+
it 'should allow multiple calls with modules and blocks' do
593+
subject.helpers Module.new do
594+
def one
595+
1
596+
end
597+
end
598+
subject.helpers Module.new do
599+
def two
600+
2
601+
end
602+
end
603+
subject.helpers do
604+
def three
605+
3
606+
end
607+
end
608+
subject.get 'howdy' do
609+
[one, two, three]
610+
end
611+
lambda{get '/howdy'}.should_not raise_error
612+
end
591613
end
592614

593615
describe '.scope' do

0 commit comments

Comments
 (0)