Skip to content

Commit 69f6ccb

Browse files
committed
Merge pull request #1153 from towanda/fix-boolean
Fixes boolean declaration in an external helper file.
2 parents d3841f1 + ff6b031 commit 69f6ccb

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#### Fixes
1111

12+
* [#1153](https://github.com/ruby-grape/grape/pull/1153): Fixes boolean declaration in an external file - [@towanda](https://github.com/towanda).
1213
* [#1142](https://github.com/ruby-grape/grape/pull/1142): Makes #declared unavailable to before filters - [@jrforrest](https://github.com/jrforrest).
1314
* [#1114](https://github.com/ruby-grape/grape/pull/1114): Fix regression which broke identical endpoints with different versions - [@suan](https://github.com/suan).
1415
* [#1109](https://github.com/ruby-grape/grape/pull/1109): Memoize Virtus attribute and fix memory leak - [@marshall-lee](https://github.com/marshall-lee).

lib/grape/dsl/helpers.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module ClassMethods
2929
def helpers(new_mod = nil, &block)
3030
if block_given? || new_mod
3131
mod = new_mod || Module.new
32+
define_boolean_in_mod(mod)
3233
if new_mod
3334
inject_api_helpers_to_mod(new_mod) if new_mod.is_a?(BaseHelper)
3435
end
@@ -51,6 +52,11 @@ def helpers(new_mod = nil, &block)
5152

5253
protected
5354

55+
def define_boolean_in_mod(mod)
56+
return if defined? mod::Boolean
57+
mod.const_set('Boolean', Virtus::Attribute::Boolean)
58+
end
59+
5460
def inject_api_helpers_to_mod(mod, &_block)
5561
mod.extend(BaseHelper)
5662
yield if block_given?

spec/grape/dsl/helpers_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ def self.mod
1111
end
1212
end
1313
end
14+
15+
module BooleanParam
16+
extend Grape::API::Helpers
17+
18+
params :requires_toggle_prm do
19+
requires :toggle_prm, type: Boolean
20+
end
21+
end
22+
1423
describe Helpers do
1524
subject { Class.new(HelpersSpec::Dummy) }
1625
let(:proc) do
@@ -39,6 +48,13 @@ def test
3948

4049
expect(subject.mod).to eq mod
4150
end
51+
52+
context 'with an external file' do
53+
it 'sets Boolean as a Virtus::Attribute::Boolean' do
54+
subject.helpers BooleanParam
55+
expect(subject.mod::Boolean).to eq Virtus::Attribute::Boolean
56+
end
57+
end
4258
end
4359
end
4460
end

0 commit comments

Comments
 (0)