Skip to content

Commit

Permalink
Merge pull request ruby-grape#192 from ppadron/fix_multiple_calls_to_…
Browse files Browse the repository at this point in the history
…helpers

fixes ruby-grape#186: helpers should allow multiple calls with modules and blocks
  • Loading branch information
dblock committed Jun 29, 2012
2 parents 2bc15ad + 9018102 commit 2502722
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,14 @@ def represent(model_class, options)
# end
# end
# end
def helpers(mod = nil, &block)
if block_given? || mod
mod ||= settings.peek[:helpers] || Module.new
def helpers(new_mod = nil, &block)
if block_given? || new_mod
mod = settings.peek[:helpers] || Module.new
if new_mod
mod.class_eval do
include new_mod
end
end
mod.class_eval &block if block_given?
set(:helpers, mod)
else
Expand Down
22 changes: 22 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,28 @@ def hello
get '/howdy'
last_response.body.should eql 'Hello, world.'
end

it 'should allow multiple calls with modules and blocks' do
subject.helpers Module.new do
def one
1
end
end
subject.helpers Module.new do
def two
2
end
end
subject.helpers do
def three
3
end
end
subject.get 'howdy' do
[one, two, three]
end
lambda{get '/howdy'}.should_not raise_error
end
end

describe '.scope' do
Expand Down

0 comments on commit 2502722

Please sign in to comment.