Skip to content

Commit 9018102

Browse files
author
Pedro Padron
committed
fixes ruby-grape#186: helpers should allow multiple calls with modules and blocks
1 parent db4f5ad commit 9018102

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
@@ -575,6 +575,28 @@ def hello
575575
get '/howdy'
576576
last_response.body.should eql 'Hello, world.'
577577
end
578+
579+
it 'should allow multiple calls with modules and blocks' do
580+
subject.helpers Module.new do
581+
def one
582+
1
583+
end
584+
end
585+
subject.helpers Module.new do
586+
def two
587+
2
588+
end
589+
end
590+
subject.helpers do
591+
def three
592+
3
593+
end
594+
end
595+
subject.get 'howdy' do
596+
[one, two, three]
597+
end
598+
lambda{get '/howdy'}.should_not raise_error
599+
end
578600
end
579601

580602
describe '.scope' do

0 commit comments

Comments
 (0)